[[20240417115433]] 『エクセルマクロVBA|ある特定文字列(複数可)を含ax(香帆) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) |

| 全文検索 | 過去ログ ]

 

『エクセルマクロVBA|ある特定文字列(複数可)を含むセルに色を付ける』(香帆)

Option Explicit
'プログラム0|変数宣言の指定

'プログラム1|プログラム開始
Sub ColorCellsWithKeywords()

    'プログラム2|キーワードを入力
    Dim keywords As String
    keywords = InputBox("対象にしたい文字列を記入。複数ある場合は、「,」で区分けすること")

    'プログラム3|キーワードがない場合、プログラムを終了
    If keywords = "" Then: Exit Sub

    'プログラム4|シート設定
    Dim ws As Worksheet
    Set ws = ActiveSheet

    'プログラム5|対象セル範囲を設定
    Dim myrange As Range
    Set myrange = ws.UsedRange()

    'プログラム6|シート内の全てのセルに対して処理
    Dim cell As Range
    For Each cell In myrange
        Debug.Print cell.Address

        'プログラム7|1行目(ヘッダー)のセルは省略
        If cell.Row = 1 Then: GoTo Continue

        'プログラム8|入力したキーワードを一つずつ処理
        Dim keyword As Variant
        For Each keyword In Split(keywords, ",")

            'プログラム9|対象セルがキーワードを含んでいれば黄色、いなければ色をなくす
            If InStr(cell.Value, keyword) > 0 Then
                cell.Interior.ColorIndex = 6
                Exit For
            Else
                cell.Interior.ColorIndex = xlNone
            End If

        Next

'プログラム10|プログラム7のジャンプ先
Continue:

    Next

'プログラム11|プログラム終了
End Sub
上記のプログラムをInputBoxに検索文字列 A,B,Cを入力した場合は全て、セル内は黄色に色付けされますが、コードを書きかえ上記のプログラムをInputBoxに検索文字列 Aの場合は赤色に、Bの場合は黄色に、Cの場合は緑色にするには
ここのコ-ド

            If InStr(cell.Value, keyword) > 0 Then
                cell.Interior.ColorIndex = 6
                Exit For
            Else
                cell.Interior.ColorIndex = xlNone
            End If

        Next
If〜Else〜End Ifをどの様にコ-ドを書きかえればよいのでしょうか?
どなたか教えて頂けないでしょうか?
お願いいたします。

< 使用 Excel:Excel2019、使用 OS:Windows8 >


カンマ区切りのキーワードの一番目が赤、二番目が黄、三番目が緑 ということでしょうか。
キーワードが4つ以上に入力されることはないのでしょうか。
ある場合は、どうしますか。

(hatena) 2024/04/17(水) 13:12:16


 勝手解釈して、こんなことですか?
 Sub ColorCellsWithKeywords()
     Dim cell  As Range
     Dim v As Variant

     For Each cell In ActiveSheet.UsedRange
         If cell.Row > 1 Then
             v = cell.Value
             Select Case True
                 Case InStr(v, "A") > 0
                     cell.Interior.Color = vbRed
                 Case InStr(v, "B") > 0
                     cell.Interior.Color = vbYellow
                 Case InStr(v, "C") > 0
                     cell.Interior.Color = vbGreen
             End Select
         End If
     Next
 End Sub
 # 変数宣言の位置について、特段の主張をするものではありません。
 # 確認の最中ですみません。折角書いてしまったので、載せておきます。

(xyz) 2024/04/17(水) 13:24:22


 カンマ区切りのキーワードの一番目が赤、二番目が黄、三番目が緑 
 4つ目以降は無視するという仕様の場合は、

        'プログラム8|入力したキーワードを一つずつ処理        
        Dim aryCI As Variant, aryKW As Variant, i As Long
        aryCI = Array(3, 6, 4) '赤:3 黄:6 緑:4
        aryKW = Split(keywords, ",")
        For i=0 to UBound(ary)
            If i > 2 Then Exit For
            If InStr(cell.Value, aryKW(i)) > 0 Then
                cell.Interior.ColorIndex = aryCI(i)
                Exit For
            Else
                cell.Interior.ColorIndex = xlNone
            End If
        Next
(hatena) 2024/04/17(水) 13:31:51

 >     If InStr(cell.Value, keyword) > 0 Then
 >         cell.Interior.ColorIndex = 6
 >         Exit For

 こんな事かな?

       If InStr(cell.Value, keyword) > 0 Then
           Select Case keyword
               Case "A": cell.Interior.Color = RGB(255, 0, 0)
               Case "B": cell.Interior.Color = RGB(255, 255, 0)
               Case "C": cell.Interior.Color = RGB(0, 255, 0)
           End Select               
           Exit For

(半平太) 2024/04/17(水) 14:36:38


コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.