0

選択した範囲の各行をループする以下のコードがあります。ただし、1 つのセルのみが選択されている場合、コードは 1 つのインスタンスだけを処理するのではなく、ワークシートの各行をループします。

1 つのセルが選択されたときに for ループが 1 つの行のみを処理するようにするには、どうすればよいですか?

Dim myRange as Range
Dim currRow as Range

Set myRange = Selection

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

ありがとう、

4

2 に答える 2

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

これがうまくいくことを願っています。

于 2013-10-02T07:47:55.857 に答える
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
于 2013-10-02T13:10:11.873 に答える