我试图在除工作簿中的一个工作表之外的所有工作表中运行此代码

时间:2016-08-26 11:58:15

标签: vba excel-vba excel

Sub Worksheet_SelectionChange(ByVal ws As Range)

        Dim rInt As Range
        Dim rCell As Range

        Set rInt = Intersect(Target, Range("B1:B32, B37:B45, K3:K11, K12:K18"))
        If Not rInt Is Nothing Then
            For Each rCell In rInt
                rCell.Value = "1"
            Next
        End If
        Set rInt = Nothing
        Set rCell = Nothing


End Sub

2 个答案:

答案 0 :(得分:3)

将代码放在您拥有的所有工作表的工作表代码区域中,除了您要避免的一张工作表。

enter image description here

答案 1 :(得分:0)

您可以使用Workbook级事件(代码进入ThisWorkbook模块)

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

    Const NOT_THIS_ONE As String = "Tester" '<< sheet to skip
    Dim rInt As Range
    Dim rCell As Range

    If Sh.Name = NOT_THIS_ONE Then Exit Sub

    Set rInt = Intersect(Target, Range("B1:B32, B37:B45, K3:K11, K12:K18"))
    If Not rInt Is Nothing Then
        For Each rCell In rInt
            rCell.Value = "1"
        Next
    End If

    Set rInt = Nothing
    Set rCell = Nothing

End Sub