vba 如何选择第二行到最后一行的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17327037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:59:20  来源:igfitidea点击:

How to select a range of the second row to the last row

excel-vbavbaexcel

提问by rwbarg15

I am currently trying to figure out how to select a range from the second row to the last row, but more specifically between a range of columns. For instance I want to select Range(A2:L2) to the last row of data in the spreadsheet.

我目前正试图弄清楚如何选择从第二行到最后一行的范围,但更具体地说是在一系列列之间。例如,我想选择 Range( A2:L2) 到电子表格中最后一行数据。

I have tried,

我试过了,

Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Range("A2:L2" & Lastrow).Select

But this selects from A2:L2all the way down to the bottom of the spreadsheet. I have checked to see if Lastrowwas incorrect but I printed it to a cell and the correct count of rows displayed.

但这从A2:L2一直到电子表格的底部进行选择。我已经检查过是否Lastrow不正确,但我将其打印到一个单元格并显示了正确的行数。

回答by rwisch45

Try this:

尝试这个:

Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Range("A2:L" & Lastrow).Select

Let's pretend that the value of Lastrowis 50. When you use the following:

让我们假设 的值为Lastrow50。当您使用以下内容时:

Range("A2:L2" & Lastrow).Select

Then it is selecting a range from A2 to L250.

然后它选择从 A2 到 L250 的范围。

回答by Yehia Amer

Sub SelectAllCellsInSheet(SheetName As String)
    lastCol = Sheets(SheetName).Range("a1").End(xlToRight).Column
    Lastrow = Sheets(SheetName).Cells(1, 1).End(xlDown).Row
    Sheets(SheetName).Range("A2", Sheets(SheetName).Cells(Lastrow, lastCol)).Select
End Sub

To use with ActiveSheet:

与 ActiveSheet 一起使用:

Call SelectAllCellsInSheet(ActiveSheet.Name)