There you go, I know it can be made shorter and is kind of repetitive, but it should get the job done!
Let me know if this works for you.
UPDATED (Browse for folder added):
Sub MoveSheets()
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ActiveWorkbook.Path & "\"
.Show
If .SelectedItems.Count = 0 Then Exit Sub
fdlr = .SelectedItems(1)
End With
Dim oXLApp As Object, wb As Object, wb2 As Object, ws As Object
Dim TempFile1 As String, TempFile2 As String
Dim CountA As Long, CountG As Long
TempFile1 = Environ$("temp") & "/" & "1" & Format(Now, "dd-mm-yy h-mm-ss") & ".xlsm"
TempFile2 = Environ$("temp") & "/" & "2" & Format(Now, "dd-mm-yy h-mm-ss") & ".xlsm"
On Error Resume Next
Kill TempFile1
Kill TempFile2
On Error GoTo 0
ThisWorkbook.SaveCopyAs TempFile1
ThisWorkbook.SaveCopyAs TempFile2
'save AFILE
Set oXLApp = CreateObject("Excel.Application")
Set wb = oXLApp.Workbooks.Open(TempFile1)
oXLApp.DisplayAlerts = False
For Each ws In wb.Worksheets
ws.Visible = True
Next
CountA = 0
For Each ws In wb.Worksheets
If Right(ws.Name, 2) = "-A" Then CountA = CountA + 1
Next
If Not CountA = 0 Then
For Each ws In wb.Worksheets
If Not Right(ws.Name, 2) = "-A" Then ws.Delete
Next
'you can change the "FileFormat" in the below line to xlOpenXMLWorkbookMacroEnabled
'as well as change the extension to ".xlsm" in case you want to retain macro in your saved files
wb.SaveAs Filename:=fdlr & "\" & "AFILE" & " " & Format(Now, "mm-dd-yy hh-mm") & ".xlsx", FileFormat:=xlOpenXMLWorkbook
Set wb2 = oXLApp.ActiveWorkbook
wb2.Close (False)
End If
oXLApp.DisplayAlerts = True
On Error Resume Next
Kill TempFile1
On Error GoTo 0
oXLApp.Quit
Set oXLApp = Nothing
Set wb = Nothing
Set wb2 = Nothing
Set ws = Nothing
'save GFILE
Set oXLApp = CreateObject("Excel.Application")
Set wb = oXLApp.Workbooks.Open(TempFile2)
oXLApp.DisplayAlerts = False
For Each ws In wb.Worksheets
ws.Visible = True
Next
CountG = 0
For Each ws In wb.Worksheets
If Right(ws.Name, 2) = "-G" Then CountG = CountG + 1
Next
If Not CountG = 0 Then
For Each ws In wb.Worksheets
If Not Right(ws.Name, 2) = "-G" Then ws.Delete
Next
'you can change the "FileFormat" in the below line to xlOpenXMLWorkbookMacroEnabled
'as well as change the extension to ".xlsm" in case you want to retain macro in your saved files
wb.SaveAs Filename:=fdlr & "\" & "GFILE" & " " & Format(Now, "mm-dd-yy hh-mm") & ".xlsx", FileFormat:=xlOpenXMLWorkbook
Set wb2 = oXLApp.ActiveWorkbook
wb2.Close (False)
End If
oXLApp.DisplayAlerts = True
On Error Resume Next
Kill TempFile2
On Error GoTo 0
oXLApp.Quit
Set oXLApp = Nothing
Set wb = Nothing
Set wb2 = Nothing
Set ws = Nothing
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
If Right(ws.Name, 2) = "-A" Or Right(ws.Name, 2) = "-G" Then ws.Delete
Next
Application.DisplayAlerts = True
End Sub
If Right(ws.Name, 3) = "-A"
will that ever be true? – Tim Williams(ws.Name, 2)
. Anyway I posted an answer :) – CaptainABC