简体   繁体   English

计算B3中的范围(行)begingin

[英]counting a range (rows) begingin in B3

I have data that starts in B3 and is dynamic so its range may be 5 rows to 500. I want to loop through the number of rows in the range. 我的数据从B3开始并且是动态的,因此它的范围可能是5行到500.我想循环遍历范围内的行数。

This is for a data query on number of report checked in. 这是针对签入的报告数量的数据查询。

Dim range1 As Range

Set range1 = Sheet1.Range("B3 : B" & Cells(Rows.Count)).End(xlUp).Rows

The result should be a range B3:B46 or whatever the end row number is. 结果应该是范围B3:B46或任何结束行号。

Try this: 尝试这个:

 Dim cell As Range

    For Each cell In Sheet1.Range("B3:B" & Sheet1.Cells(Sheet1.Rows.count, 2).End(xlUp).Row)
        'Do your stuff
    Next cell

Try this 尝试这个

Dim range1 As Range
Dim x as long

With Sheet1
    x= .Cells(.rows.Count, 2).End(xlUp).Row
    Set range1 = .Range("B3", .Cells(x,2))
End with

Another way to do it that will give you the last row that has data in it: 另一种方法是为您提供包含数据的最后一行:

Option Explicit
Sub RowCount()

    Dim WS As Worksheet
    Dim lrow As Long

    Set WS = ActiveSheet

    With WS
        lrow = .Range("B" & .Rows.Count).End(xlUp).Row - 2
    End With
End Sub

Simplest way is to use the .End(xlUp) property. 最简单的方法是使用.End(xlUp)属性。

Dim rng as Range: Set rng = Sheet1.Range("B3:B" & Sheet1.Cells(Rows.Count, 2).End(xlUp).Row)

Now you have B3:B<last_row> stored in the variable rng 现在你有B3:B<last_row>存储在变量rng

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM