2017-02-15 117 views
0

有人可以指出这段代码有什么问题吗?每当指定范围(A1:B6)中的值发生更改时,Excel都会退出Microsoft Error Reporting对话框。 我不能在Excel首选项中取消选中'错误检查(打开背景错误检查)'。Excel退出Worksheet_Change事件

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 
    Set KeyCells = Range("A1:B6") 

    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then 
     Call Macro1 
     MsgBox "Test" 
    End If 
End Sub 

宏1:

Sub Macro1() 

    Dim wb As Workbook 
    Dim wsData As Worksheet 
    Dim wsDest As Worksheet 
    Dim rInterestCell As Range 
    Dim rDest As Range 

    Set wb = ActiveWorkbook 
    Set wsData = wb.Sheets("Sheet1") 
    Set wsDest = wb.Sheets("Formula Results") 

    For Each rInterestCell In Range("Interest_Range").Cells 
     wsData.Range("A7").Value = rInterestCell.Value 
     wsData.Calculate  
     Set rDest = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1) 
     If rDest.Row < 6 Then Set rDest = wsDest.Range("A6") 
     rDest.Value = wsData.Range("A6").Value 
    Next rInterestCell 

End Sub 

第二个宏

Sub Macro2() 
Dim FLrange As Range 
Set FLrange = Range(“Initial_Rate”) 

For Each cell In FLrange 
cell.Offset(0, 5).Formula = "=SUM(B3/100*A7)” 

Next cell 
End Sub 
+2

你有没有做,因为它会询问并打开后台错误检查?这个问题很可能出现在你的Macro1中,你有没有通过F8来看看它退出了哪一行? –

+0

尝试在第一个添加'Application.EnableEvents = False',并在末尾添加'Application.EnableEvents = True'。 – Fadi

+0

为什么你有'Range(Target.Address)',只需使用'Target'。不需要'调用Macro1'来写'Macro1'。对于调试部分,在'Macro1'之前写入'MsgBox“开始”',之后写入'MsgBox“Finish”'。通过这种方式,您将知道错误来自哪里 –

回答

1

你最好在Macro1做了许多计算之前关闭事件与Application.EnableEvents = False

如果一切正常,只是发表评论MsgBox "Before Macro1"MsgBox "After Macro1"

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim KeyCells As Range 
    Set KeyCells = Me.Range("A1:B6") 

    If Not Application.Intersect(KeyCells, Target) Is Nothing Then 
     MsgBox "Before Macro1" 
     Macro1 
     MsgBox "After Macro1" 
    End If 
End Sub 

宏1:

Sub Macro1() 
    Dim wB As Workbook 
    Dim wsData As Worksheet 
    Dim wsDest As Worksheet 
    Dim rInterestCell As Range 
    Dim rDest As Range 

    Set wB = ActiveWorkbook 
    Set wsData = wB.Sheets("Sheet1") 
    Set wsDest = wB.Sheets("Formula Results") 

    Application.EnableEvents = False 

    For Each rInterestCell In Range("Interest_Range").Cells 
     wsData.Range("A7").Value = rInterestCell.Value 
     wsData.Calculate 
     DoEvents 
     Set rDest = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1) 
     If rDest.Row < 6 Then Set rDest = wsDest.Range("A6") 
     rDest.Value = wsData.Range("A6").Value 
    Next rInterestCell 

    Application.EnableEvents = True 
End Sub 
+0

不幸的是,这并没有帮助。它可能是我的第二个宏吗?它很好地工作了几次,然后我在指定的目标范围下更改了单元格值,并导致Excel崩溃(意外退出)。 –

+0

@ zafira.404:可能是因为'Worksheet_Change'非常整齐。你有错误还是Excel刚崩溃?是在显示Macro1消息之前?显示“Macro1”消息后? – R3uK

+0

它没有显示任何MsgBox',只是退出'有问题,Microsoft Excel已关闭。崩溃线程 ‘EXC_BAD_INSTRUCTION 和 错误签名::: 例外’:我们为“与‘更多信息’与以下日志按钮的不便表示歉意0 未捕获ObjC例外,原因是: - [NSAlert runModal]可能不在事务提交中调用(通常这意味着它在视图的-drawRect:方法内部调用)。“ –