0

嗨,我一直在分配给同一工作簿中多张工作表上的表单按钮的宏中使用以下代码

Sub RunAll()
Dim wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
wks.Select
Application.ScreenUpdating = False
Call UnprotectAll
Call BasicLoop
Call ProtectAll
Next wks
Application.ScreenUpdating = True
End Sub

这是它调用的宏

Sub UnprotectAll()
Dim sh As Worksheet
Dim yourPassword As String
yourPassword = ""

For Each sh In ActiveWorkbook.Worksheets
    sh.Unprotect Password:=yourPassword
Next sh

End Sub


Sub BasicLoop()
For x = 5 To 32
If Cells(x, 9) <> red Then
Cells(x, 10) = "Basic"
Else: Cells(x, 10) = ""
End If

Next x
End Sub

Sub ProtectAll()
Dim sh As Worksheet
Dim yourPassword As String
yourPassword = ""

For Each sh In ActiveWorkbook.Worksheets
    sh.Protect Password:=yourPassword
Next sh

End Sub

我要更改的是单击表单按钮时仅更新活动工作表,而不是工作簿中的所有工作表?我仍在学习,所以一个简单的解决方案将是最好的欢呼!

4

2 回答 2

0

考虑:

Sub RunAll()
    Application.ScreenUpdating = False
    Call UnprotectAll
    Call BasicLoop
    Call ProtectAll
    Application.ScreenUpdating = True
End Sub


Sub UnprotectAll()
    Dim sh As Worksheet
    Dim yourPassword As String
    yourPassword = ""
    Set sh = ActiveSheet
    sh.Unprotect Password:=yourPassword
End Sub


Sub BasicLoop()
    For x = 5 To 32
    If Cells(x, 9) <> red Then
        Cells(x, 10) = "Basic"
    Else
        Cells(x, 10) = ""
    End If
Next x
End Sub

Sub ProtectAll()
    Dim sh As Worksheet
    Dim yourPassword As String
    yourPassword = ""
    Set sh = ActiveSheet
    sh.Protect Password:=yourPassword
End Sub
于 2013-10-26T17:06:35.657 回答
0

您需要做的就是删除贯穿所有工作表的循环。您的代码应如下所示:

Sub RunAll()
    Application.ScreenUpdating = False
    Call UnprotectAll
    Call BasicLoop
    Call ProtectAll
    Application.ScreenUpdating = True
End Sub


Sub UnprotectAll()
    Dim yourPassword As String
    yourPassword = ""

    ActiveSheet.Unprotect Password:=yourPassword
End Sub


Sub BasicLoop()
    For x = 5 To 32
        If Cells(x, 9) <> red Then
            Cells(x, 10) = "Basic"
        Else
            Cells(x, 10) = ""
        End If
    Next x
End Sub

Sub ProtectAll()
    Dim yourPassword As String
    yourPassword = ""

    ActiveSheet.Protect Password:=yourPassword
End Sub

这只会在活动工作表上执行。

于 2013-10-26T17:05:23.667 回答