I am saving a copy of a workbook as a non-macro enabled workbook with some of the backend sheets hidden. I prefer to keep the backend sheets in the workbook for record keeping.
The first sheet in the workbook is one of the hidden sheets, here named "Sheet1". For some reason, when I open a copy of the newly saved workbook, it's not hidden anymore. I can even see that it was correctly hidden after the VBA saves if I add a breakpoint between saving and closing.
Any help with figuring this out is appreciated!
Sub MyVBA()
Dim ws As Worksheet
Dim wsName As Variant
'The string in quotes is what name the report should save and email as.
wsName = "My Workbook - "
ActiveWorkbook.Save
'Saves a master copy
'Name the first and last of the backend sheets you want to hide before saving a master. It will hide everything in between.
For i = Sheets("Sheet1").Index To Sheets("Sheet5").Index
Sheets(i).Select Replace:=False
Next i
ActiveWindow.SelectedSheets.Visible = False
ThisWorkbook.Sheets.Copy
With ActiveWorkbook
.SaveAs ThisWorkbook.Path & "\" & wsName & Format(Date, "yyyy-mm-dd") & ".xlsx", FileFormat:=51
.Close
End With
End Sub
EDIT:
I tested it out with explicit references, and I'm getting the same issue.
For i = Sheets("Data").Index To Sheets("Key").Index
Sheets(i).Select Replace:=False
Next i
Workbooks("Test.xlsm").Windows(1).SelectedSheets.Visible = False
Workbooks("Test.xlsm").Sheets.Copy
With ActiveWorkbook
.SaveAs Workbooks("Test.xlsm").Path & "\" & wsName & Format(Date, "yyyy-mm-dd") & ".xlsx", FileFormat:=51
.Close
End With
ActiveWindow
, or justSheets(i)
. Without that,Sheets()
will refer to whatever workbook is open, which may not be the one you're expecting. – BruceWayneFalse
is not an explicitly correct value for theVisible
property. TryxlSheetHidden
orxlSheetVeryHidden
. Why do you select the sheets and then hide them in a group? Since you already address each one, just set each one'sVisible
property. – Variatus