I have a macro to import 5 workbooks into one (and rename them).
The debugger stops after importing the first file. The wb2 part gets a runtime error: -2147221080 (800401a8)
The runtime error occurs here:
Set wb2 = Workbooks.Open(Ret2)
wb2.Sheets(1).copy wb1.Sheets(2) <- runtime error!
ActiveSheet.Name = "ZR141 Closing Stock"
wb2.Close savechanges:=False
Hope someone can fix this annoying error for me. Thank you.
The debugger stops after importing the first file. The wb2 part gets a runtime error: -2147221080 (800401a8)
Code:
Sub ImportFiles()
ChDrive "X"
ChDir "X:\Test"
Dim wb1 As Workbook, wb2 As Workbook, wb3 As Workbook, wb4 As Workbook, wb5 As Workbook
Dim Ret1, Ret2, Ret3, Ret4, Ret5
Set wb1 = ActiveWorkbook
'~~> Get the first File
Ret1 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Opening Stock ZR141")
If Ret1 = False Then Exit Sub
'~~> Get the 2nd File
Ret2 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Closing Stock ZR141")
If Ret2 = False Then Exit Sub
'~~> Get the 3rd File
Ret3 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "MB5B Opening Stock")
If Ret3 = False Then Exit Sub
'~~> Get the 4th File
Ret4 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "MB5B Closing Stock")
If Ret4 = False Then Exit Sub
'~~> Get the 5th File
Ret5 = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", _
, "Masterdata")
If Ret5 = False Then Exit Sub
'Change name and open workbooks
Set wb1 = Workbooks.Open(Ret1)
wb1.Sheets(1).copy wb1.Sheets(1)
ActiveSheet.Name = "ZR141 Opening Stock"
wb1.Close savechanges:=False
Set wb2 = Workbooks.Open(Ret2)
[B]wb2.Sheets(1).copy wb1.Sheets(2) <- runtime error!
[/B] ActiveSheet.Name = "ZR141 Closing Stock"
wb2.Close savechanges:=False
Set wb3 = Workbooks.Open(Ret3)
wb3.Sheets(1).copy wb1.Sheets(3)
ActiveSheet.Name = "MB5B Opening Stock"
wb3.Close savechanges:=False
Set wb4 = Workbooks.Open(Ret4)
wb4.Sheets(1).copy wb1.Sheets(4)
ActiveSheet.Name = "MB5B Closing Stock"
wb4.Close savechanges:=False
Set wb5 = Workbooks.Open(Ret5)
wb5.Sheets(1).copy wb1.Sheets(5)
ActiveSheet.Name = "Masterdata"
wb5.Close savechanges:=False
Set wb2 = Nothing
Set wb3 = Nothing
Set wb4 = Nothing
Set wb5 = Nothing
Set wb1 = Nothing
End Sub
The runtime error occurs here:
Set wb2 = Workbooks.Open(Ret2)
wb2.Sheets(1).copy wb1.Sheets(2) <- runtime error!
ActiveSheet.Name = "ZR141 Closing Stock"
wb2.Close savechanges:=False
Hope someone can fix this annoying error for me. Thank you.