我将工作簿的副本保存为未启用宏的工作簿,并隐藏了一些后端工作表。我更喜欢将后端工作表保留在工作簿中以进行记录。
工作簿中的第一个工作表是隐藏工作表之一,此处名为“Sheet1”。由于某种原因,当我打开新保存的工作簿的副本时,它不再隐藏。如果我在保存和关闭之间添加断点,我什至可以看到它在 VBA 保存后正确隐藏。
任何有关解决此问题的帮助都将不胜感激!
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
编辑:
我用明确的引用对其进行了测试,并且遇到了同样的问题。
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
最佳答案
发生这种情况的原因是您的 sheets.copy
选择了隐藏sheet1。我修改了您的代码,以在您复制工作表后激活工作簿中的最后一个工作表 #。
Sub MyVBA()
Dim ws As Worksheet, i As Long
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.
Dim LastSht As Long
LastSht = ThisWorkbook.Sheets.Count
For i = Sheets("Sheet1").Index To Sheets("Sheet5").Index
Sheets(i).Select Replace:=False
Next i
ActiveWindow.SelectedSheets.Visible = False
ThisWorkbook.Sheets.Copy
Sheets(LastSht).Activate
With ActiveWorkbook
.SaveAs ThisWorkbook.path & "\" & wsName & Format(Date, "yyyy-mm-dd") & ".xlsx", FileFormat:=51
.Close
End With
End Sub
关于vba - 保存工作簿副本时,工作表变为取消隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48197793/