請我有一個問題,每次在作業表上發生更改時,它都會影響所有行,而不是相關的行 (i)。使困惑。for 回圈不適用于 worksheet_change 嗎?請幫忙。謝謝。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long
'create a variable for last row of column C, LR
LR = Cells(Rows.Count, "C").End(xlUp).Row
For i = 2 To LR
If Cells(i, 6) = "Yes" And Cells(i, 7).Value = "Full" Then
Target.Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value Cells(i, 9).Value
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes"
And Target.Value = "Full" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 9).ClearContents
Cells(i, 10).Value = Cells(i, 8).Value Cells(i, 9).Value
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("G" & i & ":G" & LR)) Is Nothing And Range("F" & i) = "Yes" And
Target.Value = "Portion" Then
Application.EnableEvents = False
Cells(i, 8).Value = Cells(i, 3).Value
Cells(i, 10).Value = Cells(i, 8).Value Cells(i, 9).Value
Application.EnableEvents = True
End If
Next i
End Sub
uj5u.com熱心網友回復:
看來您需要為列 AE 啟動此事件。因此,您可以使用以下命令啟動宏:
IF Target.Column <= 5 THEN
...
END IF 'at the end of your macro
像這樣,當你啟動像Cells(i, 8).Value = ..., Cells(i, 10).Value = ..., ... 這樣的代碼時,這個宏將被呼叫,但它會立即停止。
顯然,您正在檢查最多 10 列,該列位于您在宏中更改的單元格范圍內。讓我們采用另一種方法:
在宏的最開始,放置以下行:
Application.EnableEvents = False
在宏的最后,放置以下行:
Application.EnableEvents = True
(并洗掉其他事件)。
這將確保您在運行時不會呼叫宏。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327045.html
下一篇:使用do回圈直到輸入數字的總和
