简体   繁体   English

如何计算 excel vba 中特定单元格数据的行数

[英]how to count the number of rows with data from a specific cell in excel vba

I am just learning VBA excel.我只是在学习 VBA excel。 I am writing a code to count number of rows with data from specific cell say B5我正在编写一个代码来计算来自特定单元格的数据的行数,比如 B5

the code as follows.代码如下。

Sub Count_Rows()

    Dim No_Of_Rows As Integer
    No_Of_Rows = Range("B5").End(xlDown).Rows.Count
    MsgBox No_Of_Rows
End Sub

the above code always returns the value 1.上面的代码总是返回值 1。

Using End使用结束

  • Range("B5").End(xlDown) is a reference to the cell before the first following empty cell. Range("B5").End(xlDown)是对后面第一个空单元格之前的单元格的引用。 So it has only one row.所以它只有一排。 You want to return the number of rows of the range from the first cell to this cell.您想要返回从第一个单元格到此单元格的范围的行数。

The More Reliable xlUp更可靠的xlUp

This will 'look' from the bottom-most cell of the worksheet upward and find the first empty cell to use the range to return the number of rows.这将从工作表最底部的单元格向上“查找”并找到第一个空单元格以使用该范围返回行数。

Sub countRowsUp()
    Dim No_Of_Rows As Long
    No_Of_Rows = Range("B5", Range("B" & Rows.Count).End(xlUp)).Rows.Count
    'No_Of_Rows = Range(Range("B5"), Range("B" & Rows.Count).End(xlUp)).Rows.Count
    MsgBox No_Of_Rows
End Sub

The Less Reliable xlDown不太可靠的xlDown

This will 'look' from the given cell downward and find the first empty cell to use the range to return the number of rows.这将从给定单元格向下“查看”并找到第一个空单元格以使用该范围返回行数。

Sub countRowsDown()
    Dim No_Of_Rows As Long
    No_Of_Rows = Range("B5", Range("B5").End(xlDown)).Rows.Count
    'No_Of_Rows = Range(Range("B5"), Range("B5").End(xlDown)).Rows.Count
    MsgBox No_Of_Rows
End Sub

It is always returning 1 because you are counting the number of rows in a range that you have defined as a single cell.它始终返回 1,因为您正在计算已定义为单个单元格的范围内的行数。 If you want the value from a specific cell you use .Range("B5").Value and if you want to check if all the rows in a certain set of cells have that value (lets say B10-B100) you read it into a variable and loop through it:如果您想要来自特定单元格的值,则使用.Range("B5").Value并且如果您想检查某个单元格集中的所有行是否具有该值(比如说 B10-B100),则将其读入一个变量并循环遍历它:

Dim wS As Worksheet
Dim searchArea as Range
Dim c as Range
Dim vl As Variant
Dim No_Of_Rows as Integer

Set wS = ActiveSheet
Set searchArea = wS.Range("B10:B100")
vl = wS.Range("B5").Value
No_Of_Rows = 0

For Each c in searchArea
    If (c.Value = vl) Then
        No_Of_Rows = No_Of_Rows + 1
    End If
Next

MsgBox No_Of_Rows

This might have slight syntax errors so if it doesn't run I will clarify/correct.这可能有轻微的语法错误,所以如果它没有运行,我会澄清/纠正。

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

相关问题 使用excel VBA计算特定行号的行数 - Count number of rows from a specific row number using excel VBA Excel VBA-如何基于两个单元格值添加行数并从第三个单元格复制/保留数据? - Excel VBA - How to add number of rows based on two cell values and copy/keep data from the third? 使用 VBA,我想计算行数,然后在特定单元格中打印 - Using VBA, I want to count the number of rows and then print this in a specific cell Excel VBA - 如果行数包含文本,则在单元格中输入特定文本 - Excel VBA - If number of rows contain text then enter specific text in cell 如何根据Excel VBA中使用的行数为特定的单元格区域着色 - How to color specific cell ranges depending on number of used rows in Excel VBA 如何使用VBA宏从指定的单元格开始计算Excel中的行 - How to count rows in excel starting in specified cell with vba macro 如何用数据统计excel中的行数? - How to count the number of rows in excel with data? 如果使用 VBA Excel 相邻单元格不是空白,如何自动编号行? - How to auto number rows if adjacent cell is not blank using VBA Excel? Excel VBA代码突出显示特定单元格的行 - Excel VBA code to highlight rows for a specific cell 如何计算包含包含特定文本的单元格的行数? - How to count the number of rows which contain a cell containing specific text?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM