見出し画像

表の1行おきに色付け。もう一度実行すると元に戻るChatGPTと一緒に作ったマクロの例

この説明は、ChatGPTで作成しています。

このVBAコードは、表の偶数行(2行目以降の1行おき)に色を付ける、または色を消す機能を持っています。コードを実行するたびに、現在の状態に応じて色をつけるか消すかを自動で判断してくれます。


コードの仕組み

  1. 最初の設定

    • *「画面の更新を止める」*操作で、コードの実行中に画面がちらつくのを防ぎます。

    • *「アクティブシートの指定」*で、作業中のシートを自動で選択して、そこにあるデータの行や列を把握します。

  2. 最終行と最終列の取得

    • `lastCol`変数で表の最終列、`lastRow`で最終行を自動取得します。

    • この情報を使って、表の範囲を柔軟に操作します。

  3. 色が既についているかの確認

    • 偶数行目(2, 4, 6, …)に色がついているかを、1行目の列1のセルの色から確認します。

    • 色がついていたら、コード実行で「色を消す」動き、ついていなければ「色をつける」動きに進みます。

  4. 色のつけ外し

    • 色がついているときは色をリセットします。

    • 色がついていないときは*黄色(ColorIndex = 6)*を偶数行にだけ付けます。色番号の参考は、Microsoft Learnのリンクから確認できます。


注意

色を変更したい場合は、`ColorIndex = 6`を他の番号に変えることで、任意の色に変更できます。
ColorIndex プロパティ (Excel グラフ) | Microsoft Learn


Excel VBA リファレンス | Microsoft Learn
Excelアイコンは、Icons8が作成したものです
この記事のYouTube動画はこちら

Sub 表の1行おきに色付け。もう一度実行すると元に戻るChatGPTと一緒につくったよ()
    Application.ScreenUpdating = False
    Dim lastCol As Long, lastRow As Long, i As Long
    Dim ws As Worksheet
    Dim hasColor As Boolean
    Set ws = ActiveSheet

    ' アクティブシートのカラム数と最終行数を取得
    lastCol = ws.Cells(1, ws.Columns.count).End(xlToLeft).Column
    lastRow = ws.Cells(ws.Rows.count, 1).End(xlUp).row
    
    ' 2行目から最終行までの1行おきに色がついているか確認
    hasColor = False
    For i = 2 To lastRow Step 2
        If ws.Cells(i, 1).Interior.ColorIndex <> xlNone Then
            hasColor = True
            Exit For
        End If
    Next i
    
    ' 色がついていたらリセット、ついていなければ1行おきに色をつける
    If hasColor Then
        ws.Range(ws.Cells(2, 1), ws.Cells(lastRow, lastCol)).Interior.ColorIndex = xlNone
    Else
        For i = 2 To lastRow Step 2
            ws.Range(ws.Cells(i, 1), ws.Cells(i, lastCol)).Interior.ColorIndex = 6 ' イエロー※詳細
        Next i
    End If

    Application.ScreenUpdating = True
    '※色を変更するときは下記URLwebサイトにある注釈をみてください(色の番号がわかります)
    'ColorIndex プロパティ (Excel グラフ) | Microsoft Learn
    'https://learn.microsoft.com/ja-jp/office/vba/api/excel.colorindex
End Sub

#excel #できること #vba #excel自動化 #業務効率化 #表作成 #色付け #偶数行色付け #VBA初心者 #セル操作 #アクティブシート #最終行取得 #最終列取得 #行ごとに色付け #ループ処理 #条件分岐 #色リセット #カラーインデックス #MicrosoftLearn #プログラミング初心者


Translation (English)

Alternating Row Color Highlighting. Running the Code Again Reverts to Original State

This explanation is generated by ChatGPT.

This VBA code is designed to apply or remove color highlighting to every other row (starting from row 2) in a table. Each time you run the code, it automatically decides whether to add or remove the color based on the current state.

How the Code Works

  1. Initial Setup

    • Screen updating is turned off to prevent flickering during execution.

    • The code selects the active sheet automatically, determining the number of rows and columns in the table.

  2. Retrieve Last Row and Column

    • The `lastCol` variable finds the last column, and `lastRow` finds the last row of the table.

    • This flexibility allows the code to adjust to the table’s size.

  3. Check for Existing Color

    • The code checks every other row (2, 4, 6, …) to see if any of these rows already have color applied.

    • If color is detected, the code will remove it upon running; if not, it will add color.

  4. Add or Remove Color

    • If color exists, it’s reset to no color.

    • If there’s no color, yellow (`ColorIndex = 6`) is applied to the even rows. Refer to Microsoft Learn for more color options.

Note

To change colors, modify `ColorIndex = 6` to another index number of your choice.


Excel VBA Reference | Microsoft Learn
Excel icon by Icons8
YouTube video for this article

#excel #features #vba #excelAutomation #efficiency #tableSetup #colorHighlighting #evenRowsHighlight #VBABasics #cellOperations #activeSheet #lastRow #lastColumn #rowHighlight #looping #conditionalLogic #colorReset #colorIndex #MicrosoftLearn #beginnerProgramming

いいなと思ったら応援しよう!