I have this code:
Sub test()
Dim wb As String
Dim wbb As Workbook
wb = "C:\xyz.xlsx"
Set wbb = Workbooks.Open(wb)
MsgBox ("testing")
wbb.Close
End Sub
The above does the job just fine. Workbook closes as expected
This code however, does not seem to work. But to me it looks identical in relation to the workbooks.close function. Can someone please advise why this throws the error "Run-time error '1004': Application-defined or object-defined error"
Sub filesTest(folder, dex)
Dim wb As String
Dim wbb As Workbook
Dim dat
Dim dat2
Dim dat3
Dim dat4
wb = "xyz.xlsm"
For Each file In folder.Files
If file.Name Like "*.xlsm" Then
If InStr(file.Name, "~$") = 0 Then
'sendFile (file.Name)
Set wbb = Workbooks.Open(file)
Worksheets("Sheet 1").Select
dat = Range("F11")
Worksheets("Sheet 2").Select
dat2 = Range("C54")
dat3 = Range("D54")
dat4 = Range("E54")
wbb.Close
Workbooks(wb).Activate
Range("B" & dex) = dat
Range("C" & dex) = dat2
Range("D" & dex) = dat3
Range("E" & dex) = dat4
End If
End If
Next
End Sub
I have attempted to move the location of the wbb.close but this does not seem to help at all.
I have also attempted to use an alternative "Activeworkbook.close false" and variations on this with exactly the same error message appearing.
I have also confirmed Set wbb = Workbooks.Open(file) <-- that the file variable has the same format as the code in the test block
Any help would be much appreciated,
Thank you.