如何将两个潜艇与专用潜艇Worksheet_Change合并到图纸上

时间:2019-04-17 06:58:42

标签: excel vba

早上好 我需要结合两个Private Sub Worksheet_Change(ByVal Target As Range)我是Excel VBA代码的新手,我该怎么做?下面的代码。

1)

Option Explicit
Const strAFM As String = "D3:D1000"
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Rng As Range, AFM As String, rngTomi As Range
    Set Rng = Range(strAFM)
    Set rngTomi = Intersect(Target, Rng)
    If rngTomi Is Nothing Then Exit Sub

    If rngTomi.Count <> 1 Then
        rngTomi.ClearContents
        Exit Sub
    End If
    If Trim(Target.Value) = "" Then Exit Sub

    AFM = Right("000000000" & Target.Value, 9)

    If isAFM(AFM) = False Then
        MsgBox "no afm"
        Target.Activate
        Exit Sub
    End If
End Sub

2)

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub

    Dim Rng As Range
    Set Rng = Me.Range("ColTarget")

    If Intersect(Target, Rng) Is Nothing Then Exit Sub

    ResizeTbl
End Sub

1 个答案:

答案 0 :(得分:0)

尝试:

Option Explicit
Const strAFM As String = "D3:D1000"
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim Rng As Range, AFM As String, rngTomi As Range

    If Not Target.Count > 1 Then
        Set Rng = Me.Range("ColTarget")
        If Not Intersect(Target, Rng) Is Nothing Then ResizeTbl
    End If

    Set Rng = Range(strAFM)
    Set rngTomi = Intersect(Target, Rng)
    If Not rngTomi Is Nothing Then
        If rngTomi.Count <> 1 Then
            Application.EnableEvents = False
                rngTomi.ClearContents
            Application.EnableEvents = False
            Exit Sub
        End If

    If Trim(Target.Value) = "" Then Exit Sub

    AFM = Right("000000000" & Target.Value, 9)

    If isAFM(AFM) = False Then
        MsgBox "no afm"
        Target.Activate
        Exit Sub
    End If
    End If

End Sub