如何在工作表上将两个子与 Private Sub Worksheet_Change 合并

Posted

技术标签:

【中文标题】如何在工作表上将两个子与 Private Sub Worksheet_Change 合并【英文标题】:How to merge two subs with Private Sub Worksheet_Change on sheet 【发布时间】:2019-09-07 08:27:59 【问题描述】:

早上好 我需要合并两个 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

【问题讨论】:

我只看到一个子 抱歉,您刚刚发布了包含大量 IF 语句的代码墙。什么是合并?这两个程序在哪里? 抱歉我的错误,我对最初的约会进行了更正。谢谢 你有没有尝试过? 【参考方案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

    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

【讨论】:

您好,感谢您的回答,她工作得很好……很好

以上是关于如何在工作表上将两个子与 Private Sub Worksheet_Change 合并的主要内容,如果未能解决你的问题,请参考以下文章

Excel如何实现两个工作表数据的对比

MongoDB关系子与父

如何在一张工作表上运行 2 个 Private Sub Worksheet_Change?

Python:在多张工作表上将 Pandas DataFrame 写入 Excel 的最快方法

如何在 HsqlDB 上将 MEMORY 表转换为 CACHED 表?

如何在 iOS 6 上将多个 URL 共享到 Facebook