EXCEL - VBA

VBAは結構書いていたんですけど…

環境:VBA Ver 7.1

デバッグプリント

イミディエイトウインドウに「Hello VBA」を表示します。
はじめはイミディエイトウインドウが表示されていないと思いますので、[表示] - [イミディエイトウインドウ] で表示されます。

Sub func00()
    Debug.Print ("Hello VBA")
End Sub

コメント

単行のコメントしかありません。
複数行コメントアウトしたり戻したりしたい場合は、[表示] - [ツールバー] - [編集]でメニューを表示し、「コメントブロック」、「非コメントブロック」を使います。

Sub func00()
    ' Debug.Print ("Hello VBA")
End Sub

文字列連結

「&」で連結します。

Debug.Print ("Hello" & " " &  "World")

メッセージボックスの表示

処理を止めることが出来るのでよく使ってました。

MsgBox "SUCCESS"

セルの値を取得

A1,B1のセルの値を取得します。
Range(”A1)のほうが分かりやすいのですが、LOOPで回す場合はCellsを使う感じになります。

Sub func00()

    Debug.Print (Range("A1").Value) ' A1の値
    Debug.Print (Cells(1, 1).Value) ' A1の値
    
    Debug.Print (Range("B1").Value) ' B1の値
    Debug.Print (Cells(1, 2).Value) ' B1の値
    
    Debug.Print (Range("A2").Value) ' A2の値
    Debug.Print (Cells(2, 1).Value) ' A2の値
    
    Debug.Print (Range("B2").Value) ' B2の値
    Debug.Print (Cells(2, 2).Value) ' B2の値

End Sub

横方向に処理

Cells(縦、横)となります。

Sub func00()

    Dim i As Long
    
    ' A1,B1,C1,D1,E1セルの値を表示します
    For i = 1 To 5
        Debug.Print (Cells(1, i).Value)
    Next i

End Sub

縦方向に処理

Cells(縦、横)となります。
行で処理する場合が多いのでこっちをよく使います。

Sub func00()

    Dim i As Long
    
    ' A1,A2,A3,A4,A5セルの値を表示します
    For i = 1 To 5
        Debug.Print (Cells(i, 1).Value)
    Next i

End Sub

Rows.Count って何?

これも使うんだけど…まずはなんとなくそういうもの程度の認識で良いかと…
以下の値が出力されます。
1048576

Sub func00()

    Debug.Print (Rows.Count)

End Sub

Cells(Rows.Count, 1).End(xlUp).Row だと…

縦方向に処理する場合で、「A1列に値が存在する最後の行数」を取得することができます。
途中に空白がっても「A1列に値が存在する最後の行数」を取得できます。
A1列を先頭から最後まで処理する場合はこれを使います。

個人的には、処理するデータに不備がない前提で「何行目まで」と決めてしまいます。

A列の値を表示します。途中に空白セルがあってもそのまま処理します。

Sub func00()
    Dim i As Long

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row

        Debug.Print (Cells(i, 1))

    Next i
End Sub

1行目から順番に処理をする

1行目から処理を行う。
A列に空白があったら、エラーメッセージ出して終了する。

Sub func01()
    Dim i As Long

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row

        If Cells(i, 1) = "" Then
            MsgBox "ERROR" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))

    Next i
End Sub

10行目まで処理したら終了する

Sub func01()
    Dim i As Long

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row

        If i >= 10 Then
            MsgBox "SUCCESS" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))

    Next i
End Sub

A1列に”END”があれば終了する。

Sub func01()
    Dim i As Long
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
   
        If Cells(i, 1) = "END" Then
            MsgBox "SUCCESS" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))

    Next i
End Sub

1行目から順番に処理、指定のキーワードが見つかったら背景に色を付ける

・A列の「Hello」のセルを赤に
・A列に「END」のセルが見つかったら処理終了

Sub func01()
    Dim i As Long

    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
   
        If Cells(i, 1) = "END" Then
            MsgBox "SUCCESS" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))
        If Cells(i, 1) = "Hello" Then
            Cells(i, 1).Interior.Color = RGB(255, 0, 0) ' red
        End If

    Next i
End Sub

1行目から順番に処理、指定のキーワードが見つかったら、その行の背景に色を付ける、データの存在する列で最右の列まで色を付ける。

Sub func01()
    Dim i As Long
    Dim columnEnd As Long
    
    columnEnd = Cells(1, Columns.Count).End(xlToLeft).Column ' (データの存在する列で)最後の列番号
    
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
   
        If Cells(i, 1) = "END" Then
            MsgBox "SUCCESS" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))
        If Cells(i, 1) = "Hello" Then
            Range(Cells(i, 1), Cells(i, columnEnd)).Interior.Color = RGB(255, 0, 0) ' red
        End If

    Next i
End Sub

1行目から順番に処理、指定のキーワードが見つかったら、その行の背景に色を付ける、20列目まで色を付ける。

Sub func01()
    Dim i As Long
    Dim columnEnd As Long
    
    columnEnd = 20 ' この列まで色付け
    
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
   
        If Cells(i, 1) = "END" Then
            MsgBox "SUCCESS" & " " & i & "LINE"
            Exit For
        End If

        Debug.Print (Cells(i, 1))
        If Cells(i, 1) = "Hello" Then
            Range(Cells(i, 1), Cells(i, columnEnd)).Interior.Color = RGB(255, 0, 0) ' red
        End If

    Next i
End Sub

変数宣言の強制

モジュールの先頭に記載する。どちらがよいのかはケースバイケースだと思います。

Option Explicit

For文

Sub fucn1()
    Dim i As Integer
    For i = 1 To 100
        Debug.Print i & "回目の繰り返し処理です。"
    Next
End Sub

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