![](/img/trans.png)
[英]Excel macro to cut a specific row of cells from one worksheet to another
[英]Need a macro to copy specific cells from a row in one worksheet to a specific cells in another worksheet base upon a data in the cell
我正在為我的業務編寫工作簿。
想要獲得一個宏來將數據從一個工作表的一行中的幾個單元格(從C到G的單元格)復制到另一工作表行(單元格A,B,C,F,G)。
問題是,僅當D列輸入了數據時,才需要傳輸它們。
我無法正確使用Alex P針對類似問題編寫的代碼:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If VBA.IsDate(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("A1048576").End(xlUp)), 1, .Range("A1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -3)
.Range("B" & nextRow) = Target.Offset(0, -1)
.Range("C" & nextRow) = Target
End With
End If
End If
End Sub
嘗試這個。 我得到了這個工作表:
工作表1
表2
應用以下代碼:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If Not VBA.IsEmpty(Target) Or VBA.IsNull(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("B1048576").End(xlUp)), 1, .Range("B1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -1)
.Range("B" & nextRow) = Target
.Range("C" & nextRow) = Target.Offset(0, 1)
.Range("F" & nextRow) = Target.Offset(0, 2)
.Range("G" & nextRow) = Target.Offset(0, 3)
End With
End If
End If
End Sub
嘗試在工作表1輸入新行
結果見表2
您需要注意的一點是:確保第1頁的D列是最后輸入的內容(如果您首先輸入的話),那么將復制空白行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.