セルループ!

Sub LoopThroughRange()
    Dim cellValue As Variant
    Dim currentRow As Long
    
    ' 初期行番号を2に設定
    currentRow = 2
    
    ' セルC2からG2が空でない限りループを続ける
    Do While Application.WorksheetFunction.CountA(Range("C" & currentRow & ":G" & currentRow)) > 0
        ' セルC2からG2の範囲でループを行う
        For Each cellValue In Range("C" & currentRow & ":G" & currentRow).Value
            ' 変数に値を格納
            Dim myVariable As Variant
            myVariable = cellValue
            
            ' ここに、myVariableを使って何らかの処理を行うコードを書く
            ' 例えば、myVariableをデバッグ用にメッセージボックスに表示する場合は以下のようになります
            MsgBox myVariable
        Next cellValue
        
        ' 行番号を1つ増やす
        currentRow = currentRow + 1
    Loop
End Sub

この記事が気に入ったらサポートをしてみませんか?