0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【VBA】エクセルで秀丸の強調表示を利用する(C言語)

Last updated at Posted at 2019-10-28

一旦完了

main
Sub main()
    Call 初期色付
    Call 関数強調
    Call シンタックスハイライト表示
    
    MsgBox "完了"
End Sub
1
Function 初期色付()
    With Worksheets("設定")
        Ir = .Cells(2, 11)
        Ig = .Cells(2, 12)
        Ib = .Cells(2, 13)
        Fr = .Cells(2, 6)
        Fg = .Cells(2, 7)
        Fb = .Cells(2, 8)
    End With
    
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = RGB(Ir, Ig, Ib)
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    With Selection.Font
        .Color = RGB(Fr, Fg, Fb)
        .TintAndShade = 0
    End With
End Function
2
Sub 関数強調()
    With Worksheets("設定")
        r = .Cells(2, 16)
        g = .Cells(2, 17)
        b = .Cells(2, 18)
    End With
    Call 正規表現強調表示("^[_a-zA-Z][^\(=;:]*[ \t]+[_a-zA-Z][_a-zA-Z0-9]*\([^;]*$", RGB(r, g, b))
    
    With Worksheets("設定")
        r = .Cells(3, 16)
        g = .Cells(3, 17)
        b = .Cells(3, 18)
    End With
    Call 正規表現強調表示(".*<.+>", RGB(r, g, b))
End Sub

Function 正規表現強調表示(ByVal pat As String, a_lColor)
    Dim reg, match, match2

    Set reg = CreateObject("VBScript.RegExp")   '// オブジェクト作成
    'pat = "^[_a-zA-Z][^\(=;:]*[ \t]+[_a-zA-Z][_a-zA-Z0-9]*\([^;]*$" '// '検索パターン

    '// 正規表現オブジェクトの設定
    With reg
        .Pattern = pat     '// 正規表現によるパターンを設定
        .IgnoreCase = True '// 大文字と小文字を区別する
        .Global = True      '// 文字列全体を検索する
    End With

    '// 選択セル範囲を1セルずつループ
    For Each v1 In Selection    '// v1:検索対象
        '// 正規表現によるマッチングの実行
        Set match = reg.Execute(CStr(v1))

        '// 実行結果の解析
        For Each match2 In match
            If match2.FirstIndex = 0 Then
                Call 指定文字列のフォント変更(match2, a_lColor, False)
            End If
        Next
    Next
End Function
3
Sub シンタックスハイライト表示()
    i = 2
    With Worksheets("設定")
        Do While Not IsEmpty(.Cells(i, 1))
            a_sSearch = .Cells(i, 2)
            a_lColor = GetColor(.Cells(i, 1))
            Call 指定文字列のフォント変更(a_sSearch, a_lColor, False)
            i = i + 1
        Loop
    End With
End Sub

Function GetColor(ByVal ColorCode As String) As String
    i = 2
    a_lColor = RGB(248, 248, 242)
    
    With Worksheets("設定")
        Do While Not IsEmpty(.Cells(i, 5))
            If ColorCode = .Cells(i, 5) Then
                r = .Cells(i, 6)
                g = .Cells(i, 7)
                b = .Cells(i, 8)
                a_lColor = RGB(r, g, b)
                Exit Do
            End If
            i = i + 1
        Loop
    End With
    
    GetColor = a_lColor
End Function
設定
Sub 背景コード()
    With Worksheets("設定")
        Ir = .Cells(2, 11)
        Ig = .Cells(2, 12)
        Ib = .Cells(2, 13)
        Fr = .Cells(2, 6)
        Fg = .Cells(2, 7)
        Fb = .Cells(2, 8)
    End With
    
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = RGB(Ir, Ig, Ib)
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    With Selection.Font
        .Color = RGB(Fr, Fg, Fb)
        .TintAndShade = 0
    End With
End Sub


Sub 関数コード()
    With Worksheets("設定")
        Ir = .Cells(2, 11)
        Ig = .Cells(2, 12)
        Ib = .Cells(2, 13)
    End With
    
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = RGB(Ir, Ig, Ib)
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    i = 2
    With Worksheets("設定")
        Do While Not IsEmpty(Cells(i, 15))
            a_sSearch = Cells(i, 15)
            r = .Cells(i, 16)
            g = .Cells(i, 17)
            b = .Cells(i, 18)
            a_lColor = RGB(r, g, b)
            Call 指定文字列のフォント変更(a_sSearch, a_lColor, False)
            i = i + 1
        Loop
    End With
End Sub

Sub 文字コード()
    With Worksheets("設定")
        Ir = .Cells(2, 11)
        Ig = .Cells(2, 12)
        Ib = .Cells(2, 13)
    End With
    
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = RGB(Ir, Ig, Ib)
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    
    
    i = 2
    With Worksheets("設定")
        Do While Not IsEmpty(Cells(i, 5))
            a_sSearch = Cells(i, 5)
            r = .Cells(i, 6)
            g = .Cells(i, 7)
            b = .Cells(i, 8)
            a_lColor = RGB(r, g, b)
            Call 指定文字列のフォント変更(a_sSearch, a_lColor, False)
            i = i + 1
        Loop
    End With
End Sub

Function 指定文字列のフォント変更(a_sSearch, a_lColor, a_bBold)
    Dim f   As Font     '// Fontオブジェクト
    Dim i               '// 引数文字列のセルの位置
    Dim iLen            '// 引数文字列の文字数
    Dim r   As Range    '// セル範囲の1セル

    iLen = Len(a_sSearch)
    i = 1

    '// 選択セル範囲を1セルずつループ
    For Each r In Selection
        '// 指定されたセルの文字列から引数文字列を全て検索
        Do
            '// セル文字列から引数文字列を検索
            i = InStr(i, r.Value, a_sSearch)

            '// 引数文字列が存在しない場合
            If (i = 0) Then
                '// 次検索用に検索開始位置を1に初期化
                i = 1

                '// このセルの処理を終了
                Exit Do
            End If

            '// 引数文字列部分のFontオブジェクトを取得
            Set f = r.Characters(i, iLen).Font

            '// フォント設定
            f.Color = a_lColor  '// 文字色
            f.Bold = a_bBold    '// 太さ

            '// 次検索用に検索開始位置をずらす
            i = i + 1
        Loop
    Next
End Function

シート:設定
image.png
※文字コードは文字数は小さい順にしておく
 (double ← do を含んでしまう ようなことの対策)

文字ColorCode  R  G  B    背景ColorCode  R  G  B    関数ColorCode  R  G  B
      0  248 248 242         0  39 40 34         0  237 125 49
      33 102 217 239                         1  255 155 255
      97 249 38 114                  
      161 166 226 46


参考
・VBAでセルの指定文字列の色や太さを変更する
・【VBA入門】OpenメソッドでテキストファイルやCSVの読み込み
・文字列マッチング CreateObject("VBScript.RegExp")
・秀丸エディタを黒背景の配色(Monokai風)にカスタマイズする

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?