2013-10-02 54 views
0

我有以下代碼循環選定範圍的每一行。但是,如果只選擇一個單元格,代碼將循環遍歷工作表中的每一行,而不是僅處理一個實例。循環選定範圍的行時處理單個單元格

我需要做什麼以便for循環只處理一行時選擇單個單元格?

Dim myRange as Range 
Dim currRow as Range 

Set myRange = Selection 

For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible) 
    MsgBox currRow.Address 
Next currRow 

感謝,

回答

1

我不知道原因的代碼的行爲本身。它看起來不錯。
但要得到你想要的東西,試試這個:

Dim myRange As Range 
Dim currRow As Range 

Set myRange = Selection 

If myRange.Rows.count = 1 And myRange.Columns.count = 1 Then 
    For Each currRow In myRange.Rows 
     MsgBox currRow.Address 
    Next currRow 
Else 
    For Each currRow In myRange.Rows.SpecialCells(xlCellTypeVisible) 
     MsgBox currRow.Address 
    Next currRow 
End If 

希望這個作品。

0

只需添加一個if語句代碼來處理隱藏的行:

Dim myRange As Range 
    Dim currRow As Range 

    Set myRange = Selection 

    For Each currRow In myRange 
     If currRow.EntireRow.Hidden = False Then 
      'Place your code here. 
      Debug.Print currRow.Address 
     End If 
    Next currRow 
相關問題