|
-
May 15th, 2006, 12:07 PM
#1
[RESOLVED] Application-defined or object defined error in Excel VBA from VB6
Note: Posting this thread for Hack
I am getting an "Runtime error 1004 Application-defined or object defined error" on the highlighted line when I run this sub
VB Code:
Private Sub PrintIssues()
Dim i As Long
Set objExcel = New Excel.Application
Set bkWorkBook = objExcel.Workbooks.Add
Set shWorkSheet = bkWorkBook.ActiveSheet
If optAppeal.Value = True Then
shWorkSheet.Range("A1") = "Appeals Issues Log"
Else
shWorkSheet.Range("A1") = "Reopens Issues Log"
End If
shWorkSheet.Range("B3") = "Prov. Name: " & strProvName
shWorkSheet.Range("A4") = "Prov. Number: " & strProvCode
' shWorkSheet.Range("C3") = "FYE: " & cboFYE.Text
If optAppeal.Value = True Then
shWorkSheet.Range("A5") = "Appeal"
Else
shWorkSheet.Range("A5") = "Reopen"
End If
shWorkSheet.Range("C5") = "Number " & gstrAppealNo
shWorkSheet.Range("A8") = "Issue No"
shWorkSheet.Range("B8") = "Issue"
shWorkSheet.Range("C8") = "Analyst"
shWorkSheet.Range("D8") = "Disposition"
shWorkSheet.Range("E8") = "Est. Impact Amount"
shWorkSheet.Range("F8") = "Act. Impact Amount"
shWorkSheet.Range("G8") = "Comments"
Set rngRowStart = shWorkSheet.Range("A10")
bkWorkBook.Worksheets(1).Columns(1).HorizontalAlignment = xlLeft
bkWorkBook.Worksheets(1).PageSetup.Orientation = xlLandscape
bkWorkBook.Worksheets(1).PageSetup.Zoom = False
'in order for these PageSetup things to work, the ZOOM property
'must be set to false
bkWorkBook.Worksheets(1).PageSetup.FitToPagesWide = 1
bkWorkBook.Worksheets(1).PageSetup.FitToPagesTall = 1
bkWorkBook.Worksheets(1).Columns("E:E").HorizontalAlignment = xlCenter
bkWorkBook.Worksheets(1).Columns("F:F").HorizontalAlignment = xlCenter
For i = 1 To lvwIssues.ListItems.Count
'Place each element in the coresponding column
rngRowStart.Offset(0, 0).Value = lvwIssues.ListItems(1).Text 'issue number
rngRowStart.Offset(0, 1).Value = lvwIssues.ListItems(2).Text 'issue description
rngRowStart.Offset(0, 2).Value = lvwIssues.ListItems(3).Text 'analyst
rngRowStart.Offset(0, 3).Value = lvwIssues.ListItems(4).Text 'disposition
rngRowStart.Offset(0, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
rngRowStart.Offset(0, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
rngRowStart.Offset(0, 6).Value = lvwIssues.ListItems(7).Text
Next
'Next Row
Set rngRowStart = rngRowStart.Offset(1, 0)
'start with first row of recordset display and wedgie down a couple
lngLast = bkWorkBook.Worksheets(1).Range("A10").End(xlDown).Row + 2
[hl=yellow]bkWorkBook.Worksheets(1).Cells(lngLast, 5).Value = Format(lblEstImpAmt.Caption, "###,#0")[/hl] 'est impact total
bkWorkBook.Worksheets(1).Cells(lngLast, 6).Value = Format(lblActImpAmt.Caption, "###,#0") 'act impact total
'make sure everything displays properly.
shWorkSheet.Columns("A:BZ").AutoFit
objExcel.Visible = True
End Sub
Last edited by Hack; May 16th, 2006 at 08:48 AM.
Reason: Added [RESOLVED] to thread title and green "resolved" checkmark
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
May 15th, 2006, 01:42 PM
#2
Re: Application-defined or object defined error in Excel VBA from VB6
Thanks for posting this question for me Rob! 
Lets approach this from another angle and ask the question a different way.
Each time this report is run, it will display a different number of records. Regardless of how many records there are, I need to display two totals two rows below whatever the last row is. The totals will always be in columns E and F, but the rows they will be in will differ each time the Excel report is run.
What I'm doing isn't working. How would you do it?
-
May 15th, 2006, 01:46 PM
#3
Re: Application-defined or object defined error in Excel VBA from VB6
Just a quick interjection, could it be that your label caption:
needs to be
VB Code:
bkWorkBook.Worksheets(1).lblEstImpAmt.Caption
I've been caught by that before...
As far as your last question goes, I don't know that there are any easier ways of doing it. You could use the UsedRange or SpecialCells methods to figure out where the last used cells are instead of using End, but I don't know that it will make a vast difference unless somebody has already been filling in that column with other data and it is fooled into thinking that the end of the column is row 45601.
zaza
Last edited by zaza; May 15th, 2006 at 01:50 PM.
-
May 15th, 2006, 01:50 PM
#4
Re: Application-defined or object defined error in Excel VBA from VB6
 Originally Posted by zaza
Just a quick interjection, could it be that your label caption:
needs to be
VB Code:
bkWorkBook.Worksheets(1).lblEstImpAmt.Caption
I've been caught by that before...
zaza
But, the label isn't on my worksheet. It is on a VB6 form.
-
May 15th, 2006, 01:51 PM
#5
Re: Application-defined or object defined error in Excel VBA from VB6
Then "Userform1." ? I guess this code is also within the userform then, so it seems unlikely that this would be causing the problem, but if it's in a module or in the worksheet...
Ah, whoops, it's automated, non?
My mistake.
Why do you swap from using the defined sheet to using worksheets(1), BTW? I guess if you step through it, you'll find that lnglast isn't coming up with a number that can be used in the cell? Maybe CInt is the way forward or, as you say, another method of finding the last used cell.
For example:
VB Code:
Set c = Range("E:E").SpecialCells(xlCellTypeLastCell)
msgbox(c.row)
will return the row of the last used cell in column E. You want two cells below that...
Last edited by zaza; May 15th, 2006 at 02:00 PM.
-
May 15th, 2006, 02:02 PM
#6
Re: Application-defined or object defined error in Excel VBA from VB6
Actually UsedRange probably won't work - it just applies to the worksheet rather than the column. But SpecialCells will.
zaza
-
May 15th, 2006, 02:27 PM
#7
Re: Application-defined or object defined error in Excel VBA from VB6
I tested that code and lngLast = 65538, because..
VB Code:
bkWorkBook.Worksheets(1).Range("A10").End(xlDown).Row = 65536
Which is the last possible row number for a column I think. Is it ok?
Edit: I was adding values to cells the wrong way, now lngLast is getting the correct row number. By the way, you are not using i variable in the loop, i think it should be..
VB Code:
For i = 1 To lvwIssues.ListItems.Count
'Place each element in the coresponding column
rngRowStart.Offset(i, 0).Value = lvwIssues.ListItems(1).Text 'issue number
rngRowStart.Offset(i, 1).Value = lvwIssues.ListItems(2).Text 'issue description
rngRowStart.Offset(i, 2).Value = lvwIssues.ListItems(3).Text 'analyst
rngRowStart.Offset(i, 3).Value = lvwIssues.ListItems(4).Text 'disposition
rngRowStart.Offset(i, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
rngRowStart.Offset(i, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
rngRowStart.Offset(i, 6).Value = lvwIssues.ListItems(7).Text
Next
Last edited by jcis; May 15th, 2006 at 02:47 PM.
-
May 15th, 2006, 03:18 PM
#8
Re: Application-defined or object defined error in Excel VBA from VB6
Posted over on CodeGuru, but I'll share over here too. If the range that you find the .End from starts at the last row (or higher) of the used range, it returns 65536. It should work normally if "A10" is within the UsedRange.
-
May 16th, 2006, 06:03 AM
#9
Re: Application-defined or object defined error in Excel VBA from VB6
 Originally Posted by Comintern
Posted over on CodeGuru, but I'll share over here too. If the range that you find the .End from starts at the last row (or higher) of the used range, it returns 65536. It should work normally if "A10" is within the UsedRange.
I posted it on CG just to see if I could, and I could. I got an error when I tried to post it here, which I'm looking into. That is why RobDog888 posted the question for me. 
How would I ensure A10 is within the UsedRange?
-
May 16th, 2006, 06:03 AM
#10
Re: Application-defined or object defined error in Excel VBA from VB6
 Originally Posted by jcis
By the way, you are not using i variable in the loop, i think it should be..
VB Code:
For i = 1 To lvwIssues.ListItems.Count
'Place each element in the coresponding column
rngRowStart.Offset(i, 0).Value = lvwIssues.ListItems(1).Text 'issue number
rngRowStart.Offset(i, 1).Value = lvwIssues.ListItems(2).Text 'issue description
rngRowStart.Offset(i, 2).Value = lvwIssues.ListItems(3).Text 'analyst
rngRowStart.Offset(i, 3).Value = lvwIssues.ListItems(4).Text 'disposition
rngRowStart.Offset(i, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
rngRowStart.Offset(i, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
rngRowStart.Offset(i, 6).Value = lvwIssues.ListItems(7).Text
Next
I'll give this a shot. Thanks.
-
May 16th, 2006, 06:25 AM
#11
Re: Application-defined or object defined error in Excel VBA from VB6
 Originally Posted by Hack
I posted it on CG just to see if I could, and I could. I got an error when I tried to post it here, which I'm looking into. That is why RobDog888 posted the question for me.
How would I ensure A10 is within the UsedRange?
Something like this works:
VB Code:
If bkWorkBook.Worksheets(1).UsedRange.Rows > 10 Then
'...
-
May 16th, 2006, 08:49 AM
#12
Re: [RESOLVED] Application-defined or object defined error in Excel VBA from VB6
Thanks to jcis and Comintern I have this working. 
(Thanks to RobDog888 for posting this question for me too! )
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|