我對VBA一無所知,但我想知道是否可以讓它做一些我手動做的事情。
我有一些主題資料。這些列是名字、姓氏、員工、兄弟姐妹、班級、學校、管理員。通常“類”列中有多行具有相同資料。每次“類”列中的資料發生變化時,我需要插入 5 行,將上面單元格中的“類”復制到這 5 行中,并將單詞“zzBLANK”添加到 surname 列。
我使用的一些示例資料:

最終結果應該是這樣的:

這是可能的嗎?有人可以幫忙寫代碼嗎?我設法找到了一些代碼,它在 E 列的資料更改之間添加了 5 行,但我找不到如何將資料添加到這些行中。或者至少我不明白能夠采用其他代碼并將其更改為我的需要。
Sub DoubleRowAdder()
Dim i As Long, col As Long, lastRow As Long
col = 5
lastRow = Cells(Rows.Count, col).End(xlUp).Row
For i = lastRow To 2 Step -1
If Cells(i - 1, col) <> Cells(i, col) Then
Range(Cells(i, col).EntireRow, Cells(i 4, col).EntireRow).Insert shift:=xlDown
End If
Next i
End Sub
uj5u.com熱心網友回復:
您只需要以與添加行相同的方式進行操作,而無需添加EntireRow所需的列:
Option Explicit
Public Sub DoubleRowAdder()
Const col As Long = 5
Const AddRows As Long = 5
Dim lastRow As Long
lastRow = Cells(Rows.Count, col).End(xlUp).Row
Dim i As Long
For i = lastRow To 2 Step -1
If Cells(i - 1, col) <> Cells(i, col) Then
'add rows
Range(Cells(i, col).EntireRow, Cells(i AddRows - 1, col).EntireRow).Insert shift:=xlDown
'fill column 5 in those rows with value above
Range(Cells(i, col), Cells(i AddRows - 1, col)).Value = Cells(i - 1, col).Value
'fill column 2 in those rows with zzBLANK
Range(Cells(i, 2), Cells(i AddRows - 1, 2)).Value = "zzBLANK"
End If
Next i
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/460536.html
