我想使用相同的密碼取消保護指定路徑中的 excel 作業簿。我已經使用下面的代碼來完成這個程序。
Sub Unlock_Refresh()
Dim path As String, w As Worksheet, pass As String, wb As Workbooks
pass = "1519"
Worksheets("Sheet2").Select
path = Worksheets("Sheet2").Range("A1").Value
For Each wb In path
wb.Unprotect Password:=pass
Next wb
ThisWorkbook.RefreshAll
Application.Wait ("00:00:10")
For Each wb In path
wb.Protect Password:=pass
Next wb
End Sub
它不會作業。有人可以幫我解決這個問題。
uj5u.com熱心網友回復:
Application.Wait ("00:00:10")暫停宏直到午夜過后 10 秒,我猜這不是你想要的。使用Dir遍歷目錄中的檔案,打開作業簿,重繪 ,關閉和保存。
Sub Unlock_Refresh()
Dim wb As Workbook, ws As Worksheet
Dim Filepath As String, Filename As String
Dim n As Long
Dim books As New Collection
Const pass = "1519"
Filepath = Worksheets("Sheet2").Range("A1").Value
If Right(Filepath, 1) <> "\" Then Filepath = Filepath & "\"
Filename = Dir(Filepath & "*.xls*")
Application.ScreenUpdating = False
Do While Filename <> ""
n = n 1
Set wb = Workbooks.Open(Filepath & Filename, Password:=pass)
books.Add wb, CStr(n)
Filename = Dir
Loop
ThisWorkbook.RefreshAll
Application.Wait Now TimeValue("00:00:10")
' close books
For Each wb In books
wb.Close SaveChanges:=False
Next
Application.ScreenUpdating = True
MsgBox n & " workbooks opened and refreshed " & vbLf & Filepath, vbInformation
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351350.html
