工作表更改事件VBA代码,它崩溃excel 2013

时间:2013-07-03 00:31:35

标签: vba

在人们帮我弄清楚如何解决第一个问题之后,我开了一个新问题,现在我遇到了一个新问题。您可以在下面的“INITIATING DEVICES”页面找到编码。代码的倒数第二部分的想法是从B7:b和E7:E添加列(文本值)并将其显示在列J7:J上。因此,如果将Photo输入B并将Pass输入E列,那么结果将是第J列中的Photopass。如果通过宏命令运行,原始代码可以正常工作。问题是我试图将它添加到我现有的一些代码中,现在当信息输入任何单元格时整个程序都会崩溃。程序无响应,然后关闭并重新启动。我没有得到代码或调试消息。这是页面上的整个代码。

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Column = 7 And UCase(Target.Value) = "YES" Then
    Sheets("MESSAGE CHANGES").Cells(Rows.Count, 1).End(xlUp)(2).Resize(, 3) = Sheets("INITIATING DEVICES").Cells(Target.Row, 1).Resize(, 3).Value
    Application.Goto Sheets("MESSAGE CHANGES").Cells(Rows.Count, 1).End(xlUp).Offset(, 3)

End If


 If Target.Column = 6 And UCase(Target.Value) = "YES" Then
    Sheets("DEVICE NOTES").Cells(Rows.Count, 1).End(xlUp)(2).Resize(, 3) = Sheets("INITIATING DEVICES").Cells(Target.Row, 1).Resize(, 3).Value
    Application.Goto Sheets("DEVICE NOTES").Cells(Rows.Count, 1).End(xlUp).Offset(, 3)

End If


 '(replace if new code fails)If Target.Column = 5 And UCase(Target.Value) = "FAIL" Or Target.Column = 5 And UCase(Target.Value) = "DAMAGED" Then
    '(replace if new codes fails)Sheets("FAILED DEVICES").Cells(Rows.Count, 1).End(xlUp)(2).Resize(, 3) = Sheets("INITIATING DEVICES").Cells(Target.Row, 1).Resize(, 3).Value
 If Target.Column = 5 And UCase(Target.Value) = "FAIL" Or UCase(Target.Value) = "DAMAGED" Then
    Application.EnableEvents = False
    Sheets("FAILED DEVICES").Cells(Rows.Count, 1).End(xlUp)(2).Resize(, 5) = Sheets("INITIATING DEVICES").Cells(Target.Row, 1).Resize(, 5).Value
    Sheets("FAILED DEVICES").Cells(Rows.Count, 1).End(xlUp).Offset(, 5) = Sheets("INITIATING DEVICES").Cells(Target.Row, 11).Value
    Application.EnableEvents = True


End If
'code that will place date/time when value is selcted in E
If Not Intersect(Target, Range("E:E")) Is Nothing Then
   Range("I" & Target.Row).Value = Now

End If

Dim wb As Workbook
Dim ws As Worksheet
Dim lastRow As Long

Set wb = ThisWorkbook
Set ws = wb.Sheets("INITIATING DEVICES")

lastRow = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
ws.Range("J7:J" & lastRow).Value = Evaluate("=B7:B" & lastRow & "&E7:E" & lastRow)


End Sub


Private Sub Workbook_BeforePrint(Cancel As Boolean)
With Sheets("INITIATING DEVICES")
    .PageSetup.PrintArea = .Range("A1:H" & .Cells(Rows.Count, 1).End(xlUp).Row).Address
End With

End Sub

感谢您提供的任何帮助,如果您需要更多信息,我可以发送检查文件,并在需要时提供更多信息。我确定我搞砸了一些东西,这就是为什么它一直在崩溃。还在这里学习。

1 个答案:

答案 0 :(得分:0)

如果它崩溃,则重复调用Change事件。你需要使用

Application.EnableEvents = False

在适当的时候阻止更改事件再次被触发,因为您对工作表进行了更改(使用您的代码)。