将宏窗体按钮操作从工作簿更改为活动工作表

时间:2013-10-26 16:55:19

标签: excel vba excel-vba

您好我在分配给同一工作簿中多个工作表上的表单按钮的宏中使用了以下代码

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

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

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分: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
相关问题