[英]Count number of rows in worksheet
I want to count number of rows in Sheet1, from the Sheet2 code module. 我想从Sheet2代码模块中计算Sheet1中的行数。
In the sheet1 code module, the following code works fine 在sheet1代码模块中,以下代码工作正常
ctr = Range("B2", Range("B2").End(xlDown)).Count
I tried the same code in the Sheet2 code module 我在Sheet2代码模块中尝试了相同的代码
recct = ThisWorkbook.Sheets("Sheet1").Range("B2", Range("B2").End(xlDown)).Count
I am getting run time error 1004 Application -Defined or Defined error
我得到
run time error 1004 Application -Defined or Defined error
Thanks 谢谢
The error occurs in the 2nd range reference in recct
. 该错误发生在
recct
中的第二个范围参考中。 Because you are referencing a different sheet, you need to tell VBA the sheet name in both range references. 因为您引用了不同的工作表,所以需要告诉VBA两个范围引用中的工作表名称。
Try this instead: 试试这个:
With ThisWorkbook.Sheets("Sheet1")
recct = .Range("B2", .Range("B2").End(xlDown)).Rows.Count
End With
Alternatively, this will work as well (though a bit sloppier). 或者,这也会起作用(虽然有点邋))。
recct = ThisWorkbook.Sheets("Sheet1").Range("B2", ThisWorkbook.Sheets("Sheet1").Range("B2").End(xlDown)).Rows.Count
Update 更新
Since there is a lot of discussion around what you actually mean by number of rows on the sheet, use the above code to literally start at B2 and count the number of contiguous cells directly underneath 由于关于工作表上行数的实际含义有很多讨论,使用上面的代码从字面上开始B2并直接计算下面连续单元的数量
However, if you want to find the last "real" used cell in column B (by real, I mean with data in it) do this: 但是,如果你想在B列中找到最后一个“真正的”用过的单元格(通过实际,我的意思是其中包含数据),请执行以下操作:
With ThisWorkbook.Sheets("Sheet1")
recct = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Rows.Count
End With
You can use this for example: 您可以使用此示例:
rowsInThere = Sheets("Sheet1").UsedRange.Rows.Count
This works without ranges. 这没有范围。 Also you might use
ActiveSheet
as a sheet to check, in case you would need to change current sheet and check its rows count. 您还可以使用
ActiveSheet
作为工作表来检查,以防您需要更改当前工作表并检查其行数。
Two things 两件事情
code 码
Sub GetB()
Dim ws As Worksheet
Set ws = Sheets(1)
Dim lngCnt As Long
lngCnt = ws.Range(ws.[b2], ws.Cells(Rows.Count, "b").End(xlUp)).Count
End Sub
more robust 更强大
To handle all situations cleanly then Find
is easier 要干净地处理所有情况,那么
Find
更容易
Sub GetB()
Dim ws As Worksheet
Dim rng1 As Range
Set ws = Sheets(1)
Set rng1 = ws.Range("B:B").Find("*", ws.[b1], xlValues, , , xlPrevious)
If Not rng1 Is Nothing Then
Select Case rng1.Row
Case 1
MsgBox "Only B1 has data", vbCritical
Case 2
MsgBox "No used cells past B2"
Case Else
MsgBox rng1.Row - 1 & " cells between B2 and B" & rng1.Row
End Select
Else
MsgBox ws.Name & " column B Is blank", vbCritical
End If
End Sub
Don't know if this will help but I use this in my modules all the time: 不知道这是否会有所帮助,但我一直在我的模块中使用它:
Dim TR as long, TC as long
TR = [Sheet1!A1].CurrentRegion.Rows.count
TC = [Sheet1!A1].CurrentRegion.Columns.count
If I know that if the dataset I'm dealing with doesn't have an empty row or column, like an extract from another program or something, then it's quick and works great! 如果我知道如果我正在处理的数据集没有空行或列,就像从另一个程序或其他东西中提取的那样,那么它很快就会很好用! From this I can specify a range select or perform a vlookup.
从这里我可以指定范围选择或执行vlookup。
TR = [Sheet1!A1].CurrentRegion.Rows.count
[I2] = "=vlookup($C2,'sheet1'!A$2:B$" & TR & ",2,FALSE)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.