[英]Excel Macro - Copy Row -> Insert Row under -> Paste data
我有一個貫穿 E 列的代碼(下面的示例中的 C 列)並搜索數據的任何更改。 然后它在該更改下插入一個空白行並循環遍歷數據集(200-500 行長)。 我正在尋找一種方法來在代碼中修改/添加最后一行數據的“復制和粘貼”功能,在更改之前,將其添加到新插入的行中。
前:
A欄 | B欄 | 立柱 C | E 列 |
---|---|---|---|
1 | 2 | 莎莉 | 5 |
1 | 2 | 莎莉 | 6 |
1 | 2 | 莎莉 | 2 |
1 | 2 | 追趕 | 1 |
1 | 2 | 追趕 | 4 |
1 | 2 | 本 | 9 |
后:
A欄 | B欄 | 立柱 C | E 列 |
---|---|---|---|
1 | 2 | 莎莉 | 5 |
1 | 2 | 莎莉 | 6 |
1 | 2 | 莎莉 | 2 |
2 | 莎莉 | ||
1 | 2 | 追趕 | 1 |
1 | 2 | 追趕 | 4 |
2 | 追趕 | ||
1 | 2 | 本 | 9 |
2 | 本 |
我道歉。 新來的。 我目前擁有的代碼有一個循環。 見打擊:
Sub CleanUpPart2()
'Insert Rows by column F
'
Dim iRow As Integer, iCol As Integer
Dim oRng As Range
Set oRng = Range("f1")
iRow = oRng.Row
iCol = oRng.Column
Do
'
If Cells(iRow + 1, iCol) <> Cells(iRow, iCol) Then
Cells(iRow + 1, iCol).EntireRow.Insert Shift:=xlDown
iRow = iRow + 2
Else
iRow = iRow + 1
End If
'
Loop While Not Cells(iRow, iCol).Text = ""
'
編輯::
請嘗試更新的代碼:
Sub testInsertRowCopyBefore()
Dim sh As Worksheet, lastRow As Long, i As Long
Set sh = ActiveSheet
lastRow = sh.Range("A" & Rows.count).End(xlUp).row
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For i = lastRow + 1 To 3 Step -1
If sh.Range("C" & i).Value <> sh.Range("C" & i - 1).Value Then
sh.Range("C" & i).EntireRow.Insert xlUp
sh.Range("B" & i & ":C" & i).Value = sh.Range("B" & i - 1 & ":C" & i - 1).Value
End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
MsgBox "Ready..."
End Sub
上面的代碼假設“Column A”、“Column B”和“Column C”是標題,它們保留在工作表的第一行。
請測試它並發送一些反饋
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.