如何开始在第2行选择,而不是所有行?

时间:2013-10-18 14:21:54

标签: excel vba excel-vba

如何修改以下内容以将选择限制为仅从第2行开始?目前它选择每一行。

Set myRng = .Range("D2", .Cells(.Rows.Count, "D").End(xlUp))

1 个答案:

答案 0 :(得分:1)

要使其正常工作,您必须确保单元格D2中有数据。见这个例子。

Sub Sample()
    Dim myRng As Range
    Dim ws As Worksheet
    Dim Lrow As Long

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Get last row which has data in Col D
        Lrow = .Range("D" & .Rows.Count).End(xlUp).row

        If Not Lrow < 2 Then
            Set myRng = .Range("D2:D" & Lrow)
            MsgBox myRng.Address
        Else
            MsgBox "There is no data in cell D2"
        End If
    End With
End Sub