如何修改以下内容以将选择限制为仅从第 2 行开始?目前它选择每一行。
Set myRng = .Range("D2", .Cells(.Rows.Count, "D").End(xlUp))
要使其正常工作,您必须确保 cell 中有数据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