简体   繁体   English

如何使用 VBA 在 Excel 的范围末尾插入行

[英]How to insert row at the end of a range in Excel with VBA

I'm trying to insert a row at the bottom of the range, but nothing happens when I run the below code.我试图在范围的底部插入一行,但是当我运行下面的代码时没有任何反应。 If I remove the "1" in如果我删除“1”

Cells(nextRow, 1).EntireRow.Insert

It will insert a row at the top of the range.它将在范围的顶部插入一行。

Sub newRow()

Application.ScreenUpdating = False
 Sheet1.Activate

'goes to the row at the bottom of the range
nextRow = Cells(Rows.Count, 1).End(xlUp).row + 1

'inserts new row
Cells(nextRow, 1).EntireRow.Insert

Application.ScreenUpdating = True

End Sub

Inserting a row at the bottom of the range is not visible - and it is an empty row, which is not different than the unused empty rows.在范围底部插入一行是不可见的 - 它是一个空行,与未使用的空行没有什么不同。 Anyway, inserting a row before the last one of the range is quite visible:无论如何,在范围的最后一个之前插入一行是非常明显的:

Sub NewRow()

    Dim nextRow As Long

    With Sheet1
        nextRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Cells(nextRow, 1).EntireRow.Insert
    End With

End Sub

在此处输入图像描述

Usin With Sheet1 and .Cells() instead of Select and Activate is a good practice - How to avoid using Select in Excel VBA Usin With Sheet1 and .Cells() instead of Select and Activate is a good practice - How to avoid using Select in Excel VBA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM