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?

More than 3 years have passed since last update.

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

Last updated at Posted at 2022-01-04

Excelシート内の特定の文字列のみ一括で色や太さを強調したいときがある(かもしれない)。
そういうときこそ、ユーザフォームの出番である。
ChangeWeightColorForm.png

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

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

<フォーム内のVBAコード>

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
Private Sub CancelButton_Click()

    Unload ChangeWeightColorForm

End Sub
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コード>

FontWeightColorModule.bas
Option Explicit

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

    CangeWeightColorForm.Show

End Sub
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
Function GetWeight(ByVal boldStr As String) As Boolean

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

End Function
Function GetTargetRange(ByVal rangeStr As String) As Range

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

End Function
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に入れておくと便利。

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?