zmasterdevil
New Member
- Joined
- Dec 5, 2022
- Messages
- 11
- Office Version
- 365
- Platform
- Windows
Hello Everyone,
When I click a button on a sheet within my workbook, I want a macro to do the following:
I've put my code below. I keep getting a "Run-time Error '9': Subscript out of range" (I've attached a screenshot of where the debugger stops on the "Sheets(Array(Range("AA1").Value)).Copy" line. I've looked up a lot of information on arrays, but haven't found anything that works for what I'm trying to do.
I welcome any help, or suggestions of other ways to accomplish my aim if using an array for this won't work. Apologies for any coding blunders, I do not know much VBA code.
When I click a button on a sheet within my workbook, I want a macro to do the following:
- Copy all the sheets named in cell “AA1” to a new workbook (I’m having trouble getting a dynamic array to work for this). If the array needs each sheet name in a different cell, I can put the sheet names in “AA1”, “AB1”, “AC1”, etc…
- Then save the new workbook into the Documents folder using data from 2 cells on the active sheet (“Z2”, & “AA2”) to name the file. Cell “Z2” is the account number, and cell “AA2” is the month/year of the workbook in format “mmmm yyyy” (this is NOT the current month/year).
- Then close the new workbook.
I've put my code below. I keep getting a "Run-time Error '9': Subscript out of range" (I've attached a screenshot of where the debugger stops on the "Sheets(Array(Range("AA1").Value)).Copy" line. I've looked up a lot of information on arrays, but haven't found anything that works for what I'm trying to do.
I welcome any help, or suggestions of other ways to accomplish my aim if using an array for this won't work. Apologies for any coding blunders, I do not know much VBA code.
VBA Code:
Sub Copy_Sheets_to_New_Workbook()
Dim FilePath As String
Dim FileExt As String
Dim FileName As String
Dim FileFullPath As String
Dim FileFormat As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set wb1 = ThisWorkbook
Sheets(Array(Range("AA1").Value)).Copy
Set wb2 = ActiveWorkbook
'Below gets the File Extension and File Format
With wb2
If Val(Application.Version) < 12 Then
FileExt = ".xls": FileFormat = -4143
Else
Select Case wb1.FileFormat
Case 51: FileExt = ".xlsx": FileFormat = 51
Case 52:
If .HasVBProject Then
FileExt = ".xlsm": FileFormat = 52
Else
FileExt = ".xlsx": FileFormat = 51
End If
Case 56: FileExt = ".xls": FileFormat = 56
Case Else: FileExt = ".xlsb": FileFormat = 50
End Select
End If
End With
'Save workbook in Documents folder of your system
FilePath = "C:\Users\JAMES\Documents"
'Now append month/year to the filename
FileName = Range("Z2").Value & " " & Range("AA2").Value 'Possibly change 2nd cell to: Format(Range("AA2").Value), "mmmm yyyy")
'Complete path of the file where it is saved
FileFullPath = FilePath & FileName & FileExt
'Now save currect workbook at the above path
wb2.SaveAs FileFullPath, FileFormat:=FileFormat
wb2.Close SaveChanges:=True
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub