Excel 运行时错误“424”:VBA复制/粘贴宏和取消工作簿保护需要对象

Excel 运行时错误“424”:VBA复制/粘贴宏和取消工作簿保护需要对象,excel,vba,Excel,Vba,我已经创建了一个宏,可以根据需要进行复制/粘贴。但是,我最近意识到我需要取消隐藏所需的工作表,并使用VBA取消工作簿的保护。整个工作簿需要取消密码保护,而不仅仅是一张工作表。当我使用工作簿运行代码时。取消保护密码:=SOCKS,我得到一个运行时错误“424” Sub CopyRange() Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Dim i As

我已经创建了一个宏,可以根据需要进行复制/粘贴。但是,我最近意识到我需要取消隐藏所需的工作表,并使用VBA取消工作簿的保护。整个工作簿需要取消密码保护,而不仅仅是一张工作表。当我使用工作簿运行代码时。取消保护密码:=SOCKS,我得到一个运行时错误“424”

Sub CopyRange()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Dim i As Integer
    Dim wkbDest As Workbook
    Dim wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Dim strExtension As String
    Dim LastRow As Long
    Dim a As Integer

    Const strPath As String = "H:\My Documents\Timesheet Folder\" 'Specify file path here next to the = in the " "
    ChDir strPath
    strExtension = Dir(strPath & "*.xls*")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource.Sheets("Data") 'Specify the name of the sheet here in the " " next to .Sheets
            a = .Cells(.Rows.Count, 1).End(xlUp).Row
            Worksheets("Data").Visible = True
            Workbook.Unprotect Password:="SOCKS" 'Here is the problem line
            For i = 1 To a
                If .Cells(i, 1).Value = "1934001" And .Cells(i, 2).Value = "GSMP_North_Haledon" And .Cells(i, 4).Value = "2019" And .Cells(i, 5).Value = "September" And .Cells(i, 6).Value = "20" And .Cells(i, 17).Value = "SRo" Then
                    'In the line below, name the sheet of the open destination workbook in the " " next to .Worksheets
                    LastRow = wkbDest.Worksheets("Data").Cells(wkbDest.Worksheets("Data").Rows.Count, "B").End(xlUp).Offset(1).Row
                    Worksheets("Data").Range(Worksheets("Data").Cells(i, 1), Worksheets("Data").Cells(i, 17)).Copy
                    wkbDest.Worksheets("Data").Range("B" & LastRow).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
                End If
            Next
        End With
        wkbSource.Close savechanges:=False
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

End Sub

该行未正确限定,请尝试以下操作:

wkbSource.Unprotect Password:="SOCKS"

您需要将工作簿替换为要取消保护的实际工作簿对象。我假设它的wbkSource。

工作簿本身在您的上下文中没有任何意义,您没有将它设置为特定对象。我想你的意思是写wkbSource。取消保护。。。或者因为您已经在With子句中设置了With,所以只需编写。Unprotect应该是wkbSource。Unprotect Password:=SOCKS。@jamheadart With块引用特定的工作表数据,因此。Unprotect将不适用于整个工作簿。@M.Schalk哦,是的,我没有看到它附加了额外的对象。。