0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

個人メモ(VBA)

Last updated at Posted at 2024-07-27

データの件数がわからないとき(最終行を取得)

MsgBox Cells(Rows.Count, 1).End(xlUp).Row

    '変数MaxRowを宣言
    Dim MaxRow As Long
    

    '最終行を取得
    MaxRow = Cells(Rows.Count, 1).End(xlUp).Row


    '最終行まで繰り返す
    MsgBox MaxRow
    Dim i As Long
    For i = 2 To MaxRow
        Cells(i, 2) = Cells(i, 1) - 100
    Next i

二重ループ

    'カウンタ変数i,jを宣言
    Dim i As Long, j As Long
    
    '二重ループ
    For i = 2 To 11
        For j = 1 To 3
            'すべてのセルに-100
            Cells(i, j) = Cells(i, j) - 100
        Next j
    Next i

文字列を置換

    Dim str As String
    
    str = "東京都品川区"
    str = Replace(str, "東京都", "")
    MsgBox str

image.png

'InStr関数と組み合わせ

    Dim eMail As String
    eMail = "abcdeffff@excel233333.com"

    '@は何文字目にあるか調べる
    MsgBox InStr(eMail, "@")


    '"@"より前を自動抽出
    MsgBox Left(eMail, InStr(eMail, "@") - 1)


    '"@"より後を自動抽出
    MsgBox Right(eMail, Len(eMail) - InStr(eMail, "@") - 1)

image.png

StrConv関数

    Dim str As String
    str = "ABCはエービーシー"

    '英語を大文字/小文字に変換する
    Range("A1") = StrConv(str, vbUpperCase)
    Range("A2") = StrConv(str, vbLowerCase)


    '文字列を全角/半角に変換する
    Range("A3") = StrConv(str, vbWide)
    Range("A4") = StrConv(str, vbNarrow)


    'かなをひらがな/カタカナに変換する
    Range("A5") = StrConv(str, vbHiragana)
    Range("A6") = StrConv(str, vbKatakana)

image.png

Format関数

    '現在日付を西暦(yyyy/m/d形式)に変換
    Range("A1") = Format(Now, "yyyy/m/d")
    
    '曜日に変換
    Range("A2") = Format(Now, "aaaa")
    
    '和暦に変換
    Range("A3") = Format(Now, "ggge年m月d日")
    
    'yyyy年m月d日(曜)形式に変換
    Range("A4") = Format(Now, "yyyy年m月d日(aaa)")

    '数値を桁区切り形式に変換(0の場合は非表示)
    Range("A5") = Format(29800, "#,###")
    Range("A6") = Format(0, "#,###")
    
    '数値を桁区切り形式に変換(0の場合は表示)
    Range("A7") = Format(29800, "#,##0")
    Range("A8") = Format(0, "#,##0")

image.png

偶数であれば色を塗る/5行目であれば...

    '最終行を取得する
    Dim maxRow As Long
    maxRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    
    '行番号を取得する
    Dim i As Long
    For i = 2 To maxRow
        
        '偶数であれば色を塗る
        If Range("A" & i).Row Mod 2 = 0 Then
            Range("A" & i).Resize(, 7).Interior.Color = RGB(226, 239, 218)
        End If
        
        
        '5行ごとに罫線
        If Range("A" & i).Row Mod 5 = 1 Then
            Range("A" & i).Resize(, 7).Borders(xlEdgeBottom).LineStyle = xlContinuous
        End If
        
    Next i

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?