vba 如何在vba中获得无限范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19802308/
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
how to get unlimited range in vba
提问by BruceyBandit
Dim Brand As String
Worksheets("1st").Select
Brand = Range()
Hi, I am using VBA coding in microsoft excel 2011 and I am a little stuck with range. Lets say I want to start something in cell c4, I enter in c4 in the range but if I then want it to continue till all the way down the column (unlimited as we don't know how many rows in that column will be filled up), then what is the correct way of writing this?
嗨,我在 microsoft excel 2011 中使用 VBA 编码,但我对范围有点困惑。假设我想在单元格 c4 中开始一些东西,我在范围内输入 c4,但如果我希望它一直持续到列(无限制,因为我们不知道该列中有多少行将被填充up),那么正确的写法是什么?
回答by user2140261
Range("C4", Range("C4").End(xlDown))
This will return the range, C4 to C4 and Down.
这将返回范围,C4 到 C4 和向下。
This will select all contiguous cell from C4 down all the way until the first blank cell.
这将选择从 C4 一直到第一个空白单元格的所有连续单元格。
Now if you Data has a chance that it might have blanks in between non blank cells. Then you can use the following:
现在,如果您的数据有机会在非空白单元格之间有空白。然后您可以使用以下内容:
Range("C4", Range("C" & Rows.Count).End(xlUp))
this will select C4 to The last cell in C columns up until the the cell with data. In other words this will select EVERY cell in the C column from C4 until the last cell in the entire column with a value, and include blanks.
这将选择 C4 到 C 列中的最后一个单元格,直到包含数据的单元格。换句话说,这将选择 C 列中从 C4 到整列中最后一个带有值的单元格中的每个单元格,并包括空格。
回答by Santosh
Another way
其它的办法
Sub Main()
Dim Brand As String
Dim lastRow As Long
Dim rng As Range, cell As Range
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("1st")
With sht
lastRow = .Range("C" & .Rows.Count).End(xlUp).Row
End With
If lastRow < 4 Then lastRow = 4
Set rng = sht.Range("C4:C" & lastRow)
For Each cell In rng
MsgBox cell.Value
Next
End Sub