WB.Sheets(2).Activate
WB.Sheets(2).Select
With WKS
.Range(.Cells(3, 3), .Cells(ClientRows, SavedColumn - 1)).Locked = False
.Range(.Cells(3, 3), .Cells(ClientRows, SavedColumn - 1)).FormulaHidden = False
End With
With WKS
.Range(.Cells(3, 1), .Cells(ClientRows, 1)).Locked = False
.Range(.Cells(3, 1), .Cells(ClientRows, 1)).FormulaHidden = False
End With
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
我循环遍历数据表并生成多个Excel工作表。附加的代码第一次完美地工作但是失败的"对象变量或With Block变量未设置"。谁能帮忙。感谢
答案 0 :(得分:-1)
以下是我的回答:
Option Explicit
Sub TestActivesheet()
Dim WB As Workbook 'Declare de vars
Dim WKS As Worksheet
Set WB = ThisWorkbook 'Or any other workbook || set the var
WB.Sheets(2).Activate
WB.Sheets(2).Select
Set WKS = WB.Sheets(2) 'Again set the var
With WKS
.Range(.Cells(3, 3), .Cells(ClientRows, SavedColumn - 1)).Locked = False
.Range(.Cells(3, 3), .Cells(ClientRows, SavedColumn - 1)).FormulaHidden = False
End With
With WKS
.Range(.Cells(3, 1), .Cells(ClientRows, 1)).Locked = False
.Range(.Cells(3, 1), .Cells(ClientRows, 1)).FormulaHidden = False
End With
'ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
WKS.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
'ActiveSheet.EnableSelection = xlUnlockedCells
WKS.EnableSelection = xlUnlockedCells
End Sub
始终位于每个模块的顶部,使用此Option Explicit
是一种很好的做法,这样当您使用变量时,编译器会要求您声明它,并避免此错误。