在 VBA 中多次插入特定行
Inserting a specific row multiple times in VBA
我们的客户希望他们的 excel 工作表格式化为在第 [=18= 列中有新值时插入新 header(整个“1”行)的位置。 ] 存在
本质上,我是在复制“1”行,并在我在 'A'
列中找到新值时将其插入
这是我用来格式化它的子程序,但它一直在循环并崩溃。
Sub insertHeader()
Range("A2").Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = ActiveCell.Offset(1, 0) Then
ActiveCell.Offset.Select
Else
Range("A1").EntireRow.Insert
ActiveCell.Offset.Select
End If
Loop
End Sub
你必须告诉 Offset 如何以及在哪里偏移。如偏移量(行偏移量,列偏移量):
ActiveCell.Offset(1,0).Select
哦,还有你的副本,伙计...好的,这是代码:
range("a3").select
Do Until ActiveCell.Value = ""
If ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value Then
'comparing it upwards, as the standard insert will insert a row above your active cell.
ActiveCell.EntireRow.Insert
Range("a1").EntireRow.Copy ActiveCell
ActiveCell.Offset(1, 0).Select
'move on to the row I was at before inserting a new row
End If
ActiveCell.Offset(1, 0).Select
Loop
我们的客户希望他们的 excel 工作表格式化为在第 [=18= 列中有新值时插入新 header(整个“1”行)的位置。 ] 存在
本质上,我是在复制“1”行,并在我在 'A'
列中找到新值时将其插入这是我用来格式化它的子程序,但它一直在循环并崩溃。
Sub insertHeader()
Range("A2").Select
Do Until ActiveCell.Value = ""
If ActiveCell.Value = ActiveCell.Offset(1, 0) Then
ActiveCell.Offset.Select
Else
Range("A1").EntireRow.Insert
ActiveCell.Offset.Select
End If
Loop
End Sub
你必须告诉 Offset 如何以及在哪里偏移。如偏移量(行偏移量,列偏移量):
ActiveCell.Offset(1,0).Select
哦,还有你的副本,伙计...好的,这是代码:
range("a3").select
Do Until ActiveCell.Value = ""
If ActiveCell.Value <> ActiveCell.Offset(-1, 0).Value Then
'comparing it upwards, as the standard insert will insert a row above your active cell.
ActiveCell.EntireRow.Insert
Range("a1").EntireRow.Copy ActiveCell
ActiveCell.Offset(1, 0).Select
'move on to the row I was at before inserting a new row
End If
ActiveCell.Offset(1, 0).Select
Loop