VBA - Range.Row.Count

前端 未结 10 1442
别那么骄傲
别那么骄傲 2020-12-24 13:23

I have written a simple code to illustrate my predicament.

Sub test()
    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets(\"Sheet1\")

    Dim k As Lon         


        
相关标签:
10条回答
  • 2020-12-24 13:57
    k = sh.Range("A2", sh.Range("A1").End(xlDown)).Rows.Count
    

    or

    k = sh.Range("A2", sh.Range("A1").End(xlDown)).Cells.Count
    

    or

    k = sh.Range("A2", sh.Range("A1").End(xlDown)).Count
    
    0 讨论(0)
  • 2020-12-24 14:11

    You should use UsedRange instead like so:

    Sub test()
        Dim sh As Worksheet
        Dim rn As Range
        Set sh = ThisWorkbook.Sheets("Sheet1")
    
        Dim k As Long
    
        Set rn = sh.UsedRange
        k = rn.Rows.Count + rn.Row - 1
    End Sub
    

    The + rn.Row - 1 part is because the UsedRange only starts at the first row and column used, so if you have something in row 3 to 10, but rows 1 and 2 is empty, rn.Rows.Count would be 8

    0 讨论(0)
  • 2020-12-24 14:12

    Probably a better solution is work upwards from the bottom:

    k=sh.Range("A1048576").end(xlUp).row
    
    0 讨论(0)
  • 2020-12-24 14:13
    CountRows = ThisWorkbook.Worksheets(1).Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count
    
    0 讨论(0)
提交回复
热议问题