VBA - Range.Row.Count

前端 未结 10 1483
别那么骄傲
别那么骄傲 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:47

    That is nice question :)

    When you have situation with 1 cell (A1), it is important to identify if second declared cell is not empty (sh.Range("A1").End(xlDown)). If it is true it means your range got out of control :) Look at code below:

    Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("Arkusz1")
    
    Dim k As Long
    
    If IsEmpty(sh.Range("A1").End(xlDown)) = True Then
        k = 1
    
    Else
        k = sh.Range("A1", sh.Range("A1").End(xlDown)).Rows.Count
    
    End If
    

提交回复
热议问题