Object defined error when opening multiple workbooks

mcwillis81

New Member
Joined
Dec 3, 2015
Messages
30
Hi all

I've written a module that runs through a table column of workbook paths

It opens each workbook
Refreshes the data connections
Then refreshes the the pivot caches
Then closes the workbook

However, once the second workbook opens I get


1004
application defined or object defined error

Is there an anything obviously wrong I'm doing?

It doesn't matter which order I put the workbooks in so I don't believe it's dodgy source data

Any thoughts would be appreciated
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
It will be a coding error.

Can you post your code and indicate which line is causing the problem?
 
Upvote 0
Hi Stephen,

Firstly thanks for your support this is dirviing me crazy!

The only thing I've omitted from the code below I the file save at the bottom. Apoligies in advance its a bit messy!


THE ERROR is pc.refresh

but only after its on the second workbook


Code:
Sub Run_Master()
''Application.ScreenUpdating = False
''Application.Calculation = xlCalculationManual
'''Application.EnableEvents = False
''Application.DisplayAlerts = False
Dim Wb As Workbook, MasterSht As Worksheet, MasterTbl As ListObject, _
RepBook As Workbook, RepSht As Worksheet, ErrorText As String, i As Long
ErrorText = " setting variables "
    Set Wb = ThisWorkbook
    Set MasterSht = Wb.Worksheets("Master Sheet")
    Set MasterTbl = MasterSht.ListObjects("Tbl_Master")
    
'On Error GoTo ErrorHandler:
With MasterTbl
    For i = 1 To MasterTbl.ListRows.Count
    
ErrorText = " Opening workbook "
        Set RepBook = Workbooks.Open(.DataBodyRange(i, 1))
        Application.Wait (Now + TimeValue("0:00:15"))
        
            'run report code here
'##################################################################################################
            
Dim DropSht As Worksheet, ProSht As Worksheet, ArcSht As Worksheet, sht1 As Worksheet, lo As ListObject
ErrorText = " set sheets "
Set ProSht = RepBook.Worksheets("Procedures")
ErrorText = " refresh list objects "
RepBook.Activate
On Error Resume Next
    For Each sht1 In RepBook.Worksheets
        For Each lo In sht1.ListObjects
            lo.QueryTable.Refresh False
            DoEvents
        Next lo
    Next sht1
On Error GoTo 0
Set lo = Nothing
Set sht1 = Nothing
On Error GoTo ErrorHandler:

With ProSht
    If UCase(.Range("A1")) = "MONDAY" Then
        .Range("H35") = Date - 3
    Else
        .Range("H35") = Date - 1
    End If
End With
'==================================================================================================
'========Update Pivots=============================================================================
Dim Pt As PivotTable, Pt1 As PivotTable
ErrorText = " refresh pivots "
On Error GoTo 0
RepBook.Activate
Dim Pc As PivotCache
RepBook.Activate
For Each Pc In RepBook.PivotCaches
Pc.Refresh
Next Pc
Set Pc = Nothing
On Error GoTo ErrorHandler:
ProSht.Range("F1").Value = Date
'==================================================================================================
'========Update Data Archive=======================================================================
 
ErrorText = " Data Archive "
RepBook.Activate
Set ArcSht = RepBook.Worksheets("Data Archive")
Dim End_Col As String
RepBook.Activate
If ArcSht.Range("B3").Value = 1 Then
   ' do nothing as already updated (removed msgbox to keep things running)
Else
    With ArcSht
    .Activate
    DoEvents
    .Columns(16).Insert xlToRight, xlFormatFromLeftOrAbove ' Sheets("Data Archive")
    .Range("Q2:" & CStr(Sheets("Data Archive").Range("Q2").End(xlToRight).Offset(321, 0).Address)).Copy
    .Range("P2").PasteSpecial xlPasteValuesAndNumberFormats 'Sheets("Data Archive")
    DoEvents
    Application.Wait (Now + TimeValue("0:00:05"))
    Application.CutCopyMode = False
End With
Set ArcSht = Nothing
With ProSht
    .Range("C5") = VBA.Format(Date, "mm/dd/yyyy")
    .Range("D5") = VBA.Format(Now, "HH:MM")
End With
End If
'==================================================================================================
'========Distribute================================================================================
ErrorText = " Distribute "
Application.Calculate
DoEvents
Dim DistroBook As Workbook, sht As Worksheet, SaveName As String, a As Integer
RepBook.Sheets(Array("Summary", "Breakdown")).Copy
Set DistroBook = ActiveWorkbook
 
DistroBook.Worksheets("Summary").UsedRange.Value = DistroBook.Worksheets("Summary").UsedRange.Value
 
            'disable events while setting properties and saving workbook
            'To prevent Pop Ups
            Application.EnableEvents = False
            On Error Resume Next
            
                'add the two custom properties to classify the document
                RepBook.CustomDocumentProperties.Add Name:="Classification", LinkToContent:=False, _
                Value:="Confidential", Type:=4
            
                    RepBook.CustomDocumentProperties.Add Name:="HeadersandFooters", LinkToContent:=False, _
                    Value:="None", Type:=4
                    
            SaveName = Trim(RepBook.Name)
             a = InStr(1, SaveName, "MI")
             SaveName = Left(SaveName, a + 1)
             Debug.Print SaveName
            
                        'Save the workbook
                        'DistroBook.SaveAs Filename:= 'file path goes here

            'On Error GoTo 0
            On Error GoTo ErrorHandler:

'Turn events back on
Application.EnableEvents = True
 
DistroBook.Close False
Set DistroBook = Nothing
RepBook.Activate
With ProSht
    .Range("C6") = VBA.Format(Date, "mm/dd/yyyy")
    .Range("D6") = VBA.Format(Now, "HH:MM")
End With
    
        RepBook.Close True
        Set RepBook = Nothing
        .DataBodyRange(i, 3) = Date
Next_i:
MsgBox "Rep " & i & " Complete."
    Next i
    
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.DisplayAlerts = True

Exit Sub
ErrorHandler:
MsgBox "Error!"
With MasterTbl
        .DataBodyRange(i, 5) = Err.Number
        .DataBodyRange(i, 6) = Err.Description
        .DataBodyRange(i, 7) = Err.LastDllError
        .DataBodyRange(i, 8) = Err.Source
        .DataBodyRange(i, 9) = ErrorText
End With
If Not RepBook Is Nothing Then RepBook.Close False
Set RepBook = Nothing
Err.Clear
Resume Next_i:

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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