2017-02-15 48 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:方法內部調用)。「 –