Excel VBA代码不';当我自动调用它时,它不会运行

Excel VBA代码不';当我自动调用它时,它不会运行,excel,vba,Excel,Vba,我有一个名为Splittext的宏,当工作表macro Process的单元格“B4”发生更改时调用该宏。调用该宏时它不起作用,但手动运行时它起作用。代码中没有错误 Sub splitText() Dim wsS1 As Worksheet 'Sheet1 Dim textstring As String, warray() As String, counter As Integer, strg As String Set wsS1 = Sheets("OUTPUT 1

我有一个名为
Splittext
的宏,当工作表
macro Process的单元格“B4”发生更改时调用该宏。调用该宏时它不起作用,但手动运行时它起作用。代码中没有错误

Sub splitText()
    Dim wsS1 As Worksheet 'Sheet1
    Dim textstring As String, warray() As String, counter As Integer, strg As String

    Set wsS1 = Sheets("OUTPUT 1")
    wsS1.Activate

    textstring = Range("A2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 1).Value = Trim(strg)
    Next counter

    textstring = Range("B2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 2).Value = Trim(strg)  
    Next counter

    textstring = Range("C2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 3).Value = Trim(strg)
    Next counter
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Set Target = Range("B4")
    If Target.Value = "Completed" Then
        Call splitText
    End If
End Sub
此代码用于分隔工作表“输出1”的单元格(“A2”)(“B2”)(“C2”)中的文本

这就是我调用代码的方式

Sub splitText()
    Dim wsS1 As Worksheet 'Sheet1
    Dim textstring As String, warray() As String, counter As Integer, strg As String

    Set wsS1 = Sheets("OUTPUT 1")
    wsS1.Activate

    textstring = Range("A2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 1).Value = Trim(strg)
    Next counter

    textstring = Range("B2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 2).Value = Trim(strg)  
    Next counter

    textstring = Range("C2").Value
    warray() = Split(textstring, ">")

    For counter = LBound(warray) To UBound(warray)
        strg = warray(counter)
        Cells(counter + 3, 3).Value = Trim(strg)
    Next counter
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Set Target = Range("B4")
    If Target.Value = "Completed" Then
        Call splitText
    End If
End Sub

不清楚您正在监控哪张表的更改,但这对我很有用:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Set Target = sh.Range("B4")
    If Target.Value = "Completed" Then
        Application.EnableEvents = False
        splitText
        Application.EnableEvents = True
    End If
End Sub


Sub splitText()
    Dim warray() As String, i As Long, c As Range
    For Each c In ThisWorkbook.Sheets("OUTPUT 1").Range("A2:C2").Cells
        warray = Split(c.Value, ">")
        For i = LBound(warray) To UBound(warray)
            c.Offset(i + 1, 0).Value = Trim(warray(i))
        Next i
    Next c
End Sub

您到底是如何自动调用的?@TimWilliams请检查我用我用来调用宏的代码更新的问题您的代码中没有一个引用特定的工作表-一个好的开始是限定所有范围/单元格引用,以便它们引用正确的工作表。您正在监视哪张表的更改?所有这些?FWIW你的代码基本上都是在顶部重复的,我认为,在1到3之间的另一个外循环中,可以将内循环减少到一个块。对于i=1到3:textstring=Cells(2,i)。Value:Cells(counter+3,i)'您的意思是我需要定义一个工作表,代码需要在该工作表上运行@TimWilliams最后…谢谢。。。成功了…非常感谢你的耐心和帮助!Tim,我知道是在OP中-但是为什么要更改
表单
,然后将
目标
设置为一个可能不是目标的固定地址?我真的很惊讶你没有捡到这个!(相反:
If Target.Address=“B4”
等)。我确实捡到了它,但有一些潜在的原因可以解释为什么会如此……Tim,来自OP“当工作表的单元格“B4”发生更改时调用它”-因此事件处理程序应该检查是否是更改的单元格-而不是强制问题。我唯一能想到的是,如果B4是通过公式改变的,那么必须检查其他一些标准,看看是否真的有变化。只要B4=completed,当前的代码将在任何工作表中每次发生更改时运行。代码上方的注释提示OP,他们需要做一些工作才能使其正确。B4就在被分裂的细胞下面,这个过程很可能会覆盖B4中的任何东西。。。