在数据之间插入新行

时间:2017-06-22 12:43:15

标签: excel vba excel-vba

在单元格A1和A3中,我有数据。如果我在单元格A2中插入新数据,我需要在A2和A3之间插入一个新行,这样A3将始终保持在底部(将移动到A4 ......等等)。 我到处寻找,但我发现的是对插入物的不同需求。提前谢谢。

3 个答案:

答案 0 :(得分:1)

这是一个非常常见的问题,可以通过循环来解决。

您需要指定循环功能的步骤,例如:

For i = 100 to 2 Step -1

'Code... soemthign like If Cells(i,1).Value>0 Then Rows(i).EntireRow.Insert End If

Next i

这将按相反的顺序进行,这将消除向下移动的线的问题。

您还可以将最后一行设置为动态(我会让您找到该代码...快速谷歌搜索通常会将其打开)。 For语句看起来像:

Dim LR as Long
LR = 'dynamic code
For i = LR to 1 Step -1
'Code
Next i

答案 1 :(得分:1)

阅读上面的评论我可以看到你不在球场。找到你上一次使用的行可能有十几种左右。这是一对夫妇:

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastRow = ActiveSheet.Cells(1048576, 1).End(xlUp).row
LastRow = ActiveSheet.Columns("A").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

以下是完成任务的代码。

Sub tester1()
Dim LastRow As Long

    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    ActiveSheet.Range("A" & LastRow).Select
    Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
    ActiveSheet.Range("A" & LastRow).Value = "Done!"

End Sub

答案 2 :(得分:0)

谢谢大家,我做了一些小改动:

Dim LastRow As Long
    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
    ActiveSheet.Range("A" & LastRow).Select
    Selection.Offset(1, 0).EntireRow.Insert
End Sub
相关问题