2013-10-18 54 views
-1

如何修改以下内容以将选择限制为仅在第2行开始?目前它选择每一行。如何在第2行开始选择,而不是所有行?

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

如果D列中没有任何内容或者选择了D2:D(last_row_with_data_in_D),则显示的代码选择行1:2,不是吗?除了直到D列最后一行有数据之外,我不觉得可以选择所有行。 –

回答

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 
相关问题