Excelシート内の指定した文字列の色と太さを一括変更する

Excelシート内の特定の文字列のみ一括で色や太さを強調したいときがある（かもしれない）。
そういうときこそ、ユーザフォームの出番である。
![ChangeWeightColorForm.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/146265/6f28e6be-4600-0931-37c7-7ee86b8204e2.png)

- ［フォーム名］`ChangeWeightColorForm`
- ［対象文字列テキストボックス名］`TextBox1`
- ［文字の色コンボボックス名］`ColorComboBox`
- ［文字の太さコンボボックス名］`WeightComboBox`
- ［対象範囲コンボボックス名］`RangeComboBox`
- ［OKボタン名］`OKButton`
- ［キャンセルボタン名］`CancelButton`

というフォームを作り、以下のコードを記述する。

**＜フォーム内のVBAコード＞**

```vb:ChangeWeightColorForm.frm
Option Explicit

Private Sub OKButton_Click()

    Dim sh      As Worksheet
    Dim allCnt  As Long         '// 変更した件数
    Dim cnt     As Long         '// 変更した件数のカウンタ

    If TextBox1.Value = "" Then
        MsgBox "対象文字列を入力してください。"
        Exit Sub
    End If

    Unload ChangeWeightColorForm
    
    Application.ScreenUpdating = False
    
        If RangeComboBox.Value = "ブック全体" Then
            For Each sh In Worksheets
                cnt = ChangeFont(sh.UsedRange, _
                                 TextBox1.Value, _
                                 GetColorLong(ColorComboBox.Value), _
                                 GetWeight(WeightComboBox.Value))
                allCnt = allCnt + cnt
            Next sh
        Else
            allCnt = ChangeFont(GetTargetRange(RangeComboBox.Value), _
                                TextBox1.Value, _
                                GetColorLong(ColorComboBox.Value), _
                                GetWeight(WeightComboBox.Value))
        End If
    
    Application.ScreenUpdating = True
    
    MsgBox allCnt & " 件変更しました。"
    
End Sub
```

```vb
Private Sub CancelButton_Click()

    Unload ChangeWeightColorForm

End Sub
```

```vb
Private Sub UserForm_Initialize()

    With ColorComboBox
        .AddItem "赤"
        .AddItem "青"
        .AddItem "緑"
        .AddItem "紫"
        .AddItem "茶"
        .AddItem "黒"
        .AddItem "白"
    End With
    
    With WeightComboBox
        .AddItem "普通"
        .AddItem "太字"
    End With
    
    With RangeComboBox
        .AddItem "ブック全体"
        .AddItem "シート全体"
        .AddItem "選択したセル"
    End With
    
End Sub
```

**＜文字列の色と太さを変更するモジュールのVBAコード＞**

```vb:FontWeightColorModule.bas
Option Explicit

Sub 指定した文字列の色と太さを変更()

    CangeWeightColorForm.Show

End Sub
```

```vb
Function GetColorLong(ByVal key As String) As Long

    Dim coll As Collection

    Set coll = New Collection
    
    With coll
        .Add RGB(255, 0, 0), "赤"
        .Add RGB(0, 0, 255), "青"
        .Add RGB(0, 122, 0), "緑"
        .Add RGB(112, 48, 160), "紫"
        .Add RGB(97, 37, 35), "茶"
        .Add RGB(0, 0, 0), "黒"
        .Add RGB(255, 255, 255), "白"
    End With

    GetColorLong = coll(key)

End Function
```

```vb
Function GetWeight(ByVal boldStr As String) As Boolean

    If boldStr = "太字" Then
        GetWeight = True
    Else
        GetWeight = False
    End If

End Function
```

```vb
Function GetTargetRange(ByVal rangeStr As String) As Range

    Select Case rangeStr
        Case "シート全体"
            Set GetTargetRange = GetActiveSheet.UsedRange
        Case "選択したセル"
            Set GetTargetRange = Selection
    End Select

End Function
```

```vb
Function ChangeFont(target_range As Range, ByVal str As String, _
                    ByVal f_color As Long, ByVal fBold As Boolean) As Long

    Dim rng         As Range
    Dim strLen      As Long
    Dim strPoint    As Long
    Dim cnt         As Long
    
    strLen = Len(str)
    
    For Each rng In target_range
        With rng
            Do
                strPoint = InStr(strPoint + 1, .Value, str)
                If strPoint > 0 Then
                    With .Characters(Start:=strPoint, Length:=strLen).Font
                        .Bold = fBold
                        .Color = f_color
                        cnt = cnt + 1
                    End With
                End If
            Loop While strPoint > 0
        End With
        strPoint = 0
    Next rng

    Range("A1").Select
    
    ChangeFont = cnt

End Function
```
ADINに入れておくと便利。