gpt4 book ai didi

excel - 在数据更改之间插入 5 行,复制一列并将文本字符串添加到另一列

转载 作者:行者123 更新时间:2023-12-04 20:07:49 28 4
gpt4 key购买 nike

我对VBA一无所知,但我想知道是否可以让它做一些我手动做的事情。
我有一些主题数据。这些列是名字、姓氏、员工、 sibling 、类(class)、学校、管理员。
通常“类”列中有多行具有相同数据。每次“类”列中的数据发生变化时,我需要插入 5 行,将上面单元格中的“类”复制到这 5 行中,并将单词“zzBLANK”添加到 surname 列。
我使用的一些示例数据:
orig example data
最终结果应该是这样的:
Result example data
这是可能的吗?有人可以帮忙写代码吗?我设法找到了一些代码,它在 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

关于excel - 在数据更改之间插入 5 行,复制一列并将文本字符串添加到另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71955927/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com