I'm trying to write a simple VBA Subroutine that:
- creates a new workbook in the same directory of the excel file that contains the code ("original file" from here onward)
- saves the new workbook as _export.xlsx
- copies some predefined sheets from the original file to the "*_export" one.
This is what I've got at the time being:
Sub export()
Dim myPath, folderPath, fileName, exportFileFullPath As String
Dim arrayOfSheetsToCopy As Variant
folderPath = Application.ActiveWorkbook.Path
fullPath = Application.ActiveWorkbook.FullName
fileName = Replace(Application.ActiveWorkbook.Name, ".xlsm", "")
exportFileFullPath = folderPath & "\" & fileName & "_export.xlsx"
Workbooks.Add
ActiveWorkbook.SaveAs fileName:=exportFileFullPath, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
arrayOfSheetsToCopy = Array("originalSheet1", "originalSheet2", "originalSheet3")
Workbooks(fullPath).Sheets(arrayOfSheetsToCopy).Copy After:=Workbooks(exportFileFullPath).Sheets(Sheets.Count)
End Sub
The code seems to run until a "Subscript out of range" error at Sheets(arrayOfSheetsToCopy).Copy... Initially I though to some kind of syntax error in the definition of the Array of Sheets, so I tried to write a separate .Copy instruction for each Sheet. The same code interrupts at the same point with the same error.
Any idea? Thank you!