在数据更改之间插入 5 行,复制一列并将文本字符串添加到另一列
Insert 5 rows between data change, copy one column & add text string to another
我不了解 VBA 但我想知道我是否可以让它做一些我手动做的事情。
我有一些主题数据。这些列是名字、姓氏、员工、兄弟姐妹、Class、学校、管理员。
通常在“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
您只需按照与添加行相同的方式执行此操作,只是没有 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
我不了解 VBA 但我想知道我是否可以让它做一些我手动做的事情。
我有一些主题数据。这些列是名字、姓氏、员工、兄弟姐妹、Class、学校、管理员。 通常在“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
您只需按照与添加行相同的方式执行此操作,只是没有 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