我的工作簿A包含五张(Sheet1,Sheet2,Sheet3,Sheet4和Sheet5)。我保护包含这些公式的单元格,现在我只想在名为“myfile”的新工作簿中保存Sheet1,Sheet2,Sheet3和Sheet4。
Sub Protect()
Dim pwd As String, s As Long
pwd = InputBox("entrer a password", Title:="Password")
With ThisWorkbook
For s = 1 To 4
With .Worksheets("Sheet" & s)
.Copy
End With
With ActiveWorkbook
for i=1 to 4
With .Worksheets(i)
.UsedRange
On Error Resume Next
.Cells.SpecialCells(xlCellTypeBlanks).Locked = False
.Cells.SpecialCells(xlCellTypeConstants).Locked = False
.Columns("O").Hidden = True 'i want to hide it for each Sheet
.Columns("P").Hidden = True 'i want to hide it for each Sheet
.Columns("Q").Hidden = True 'i want to hide it for each Sheet
.Columns("R").Hidden = True 'i want to hide it for each Sheet
.Columns("S").Hidden = True 'i want to hide it for each Sheet
.Columns("T").Hidden = True 'i want to hide it for each Sheet
.Columns("U").Hidden = True 'i want to hide it for each Sheet
.Columns("V").Hidden = True 'i want to hide it for each Sheet
On Error GoTo 0
.protect pwd, True, True, True, True
End With
next i
End With
Next s
End With
.SaveAs Filename:="myfile" & s, FileFormat:=xlOpenXMLWorkbook
.Close SaveChanges:=False
End Sub
我添加了.SaveAs Filename:=“myfile”ActiveWorkbook.Close,但它不起作用。我怎么修理它?
答案 0 :(得分:1)
要保存4或5张,请尝试使用 .Sheets(Array("Sheet1", "Sheet2")).Copy ' Or use SheetName
,然后保存。
以下是如何保存某些工作表的示例...
Option Explicit
Sub Email_Sheets_Ali()
Dim SourceBook As Workbook
Dim Book As Workbook
Dim FilePath As String
Dim FileName As String
Dim TheActiveWindow As Window
Dim TempWindow As Window
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
Set SourceBook = ActiveWorkbook
' // Copy the sheets to a new workbook
' // We add a temporary Window to avoid the Copy problem
' // if there is a List or Table in one of the sheets and
' // if the sheets are grouped
With SourceBook
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("Sheet1", "Sheet2")).Copy ' Or use SheetName
End With
' // Close temporary Window
TempWindow.Close
Set Book = ActiveWorkbook
' // Save the new workbook
FilePath = "C:\Temp\"
FileName = "MyFileName"
With Book
.SaveAs FilePath & FileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
.Close savechanges:=False
End With
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub