有人可以指出這段代碼有什麼問題嗎?每當指定範圍(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
你有沒有做,因爲它會詢問並打開後臺錯誤檢查?這個問題很可能出現在你的Macro1中,你有沒有通過F8來看看它退出了哪一行? –
嘗試在第一個添加'Application.EnableEvents = False',並在末尾添加'Application.EnableEvents = True'。 – Fadi
爲什麼你有'Range(Target.Address)',只需使用'Target'。不需要'調用Macro1'來寫'Macro1'。對於調試部分,在'Macro1'之前寫入'MsgBox「開始」',之後寫入'MsgBox「Finish」'。通過這種方式,您將知道錯誤來自哪裏 –