我对VBA一无所知,但我想知道我是否能让它做一些我手动做的事情。
我有一些主题数据。列有姓名、姓氏、教职员工、兄弟姐妹、班级、学校、行政。通常,"Class“列中有多个具有相同数据的行。每次"Class“列中的数据发生更改时,我都需要插入5行,将"class”从上面的单元格复制到这5行,并将单词"zzBLANK“添加到姓氏列。
我使用的一些示例数据:

最终的结果应该是这样:

这是可行的,谁能帮上忙吗?我设法找到了一些代码,其中在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发布于 2022-04-21 14:37:05
您只需按照添加行的方式进行,而不需要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 Subhttps://stackoverflow.com/questions/71955927
复制相似问题