Vba 在一个工作表中有多个工作表\u更改

Vba 在一个工作表中有多个工作表\u更改,vba,excel,excel-formula,Vba,Excel,Excel Formula,我希望在单元格范围内将工作簿用户限制为1000个字符(示例:A5:A30) 换句话说,将A5:A30范围内的字符总数限制为1000个字符 当用户填写发送范围超过1000个字符限制的单元格时,它将调用Application.undo,这将删除他们添加的最后一个文本 但是,由于我在工作表上有另一个私有子工作表\u更改(ByVal target As Range),因此会导致错误 下面是两个工作表和更改子项。两者使用相同的单元格 Private Sub Worksheet_Change(ByVal T

我希望在单元格范围内将工作簿用户限制为1000个字符
(示例:A5:A30)

换句话说,将A5:A30范围内的字符总数限制为1000个字符

当用户填写发送范围超过1000个字符限制的单元格时,它将调用Application.undo,这将删除他们添加的最后一个文本

但是,由于我在工作表上有另一个
私有子工作表\u更改(ByVal target As Range)
,因此会导致错误

下面是两个工作表和更改子项。两者使用相同的单元格

Private Sub Worksheet_Change(ByVal Target As Range)
 Dim charCount As Long

    If Not Intersect(Target, Range("E6,E11,E16")) Is Nothing Then

        Dim arrValues As Variant
        arrValues = Range("E6,E11,E16").Value2

        Dim i As Long
        Dim tempSplit As Variant
        Dim j As Long

            For i = LBound(arrValues) To UBound(arrValues)
             tempSplit = Split(arrValues(i, 1), " ")

            For j = LBound(tempSplit) To UBound(tempSplit)
                charCount = charCount + Len(tempSplit(j))
            Next j
        Next i

    End If

If charCount > 1000 Then
   Application.Undo
    MsgBox "Adding this exceeds the 1000 character limit"
 End If


            If Not Intersect(Target, Range("D6")) Is Nothing Then

        If Target.Value2 = "Material" Then
        'assumes the comment cell is one column to the right
            Target.Offset(0, 1) = "**"
        End If

    End If

            If Not Intersect(Target, Range("D7")) Is Nothing Then

        If Target.Value2 = "Material" Then
        'assumes the comment cell is one column to the right
            Target.Offset(-1, 1) = "**"
        End If

        End If

       If Not Intersect(Target, Range("D8")) Is Nothing Then

        If Target.Value2 = "Material" Then
           Target.Offset(-2, 1) = "**"
        End If

    End If
End Sub

有没有办法让我在同一张工作表上有两个
工作表更改

一张工作表中不能有两个
工作表更改事件。但是,一个就足够了:

Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case True
    Case Not Intersect(ActiveCell, Range("A5:A30")) Is Nothing
        DoThingOne
    Case Not Intersect(ActiveCell, Range("B5:B30")) Is Nothing
        DoThingTwo
    End Select

End Sub

Private Sub DoThingOne()
    Debug.Print "THING ONE"
End Sub

Private Sub DoThingTwo()
    Debug.Print "THING TWO"
End Sub

用维蒂亚塔的想法修改一下怎么样

Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case True

        Case Not Intersect(Target, Range("E6,E11,E16")) Is Nothing

            Dim charCount As Long

            Dim arrValues As Variant
            arrValues = Range("E6,E11,E16").Value2

            Dim i As Long
            Dim tempSplit As Variant
            Dim j As Long

            For i = LBound(arrValues) To UBound(arrValues)
                tempSplit = Split(arrValues(i, 1), " ")

                For j = LBound(tempSplit) To UBound(tempSplit)
                    charCount = charCount + Len(tempSplit(j))
                Next j
            Next i

            If charCount > 1000 Then
                With Application
                    .EnableEvents = False
                    .Undo
                    .EnableEvents = True
                End With

                MsgBox "Adding this exceeds the 1000 character limit"
            End If

        Case Not Intersect(Target, Range("D6")) Is Nothing

            If Target.Value2 = "Material" Then
                'assumes the comment cell is one column to the right
                Target.Offset(0, 1) = "**"
            End If

        Case Not Intersect(Target, Range("D7")) Is Nothing

            If Target.Value2 = "Material" Then
                'assumes the comment cell is one column to the right
                Target.Offset(-1, 1) = "**"
            End If

        Case Not Intersect(Target, Range("D8")) Is Nothing

            If Target.Value2 = "Material" Then
                Target.Offset(-2, 1) = "**"
            End If

    End Select

End Sub

如果您粘贴两个代码集,我们可能只需将代码放在同一个
工作表\u更改中
,您想对第二个工作表\u更改做什么?@RicardoA the second,是根据同一工作表上下拉列表的选择,将一个字符放置在特定单元格中。能否将此注释移到代码来源的问题上?它不完全属于这里
工作表\u Change
是一个事件处理程序。它处理
工作表
界面的
Change
事件,只有当它具有确切的特定名称(包括下划线)时,它才会工作(给定的过程名称在模块中只能存在一次)。如果您“需要两个处理程序”,您需要编写两个不同的
Sub
过程,并使您的处理程序有条件地分支到(/invoke)中,或者使用@Vitaya的
Select Case
语句,或者使用
If…Else If…End If
块,不管怎样。只要不要做一个10K线性事件处理程序,你就会做得很好。我认为这个答案将更有价值,用下面的
DoThingOne
DoThingTwo
代替
Debug.Print
语句和
Private Sub-DoThingTwo
来代替
Private Sub-DoThingTwo
,这两个选项已经是经过了升级的代码行“For i=LBound(arrValues)To UBound(arrValues)”在我使用下拉列表或类型时显示运行时错误13“类型不匹配”,然后单击单元格外。@Tmacjoshua如果我们将该
包围在i中,我可能会在这里欺骗系统。……下一个i
如果是isArray(arrValues),那么…(对于i)…如果
Ok,则结束,这样错误就不会发生,下拉列表将**放在单元格中,但1000个字符的限制似乎不起作用。@Tmacjoshua我不确定,这只是概念化:/