複数セル=ある範囲 の処理

本日のVBA、のお時間です。
ちゃららっちゃっちゃっちゃ♪(ry

Selection

こいつを使うぜ。

Selection なので、こいつを使う前にセルを選択しておく必要がある。
こいつは、

Selection.Address

とすると面白いんだが、、あまり実用性がない。
ExcelのINDIRECTとかと組み合わせるとか?あんまり思いつかないや)

とりあえず一例。

Sub MultiCellProcess()
    Dim intSt_Row As Integer, intEd_Row As Integer
    Dim intSt_Col As Integer, intEd_Col As Integer
    
    intSt_Row = Selection.Cells(1).Row
    intEd_Row = Selection.Cells(Selection.Count).Row
    intSt_Col = Selection.Cells(1).Column
    intEd_Col = Selection.Cells(Selection.Count).Column
    For ii = intSt_Row To intEd_Row
        For jj = intSt_Col To intEd_Col
            Cells(ii, jj).Value = ii * jj ’適当に処理。とりあえず九九っぽく
        Next jj
    Next ii
End Sub


とりあえず二例目。

Sub MultiCellProcess2()
    Dim intCnt As Integer
    Dim objR As Range

    intCnt = 1
    For Each objR In Selection.Cells
        objR.Value = intCnt    ' ここで適当な処理を行う
        intCnt  = intCnt  + 1 ' とりあえず処理される順番をみてみる
    Next
End Sub