I'm trying to copy groups of sheets to a new workbook using VBA. But the number of sheets in a group can change depending on a criterion. I have the names of the sheets that I would like to grab, stored in referenced cells.
Is there a way to "stop" after the last cell with a value? If I have 10 cells that are in the range but only 5 have values, how can I have the VBA only select the 5 with values and use that as my range?
My current code works but is still very manual.
Thank you in advance!
gpippin84
Is there a way to "stop" after the last cell with a value? If I have 10 cells that are in the range but only 5 have values, how can I have the VBA only select the 5 with values and use that as my range?
My current code works but is still very manual.
Code:
Sub ExportBranch()
Dim FirstSheet As String
With ActiveSheet
FirstSheet = .Range("B279").Value
End With
Dim SecondSheet As String
With ActiveSheet
SecondSheet = .Range("B280").Value
End With
Dim ThirdSheet As String
With ActiveSheet
ThirdSheet = .Range("B281").Value
End With
Dim FourthSheet As String
With ActiveSheet
FourthSheet = .Range("B282").Value
End With
Dim FifthSheet As String
With ActiveSheet
FifthSheet = .Range("B283").Value
End With
Sheets(Array(FirstSheet, SecondSheet, ThirdSheet, FourthSheet, FifthSheet)).Copy
'2nd Half of macro that copy and pastes values and saves the file
Sheets(SecondSheet).Select
Range("C264:N273").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("C134:N140").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Sheets(SecondSheet).Select
Dim fname As String
With ActiveSheet
fname = .Range("B284").Value
ActiveWorkbook.SaveAs Filename:= _
"\\server\folder\folder\2018 Folders & WP\2018 Budget\#Export Files\" & fname, FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWindow.Close
End With
End Sub
Thank you in advance!
gpippin84