除了最后一部分,我已经完成了所有工作。我在这一行得到一个Object变量/块变量错误:
ExecutiveWorkBook.Worksheets("Summary").Range("A1:Q22000").Locked = True
ExecutiveWorkBook.Worksheets("Summary").Protect "12345+", True, True
' CLOSE THE SOURCE FILE.
ExecutiveWorkBook.Close ' FALSE - DON'T SAVE THE SOURCE FILE.
我想锁定Executive工作簿的sheetrange。请帮忙。 以下是我的整个代码供参考。
Sub Executive_Files_Update()
Dim readLastCell As Long
Dim readLastCellNameSheet As Long
Dim billNumber
Dim SheetName As String
Dim billNumberNamesheet As Long
Dim ExecutiveWorkBookPath As String
Dim excelFilePath
Dim ExecutiveWorkBook As Workbook
Dim MainTemplate As String
MainTemplate = ThisWorkbook.Name
ThisWorkbook.Sheets("Master").Unprotect "12345+"
ThisWorkbook.Worksheets("Master").Range("R1:AV20000").Locked = False
ThisWorkbook.Worksheets("Master").Range("R4:AV20000").Value = ""
'ChDir Defaulth path
excelFilePath = Application.ActiveWorkbook.Path
Application.EnableEvents = False
strFilename = Dir(excelFilePath & "\*xlsm")
Do While strFilename <> ""
'Set variable equal to opened workbook
If InStr(strFilename, "Executive") > 0 Then
Set ExecutiveWorkBook = Workbooks.Open(strFilename, ReadOnly:=True)
ExecutiveWorkBook.Sheets("Summary").Unprotect "12345+"
ExecutiveWorkBook.Worksheets("Summary").Range("A1:Q22000").Locked = False
SheetName = "Sasita"
readLastCell = ThisWorkbook.Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Row + 1000
readLastCellNameSheet = ExecutiveWorkBook.Sheets("Summary").Cells(Rows.Count, 1).End(xlUp).Row + 1000
For x = 4 To readLastCellNameSheet
cell = "A" & x
billNumber = ThisWorkbook.Worksheets("Master").Range(cell).Value
If Len(billNumber) = 0 Then Exit For
For N = 4 To readLastCellNameSheet
cell = "A" & N
billNumberNamesheet = ExecutiveWorkBook.Worksheets("Summary").Range(cell).Value
If Len(billNumberNamesheet) = 0 Then Exit For
If billNumberNamesheet = billNumber Then
cell = "R" & N & ":" & "AV" & N
copycell = "R" & x & ":" & "AV" & x
ExecutiveWorkBook.Worksheets("Summary").Range(cell).Copy
ThisWorkbook.Worksheets("Master").Range(copycell).PasteSpecial Paste:=xlPasteAll
End If
Next N
Next x
Else
End If
ExecutiveWorkBook.Worksheets("Summary").Range("A1:Q22000").Locked = True
ExecutiveWorkBook.Worksheets("Summary").Protect "12345+", True, True
' CLOSE THE SOURCE FILE.
ExecutiveWorkBook.Close ' FALSE - DON'T SAVE THE SOURCE FILE.
'to get next file name
strFilename = Dir
Loop
Application.EnableEvents = True
'MsgBox "Updated Succesully"
End Sub
*编辑 现在我已经完成了第一部分工作,第二部分就出现了。即使我已经定义了我的工作路径并将我的执行工作簿命名为Executive,但我收到的错误是他们无法找到我的文件。
Set ExecutiveWorkBook = Workbooks.Open(strFilename, ReadOnly:=True)
工作簿路径定义有什么问题吗?