I try to move sheets in 2 Excel files. Will anyone help me and answer, why the code below doesn't work? The first moving is done, but doing second VBA generates error: "Run-time error '1004': Method 'Sheets' of object '_Global' failed".
Platform: MS Access 2010, Excel 2010, Windows 7. References on: Visual Basic for Applications, Microsoft Access 14.0 Object Library. Microsoft Excel 14.0 Object Library, OLE Automation, Microsoft ActiveX Data Objects 6.0 Library.
Sub test()
Dim my_file As Object
Set my_file = CreateObject("Excel.Application")
my_file.Visible = True
my_file.UserControl = True
my_file.Workbooks.Open ("c:\data\test_1_.xlsx")
my_file.Sheets("Sheet1").Move After:=Sheets("Sheet2")
my_file.ActiveWorkbook.Save
my_file.Quit
Set my_file = Nothing
Set my_file = CreateObject("Excel.Application")
my_file.Visible = True
my_file.UserControl = True
my_file.Workbooks.Open ("c:\data\test_2_.xlsx")
my_file.Sheets("Sheet1").Move After:=Sheets("Sheet2") 'error occurs on this line
my_file.ActiveWorkbook.Save
my_file.Quit
Set my_file = Nothing
End Sub
Platform: MS Access 2010, Excel 2010, Windows 7. References on: Visual Basic for Applications, Microsoft Access 14.0 Object Library. Microsoft Excel 14.0 Object Library, OLE Automation, Microsoft ActiveX Data Objects 6.0 Library.
Sub test()
Dim my_file As Object
Set my_file = CreateObject("Excel.Application")
my_file.Visible = True
my_file.UserControl = True
my_file.Workbooks.Open ("c:\data\test_1_.xlsx")
my_file.Sheets("Sheet1").Move After:=Sheets("Sheet2")
my_file.ActiveWorkbook.Save
my_file.Quit
Set my_file = Nothing
Set my_file = CreateObject("Excel.Application")
my_file.Visible = True
my_file.UserControl = True
my_file.Workbooks.Open ("c:\data\test_2_.xlsx")
my_file.Sheets("Sheet1").Move After:=Sheets("Sheet2") 'error occurs on this line
my_file.ActiveWorkbook.Save
my_file.Quit
Set my_file = Nothing
End Sub