Object Variable Not Set Error when opening workbooks - BUT WORKS FINE WHEN I INSERT BREAKPOINTS

nigelandrewfoster

Well-known Member
Joined
May 27, 2009
Messages
747
Hi,

Code below works fine if I put breakpoints in the first procedure, but stops with above error on highlighted line when I don't. Why please? And how can I overcome this? Puzzled! Thanks.

Code:
Sub Compile_Megalist()    Dim wsCustomers As Worksheet
    Dim wsHistoric As Worksheet
    Dim wsNewArrivals As Worksheet
    
    [B]Set wsCustomers = OpenWorkbook(strMailChimpPath(), "Megalist A - CUSTOMERS.xlsx").Worksheets(1)[/B]
    Set wsHistoric = OpenWorkbook(strMailChimpPath(), "Megalist B - HISTORY.xlsx").Worksheets(1)
    Set wsNewArrivals = OpenWorkbook(strMailChimpPath(), "Megalist C - NEW ARRIVALS.xlsx").Worksheets(1)
    
End Sub
Code:
Function OpenWorkbook(strPath As String, strFilename As String) As Workbook

    On Error Resume Next
    Set OpenWorkbook = Workbooks(strFilename)
    If Err.Number = 9 Then Set OpenWorkbook = Workbooks.Open(Filename:=strPath & strFilename)
    On Error GoTo 0
End Function
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The following alteration works but it's dreadfully inelegant:
Code:
Sub Compile_MegalistA()    Dim wsCustomers As Worksheet
    Dim wsHistoric As Worksheet
    Dim wsNewArrivals As Worksheet
    
    On Error Resume Next
    Set wsCustomers = OpenWorkbook(strMailChimpPath(), "Megalist A - CUSTOMERS.xlsx").Worksheets(1)
    Do While Err.Number <> 0
        Err.Clear
        Set wsCustomers = OpenWorkbook(strMailChimpPath(), "Megalist A - CUSTOMERS.xlsx").Worksheets(1)
    Loop
    Set wsHistoric = OpenWorkbook(strMailChimpPath(), "Megalist B - HISTORY.xlsx").Worksheets(1)
    Do While Err.Number <> 0
        Err.Clear
        Set wsHistoric = OpenWorkbook(strMailChimpPath(), "Megalist B - HISTORY.xlsx").Worksheets(1)
    Loop
    Set wsNewArrivals = OpenWorkbook(strMailChimpPath(), "Megalist C - NEW ARRIVALS.xlsx").Worksheets(1)
    Do While Err.Number <> 0
        Err.Clear
        Set wsNewArrivals = OpenWorkbook(strMailChimpPath(), "Megalist C - NEW ARRIVALS.xlsx").Worksheets(1)
    Loop
    
End Sub
 
Upvote 0
I'm not a fan of this particular style of error handling as it is hiding what's going wrong. Are you really working a lot with workbooks that are sometimes open, sometimes not open? These workbooks appear to have problems opening, as the root cause of the trouble.
 
Upvote 0
I'm not a fan at all. Makes my skin crawl to type in the code, but it is the only way I can get the thing to work. The only thing I can think is the workbooks are stored on OneDrive and the delay in opening causes the issue. No nearer a proper solution though. I've only seen this before when using VBA code to rename PDF files I CREATED using an external app (in the days of Excel 2003). Cheers for your interest in my post.
 
Upvote 0
I don't have any experience with using files that are stored in One Drive, I'm afraid.
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,185
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top