2012-07-28 207 views
1

我是新來的編碼宏,只是有一個快速的問題。我一直在試圖做的是在空單元格之前選擇所有數據點,然後存儲最後一點的行索引。例如,在下面的代碼我想選擇行1-4和將被存儲在行指數爲4。我有此代碼到目前爲止它選擇數據點:獲取最後一個非空白單元格的行索引。

Cells(2, 2).Select 
Range(Selection, Selection.End(xlDown)).Select 

我只需要存儲的最後一行索引。示例數據:

1. 342 
2. 342 
3. 324 
4. 234 
5. <This would be an empty cell> 
6. 43242 
7. 342 
8. 32423 
9. 4324 

回答

0
' column = whatever column you're working with 

For evalRows = 1 to Range(Selection, Selection.End(xlDown)).Row 

    If IsEmpty(Cells(evalRows, column).Value) Then 

    ' you can only refer to the previous row if you're not on the first row 
    If evalRows = 1 then 

     ' do nothing 

    Else 

     ' refer to previous row 
     lastUsefulRow = Offset(evalRows - 1, column).Row 

    End If 

    End If 

Next 
1

試試這個

LastRow = Cells(2, 2).End(xlDown).Row 

如果你在選擇範圍的意圖,使用

LastRow = Selection.Row + Selection.Rows.Count - 1 

雖然我建議不要選擇範圍。使用這個代替

Dim rng As Range 
Set rng = Range(Cells(2, 2), Cells(2, 2).End(xlDown)) 
LastRow = rng.Row + rng.Rows.Count - 1 
相關問題