excel Worksheet_Change(ByVal Target As Range)的代码执行已中断错误

sauutmhj  于 2023-04-07  发布在  其他
关注(0)|答案(1)|浏览(224)

我写了一个vba代码,如果某些单元格的值发生变化,excel将运行现有的宏。然而,我一直得到一个“代码执行已被中断”错误几乎80%的时间,它运行。当我点击调试并按下继续,一切正常,所以我知道我的代码没有任何问题。有人可以帮助我解决这个问题吗?
下面是我的代码:

Private Sub WeldCalc_Click()

Range("C428").Select
ActiveCell.FormulaR1C1 = "10"
Range("R502").GoalSeek Goal:=0, ChangingCell:=Range("C428")
ActiveWindow.ScrollRow = 1

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("C34") = "DWB" Then

If Target.Address = "$C$36" Or Target.Address = "$C$39" Or Target.Address = "$C$40" Or Target.Address = "$C$41" Or Target.Address = "$C$43" Or Target.Address = "$C$44" Then
    WeldCalc_Click
End If
    
End If 

End Sub

我甚至尝试了幻影断点方法,你按ctrl暂停两次,但它不起作用。

polhcujo

polhcujo1#

工作表变更:调用另一个过程来处理工作表

Private Sub WeldCalc_Click()
    WeldCalc Me
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const TGT_RANGE As String = "C36,C39,C40,C41,C43,C44"
    
    On Error GoTo ClearError
    
    Dim IsReady As Boolean
    
    If Intersect(Target, Me.Range(TGT_RANGE)) Is Nothing Then Exit Sub
    If CStr(Me.Range("C34").Value) <> "DWB" Then Exit Sub
    
    Application.EnableEvents = False
    WeldCalc Me
    
ProcExit:
    On Error Resume Next
        If Not Application.EnableEvents Then Application.EnableEvents = True
    On Error GoTo 0
Exit Sub
ClearError:
    MsgBox "Run-time error '" & Err.Number & "':" & vbLf & vbLf _
        & Err.Description
    Resume ProcExit
End Sub

Sub WeldCalc(ByVal ws As Worksheet)
    ws.Range("C428").FormulaR1C1 = "10"
    ws.Range("R502").GoalSeek Goal:=0, ChangingCell:=ws.Range("C428")
End Sub

相关问题