I have one workbook with many sheets we'll call "type A" and an equal amount of "type B" sheets in the same workbook that correspond to a given type A sheet.
For simplicity, lets say my sheets go: red, yellow, blue, dark red, dark yellow then dark blue. I want to copy both red sheets to a brand new workbook, then both yellows to a different new workbook, and so on. I also want the name of the new workbook file to be the name of the type A file (e.g. Red). Any help would be greatly appreciated.
Here's the VBA I have so far. I am able to copy the first combo to a new workbook (i.e. both reds) but after that I receive a "424" error. I am using an "i" loop and referencing sheets by their number for simplicity/generalization.
Sub export2sheets()
Dim twb As Workbook
Set twb = ThisWorkbook
Dim i As Integer
XPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To 2
    twb.Activate
    Worksheets(Array(i, (i + 2))).Copy
    With ActiveWorkbook
         Application.ActiveWorkbook.SaveAs Filename:=XPath & "\" & xWs.Name & ".xlsx"
         Application.ActiveWorkbook.Close False
    End With
    Workbooks.Add
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
EDIT 1: the code below works but it does not name the file or close it as intended, ostensibly because I removed the lines of code to do so.
Sub export2sheets()
Dim twb As Workbook
Set twb = ThisWorkbook
Dim i As Integer
XPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For i = 1 To 2
    twb.Activate
    Worksheets(Array(i, (i + 3))).Copy
Next i
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
EDIT 2: Most recent code is below.
- I have used nto generalize the number of files/type A sheets.
- I have removed anything related to xPath.
- I have changed xWstoWorksheets(i).
- I have removed Application.ActiveWorkbook.
- i have changed Close FalsetoClose Truebecause I want it to close the file when finished.
Any ideas why the code is giving me a "52" error code when pressing F8 at the SaveAs line? It currently executes up until SaveAs, thus it does not change the filename or close the file. Also, for some reason Save and Close False work, but if either SaveAs or Close True is used, it does not work.
Sub export2sheets()
    Dim twb As Workbook
    Set twb = ThisWorkbook
    Dim i As Integer
    Dim n As Integer
    n = 3 'set n = the number of type A files
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For i = 1 To n
        twb.Worksheets(Array(i, (i + n))).Copy
        SaveAs Filename:=Worksheets(i).Name & ".xlsx"
        Close True
    Next i
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub
