我的Worksheet_Calculate活動運作良好。但是,當它粘貼時,它會找到下一個打開的行,sht2然后一旦再次觸發事件,它就會重新粘貼到它第一次粘貼的原始行上。A39:Q39目標是每次觸發事件時復制/粘貼到下一個可用行中,sht2而不是覆寫之前的粘貼。
Private Sub Worksheet_Calculate()
Dim timeCells As Range, i As Integer
If Worksheets("Dashboard").ToggleButton1.Value = True Then
On Error GoTo SafeExit
Application.EnableEvents = False
'TimeLog
Set timeCells = Me.Range("D4:D393")
Set sht1 = ThisWorkbook.Sheets("Dashboard")
Set sht2 = ThisWorkbook.Sheets("Log")
Set cpyRng = sht1.Range("A39:Q39")
Set rngLogTargetBeginningCell = sht2.Cells(Rows.Count, 1).End(xlUp)
Set rngLastCellSelection = Selection
Application.ScreenUpdating = False ' Stop Updating Graphic during data copy
'TimeLog
For i = 1 To UBound(myArrTimeLog)
If timeCells(i, 1).Value <> myArrTimeLog(i, 1) Then
cpyRng.Copy
rngLogTargetBeginningCell.Offset(0, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False ' Remove the copy area marker
rngLastCellSelection.Select ' reselect the old cell
Application.ScreenUpdating = True ' update graphics again
End If
Next i
End If
SafeExit:
Application.EnableEvents = True
PopulateTimeLog
End Sub
uj5u.com熱心網友回復:
好吧,這是一個簡單任務的大量代碼。您的代碼復制到與以前完全相同的位置,因為您告訴代碼在最后一行的單元格 1 旁邊開始一列。將粘貼行中的偏移更改為
rngLogTargetBeginningCell.Offset(1, 0).PasteSpecial xlPasteValues
你可以走了。由于您的代碼中有更多資訊,如果沒有更多資訊我無法解釋,我不能肯定地告訴您,它有效......但它可能是一個嘗試......
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/447205.html
上一篇:Excel計數/唯一動態陣列
下一篇:在Excel中過濾文本和數字陣列
