vba 计算另一个工作表中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27763089/
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
Count the number of rows in another sheet
提问by user2859603
I have looked at the suggested questions to find the answer to my problem. The closest question is called: Count number of rows in a different Excel Sheet Count number of rows in a different Excel Sheet
我查看了建议的问题以找到我的问题的答案。最接近的问题称为:Count number of rows in a different Excel Sheet Count number of rows in a different Excel Sheet
The solution to that problem does not work for me.
该问题的解决方案对我不起作用。
I am trying to count the number of rows in a range in a different worksheet than the active worksheet. Here is my code:
我正在尝试计算与活动工作表不同的工作表中某个范围内的行数。这是我的代码:
Sub verbflashcards()
Dim wordcount As Long
With Worksheets("Verbs")
wordcount = .Range(Cells(4, 1), Cells(4, 1).End(xlDown)).Rows.Count
End With
MsgBox (wordcount)
End Sub
I have a worksheet called Verbs and it is the second worksheet in the workbook. I have tried:
我有一个名为 Verbs 的工作表,它是工作簿中的第二个工作表。我试过了:
With Verbs
With Sheet2
With Sheets("Verbs")
With Sheets("Sheet2")
None of them seem to work.
它们似乎都不起作用。
采纳答案by Paresh J
Check this and hope this will help you:
检查这个,希望这会帮助你:
Sub verbflashcards()
Dim wordcount As Long
wordcount = ActiveWorkbook.Worksheets("Verbs").Range("A4", Worksheets("Verbs").Range("A4").End(xlDown)).Rows.Count
MsgBox (wordcount)
End Sub
Where, D1is the column from which you can get row count.
其中,D1是您可以从中获取行数的列。
Method 2:
方法二:
Sub verbflashcards()
Dim wordcount As Long
With Sheets("Verbs")
wordcount = .Range("A" & .Rows.Count).End(xlUp).Row
End With
MsgBox (wordcount)
End Sub
Note: There are lots of answers to your questions. Check this SO link: How can I find last row that contains data in the Excel sheet with a macro?
注意:您的问题有很多答案。检查此 SO 链接:如何使用宏在 Excel 工作表中找到包含数据的最后一行?
回答by
Your original was not working because the parent of Cells(4, 1)
and Cells(4, 1).End(xlDown)
was not specified. Prefix any cell address with a period (aka . or full stop) when you are inside a With ... End With
block. Example:
您的原件不起作用,因为没有指定Cells(4, 1)
和的父项Cells(4, 1).End(xlDown)
。当您在块内时,在任何单元格地址前加上句点(又名 . 或句号)With ... End With
。例子:
With Worksheets("Verbs")
wordcount = .Range(.Cells(4, 1), .Cells(4, 1).End(xlDown)).Rows.Count
End With
Note the .Cells(4, 1)
and not Cells(4, 1)
. The period specifies that the cell(s) you are referring to are within Worksheets("Verbs").
请注意.Cells(4, 1)
和 不是Cells(4, 1)
。句点指定您所指的单元格在 Worksheets("Verbs") 内。