gpt4 book ai didi

excel - 运行时错误 '424' : Object required for VBA copy/paste macro with unprotect workbook

转载 作者:行者123 更新时间:2023-12-03 01:24:47 25 4
gpt4 key购买 nike

我创建了一个宏来根据需要复制/粘贴。然而,我最近意识到我需要取消隐藏我需要的工作表并使用 VBA 取消对我的工作簿的保护。整个工作簿都需要不受密码保护,而不仅仅是一张工作表。当我使用 Workbook.Unprotect Password:="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"

关于excel - 运行时错误 '424' : Object required for VBA copy/paste macro with unprotect workbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58167735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com