Results 1 to 12 of 12

Thread: [RESOLVED] Application-defined or object defined error in Excel VBA from VB6

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Resolved [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:
    1. Private Sub PrintIssues()
    2. Dim i As Long
    3.  
    4.     Set objExcel = New Excel.Application
    5.     Set bkWorkBook = objExcel.Workbooks.Add
    6.     Set shWorkSheet = bkWorkBook.ActiveSheet
    7.       If optAppeal.Value = True Then
    8.         shWorkSheet.Range("A1") = "Appeals Issues Log"
    9.       Else
    10.         shWorkSheet.Range("A1") = "Reopens Issues Log"
    11.       End If
    12.         shWorkSheet.Range("B3") = "Prov. Name:  " & strProvName
    13.         shWorkSheet.Range("A4") = "Prov. Number:  " & strProvCode
    14.        ' shWorkSheet.Range("C3") = "FYE:  " & cboFYE.Text
    15.       If optAppeal.Value = True Then
    16.         shWorkSheet.Range("A5") = "Appeal"
    17.       Else
    18.         shWorkSheet.Range("A5") = "Reopen"
    19.       End If
    20.         shWorkSheet.Range("C5") = "Number " & gstrAppealNo
    21.         shWorkSheet.Range("A8") = "Issue No"
    22.         shWorkSheet.Range("B8") = "Issue"
    23.         shWorkSheet.Range("C8") = "Analyst"
    24.         shWorkSheet.Range("D8") = "Disposition"
    25.         shWorkSheet.Range("E8") = "Est. Impact Amount"
    26.         shWorkSheet.Range("F8") = "Act. Impact Amount"
    27.         shWorkSheet.Range("G8") = "Comments"
    28.  
    29.     Set rngRowStart = shWorkSheet.Range("A10")
    30.          
    31.         bkWorkBook.Worksheets(1).Columns(1).HorizontalAlignment = xlLeft
    32.         bkWorkBook.Worksheets(1).PageSetup.Orientation = xlLandscape
    33.         bkWorkBook.Worksheets(1).PageSetup.Zoom = False
    34.         'in order for these PageSetup things to work, the ZOOM property
    35.         'must be set to false
    36.         bkWorkBook.Worksheets(1).PageSetup.FitToPagesWide = 1
    37.         bkWorkBook.Worksheets(1).PageSetup.FitToPagesTall = 1
    38.         bkWorkBook.Worksheets(1).Columns("E:E").HorizontalAlignment = xlCenter
    39.         bkWorkBook.Worksheets(1).Columns("F:F").HorizontalAlignment = xlCenter
    40.              
    41.             For i = 1 To lvwIssues.ListItems.Count
    42.                 'Place each element in the coresponding column
    43.                 rngRowStart.Offset(0, 0).Value = lvwIssues.ListItems(1).Text 'issue number
    44.                 rngRowStart.Offset(0, 1).Value = lvwIssues.ListItems(2).Text 'issue description
    45.                 rngRowStart.Offset(0, 2).Value = lvwIssues.ListItems(3).Text 'analyst
    46.                 rngRowStart.Offset(0, 3).Value = lvwIssues.ListItems(4).Text 'disposition
    47.                 rngRowStart.Offset(0, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
    48.                 rngRowStart.Offset(0, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
    49.                 rngRowStart.Offset(0, 6).Value = lvwIssues.ListItems(7).Text
    50.             Next
    51.                    
    52.             'Next Row
    53.             Set rngRowStart = rngRowStart.Offset(1, 0)
    54.        
    55.        'start with first row of recordset display and wedgie down a couple
    56.        lngLast = bkWorkBook.Worksheets(1).Range("A10").End(xlDown).Row + 2
    57.        [hl=yellow]bkWorkBook.Worksheets(1).Cells(lngLast, 5).Value = Format(lblEstImpAmt.Caption, "###,#0")[/hl] 'est impact total
    58.        bkWorkBook.Worksheets(1).Cells(lngLast, 6).Value = Format(lblActImpAmt.Caption, "###,#0") 'act impact total
    59.  
    60.         'make sure everything displays properly.
    61.         shWorkSheet.Columns("A:BZ").AutoFit
    62.        
    63.         objExcel.Visible = True
    64. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  3. #3
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Application-defined or object defined error in Excel VBA from VB6

    Just a quick interjection, could it be that your label caption:

    VB Code:
    1. lblEstImpAmt.Caption

    needs to be

    VB Code:
    1. 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.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Application-defined or object defined error in Excel VBA from VB6

    Quote Originally Posted by zaza
    Just a quick interjection, could it be that your label caption:

    VB Code:
    1. lblEstImpAmt.Caption

    needs to be

    VB Code:
    1. 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.

  5. #5
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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:
    1. Set c = Range("E:E").SpecialCells(xlCellTypeLastCell)
    2. 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.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  7. #7
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Application-defined or object defined error in Excel VBA from VB6

    I tested that code and lngLast = 65538, because..
    VB Code:
    1. 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:
    1. For i = 1 To lvwIssues.ListItems.Count
    2.         'Place each element in the coresponding column
    3.         rngRowStart.Offset(i, 0).Value = lvwIssues.ListItems(1).Text 'issue number
    4.         rngRowStart.Offset(i, 1).Value = lvwIssues.ListItems(2).Text 'issue description
    5.         rngRowStart.Offset(i, 2).Value = lvwIssues.ListItems(3).Text 'analyst
    6.         rngRowStart.Offset(i, 3).Value = lvwIssues.ListItems(4).Text 'disposition
    7.         rngRowStart.Offset(i, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
    8.         rngRowStart.Offset(i, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
    9.         rngRowStart.Offset(i, 6).Value = lvwIssues.ListItems(7).Text
    10.     Next
    Last edited by jcis; May 15th, 2006 at 02:47 PM.

  8. #8
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    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.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Application-defined or object defined error in Excel VBA from VB6

    Quote 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?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Application-defined or object defined error in Excel VBA from VB6

    Quote Originally Posted by jcis
    By the way, you are not using i variable in the loop, i think it should be..
    VB Code:
    1. For i = 1 To lvwIssues.ListItems.Count
    2.         'Place each element in the coresponding column
    3.         rngRowStart.Offset(i, 0).Value = lvwIssues.ListItems(1).Text 'issue number
    4.         rngRowStart.Offset(i, 1).Value = lvwIssues.ListItems(2).Text 'issue description
    5.         rngRowStart.Offset(i, 2).Value = lvwIssues.ListItems(3).Text 'analyst
    6.         rngRowStart.Offset(i, 3).Value = lvwIssues.ListItems(4).Text 'disposition
    7.         rngRowStart.Offset(i, 4).Value = Format(lvwIssues.ListItems(5).Text, "###,#0") 'est impact amt
    8.         rngRowStart.Offset(i, 5).Value = Format(lvwIssues.ListItems(6).Text, "###,#0") 'act impact amt
    9.         rngRowStart.Offset(i, 6).Value = lvwIssues.ListItems(7).Text
    10.     Next
    I'll give this a shot. Thanks.

  11. #11
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Application-defined or object defined error in Excel VBA from VB6

    Quote 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:
    1. If bkWorkBook.Worksheets(1).UsedRange.Rows > 10 Then
    2.     '...

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width