My workbook A containing five sheets (Sheet1, Sheet2, Sheet3, Sheet4 and Sheet5). I protected the cells containing these formulas and now I want to save only Sheet1, Sheet2, Sheet3 and Sheet4 in the new workbook named "myfile".
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
I add .SaveAs Filename:="myfile" ActiveWorkbook.Close at the end but it doesnt work. How can I fixe it ?
.Close
outside aWith
statement (2) using.Copy
without pasting it anywhere (3) using aWith
statement for only one line? (4) You mentioned in your post "saving" four sheets in "the" new sheet (implying one sheet). How do you want to go about that (cannot be seen in the above code)? – Ralph.Paste
the sheets somewhere (not just copying them). – Ralph