0
我創建了一個工作表更改事件,如果列I被更改(添加了日期),它會將其插入到新工作表並將其刪除。允許在工作表事件事件中插入
但是,無論何時插入一行,它都會自動將該行插入到其他工作表中,然後將其刪除。我怎樣才能防止它,所以只要單元格有一個值以便我可以插入,它只會執行工作表更改事件?
我認爲我可以通過檢查ActiveCell.Value <> ""
來進一步嵌套IF語句,但是單元格被關閉時會發生變化。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Dim LastRowCompleted As Long
Dim RowToDelete As Long
RowToDelete = 0
LastRowCompleted = Sheets("completed").Cells(Sheets("completed").Rows.Count, "A").End(xlUp).Row
LastRowCompleted = LastRowCompleted + 1 'Next row after last row
Set KeyCells = Range("I:I")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
'Cut and Paste Row
Target.EntireRow.Copy Sheets("completed").Range(LastRowCompleted & ":" & LastRowCompleted)
'Mark to delete row
RowToDelete = Target.EntireRow.Row
Call DeleteRow(RowToDelete)
Application.EnableEvents = True
End If
End Sub
Sub DeleteRow(Row As Long)
If Row > 0 Then
Rows(Row).EntireRow.Delete Shift:=xlUp
End If
End Sub