循环遍历每个打开的WB中的每个WS并取消保护

时间:2016-07-01 16:37:15

标签: excel vba excel-vba macros

我需要完成一项涉及从大量工作簿中整理数据的任务,我将它们全部放在同一个文件夹中,并且都是* xlsm文件。 我已经有一个宏来从每个工作表中获取数据并将其全部输入到主工作表中,就像我之前所做的那样。

现在每个工作表现在都受密码保护以限制用户,我需要:

- 打开每个工作簿 - 遍历每张表并用密码解锁(“我有哪个”)

目前,下面的代码打开了每个工作簿并解锁了当前处于活动状态但未遍历每个打开的工作簿的工作簿。

Sub OpenFiles()
Dim MyFolder As String
Dim MyFile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim pwd As String

pwd = "password"

'_______Opens every excel file in folder_______

MyFolder = "Test"

MyFile = Dir(MyFolder & "\*.xlsm")
Do While MyFile <> ""
Workbooks.Open Filename:=MyFolder & "\" & MyFile
MyFile = Dir
Loop

For Each wb In Application.Workbooks
    For Each ws In Worksheets
      ws.Unprotect Password:=pwd
    Next ws
Next wb

End Sub

谢谢

1 个答案:

答案 0 :(得分:3)

这是你在找什么?

Option Explicit

Public Sub tmpSO()

Dim wb As Workbook
Dim ws As Worksheet

For Each wb In Application.Workbooks
    For Each ws In wb.Worksheets
        ws.Unprotect "Which I have"
    Next ws
Next wb

End Sub