我有一个问题,我的VBA代码,它不是解除保护我的工作簿与一些工作表是隐藏的。一个假设,我认为,是取消隐藏所有工作表,取消保护工作簿,并在此之后取消隐藏所有工作表,最初隐藏。
我已经试着建立了一个代码,但隐藏所有工作表的步骤,最初隐藏它的不工作!
拜托你们能不能帮我解释一下为什么它不起作用!?
Sub UnlockSheets()
' Define the password to unlock the tabs
Dim password As String
password = InputBox("Enter the password")
' Collection to store the original visibility state of the tabs
Dim originalState As New Collection
' Reference to the currently active worksheet
Dim activeSheet As Worksheet
Set activeSheet = ActiveSheet
' Loop through all the tabs in the workbook
For Each ws In ThisWorkbook.Worksheets
' Check if the tab is protected with a password
If ws.ProtectContents Then
' Store the original visibility state of the tab
originalState.Add ws.Visible, ws.Name
' Show the tab before unprotecting it (only if it was hidden)
If ws.Visible = xlSheetHidden Or ws.Visible = xlSheetVeryHidden Then
ws.Visible = xlSheetVisible
End If
' Activate the tab before unprotecting it
ws.Activate
' Unprotect the tab using the provided password
On Error Resume Next
ws.Unprotect Password:=password
On Error GoTo 0
ActiveWorkbook.Unprotect Password:=password
End If
Next ws
' Return to the originally active worksheet
activeSheet.Activate
' Hide the tabs again (if they were originally hidden)
Dim tab As Variant
For Each tab In originalState
On Error Resume Next
If originalState(tab) = xlSheetHidden Or originalState(tab) = xlSheetVeryHidden Then
Dim newTabName As String
newTabName = Replace(tab, oldTabName, newTabName)
ThisWorkbook.Worksheets(newTabName).Visible = originalState(tab)
End If
On Error GoTo 0
Next tab
End sub
字符串
我还有另一个疑问,我有这个相同的代码appllied一个按钮。当我从按钮上播放宏时,它在wb行上返回错误1004。当我从代码本身播放时,它工作了!这有什么逻辑可言!?
1条答案
按热度按时间wdebmtf21#
取消保护工作簿和工作表
*****
指定的行,并隐藏工作表,以证明我错了。字符串