■■■■■■■第3章 04■■■■■■■

■■■■p79プロンプト例

あなたは優秀なExcel VBAのプログラマーです。
次の操作をするExcel VBAのプログラムを書いてください。

・シート内にある、特定の文字列だけを赤色の太字にして強調する 
・強調する文字列は、ダイアログボックスで入力して指定する 
・最後に、該当した文字列の数を、ダイアログボックスで表示する
・マクロ名は「指定した文字列を強調」とする


■■■■p79コード例

Sub 指定した文字列を強調()
    Dim targetStr As String
    Dim ws As Worksheet
    Dim c As Range
    Dim startPos As Long
    Dim foundCount As Long
    
    ' 強調する文字列を入力
    targetStr = InputBox("強調する文字列を入力してください。", "文字列指定")
    If targetStr = "" Then Exit Sub
    
    Application.ScreenUpdating = False
    foundCount = 0
    
    Set ws = ActiveSheet
    
    ' シート内のすべてのセルを対象
    For Each c In ws.UsedRange
        If c.Value <> "" Then
            startPos = 1
            ' セル内で複数回出現する場合も処理
            Do
                startPos = InStr(startPos, c.Value, targetStr, vbTextCompare)
                If startPos > 0 Then
                    With c.Characters(startPos, Len(targetStr)).Font
                        .Color = vbRed
                        .Bold = True
                    End With
                    foundCount = foundCount + 1
                    startPos = startPos + Len(targetStr)
                End If
            Loop While startPos > 0
        End If
    Next c
    
    Application.ScreenUpdating = True
    
    ' 結果を表示
    MsgBox "「" & targetStr & "」は " & foundCount & " 件見つかりました。", vbInformation
End Sub


■■■■p81プロンプト例

次のように仕様を変更してください。

・強調するための書式を「赤い太字」「青い太字」「緑の太字」の3種類から選択する


■■■■p81コード例（上記プロンプトの出力）

Sub 指定した文字列を強調()
    Dim targetStr As String
    Dim colorChoice As String
    Dim chosenColor As Long
    Dim ws As Worksheet
    Dim c As Range
    Dim startPos As Long
    Dim foundCount As Long
    
    ' 強調する文字列を入力
    targetStr = InputBox("強調する文字列を入力してください。", "文字列指定")
    If targetStr = "" Then Exit Sub
    
    ' 書式の選択
    colorChoice = InputBox( _
        "書式を選択してください。" & vbCrLf & _
        "1：赤い太字" & vbCrLf & _
        "2：青い太字" & vbCrLf & _
        "3：緑の太字", _
        "書式選択", "1")
    
    Select Case colorChoice
        Case "1": chosenColor = vbRed
        Case "2": chosenColor = vbBlue
        Case "3": chosenColor = vbGreen
        Case Else
            MsgBox "正しい番号を入力してください。", vbExclamation
            Exit Sub
    End Select
    
    Application.ScreenUpdating = False
    foundCount = 0
    
    Set ws = ActiveSheet
    
    ' シート内のすべてのセルを対象
    For Each c In ws.UsedRange
        If c.Value <> "" Then
            startPos = 1
            ' セル内で複数回出現する場合も処理
            Do
                startPos = InStr(startPos, c.Value, targetStr, vbTextCompare)
                If startPos > 0 Then
                    With c.Characters(startPos, Len(targetStr)).Font
                        .Color = chosenColor
                        .Bold = True
                    End With
                    foundCount = foundCount + 1
                    startPos = startPos + Len(targetStr)
                End If
            Loop While startPos > 0
        End If
    Next c
    
    Application.ScreenUpdating = True
    
    ' 結果を表示
    MsgBox "「" & targetStr & "」は " & foundCount & " 件見つかりました。", vbInformation
End Sub


■■■■p83プロンプト例

色を選択するダイアログボックスでは、「書式を番号で選択してください。」と表示する


■■■■p83コード例（上記プロンプトの出力）

Sub 指定した文字列を強調()
    Dim targetStr As String
    Dim colorChoice As String
    Dim chosenColor As Long
    Dim ws As Worksheet
    Dim c As Range
    Dim startPos As Long
    Dim foundCount As Long
    
    ' 強調する文字列を入力
    targetStr = InputBox("強調する文字列を入力してください。", "文字列指定")
    If targetStr = "" Then Exit Sub
    
    ' 書式の選択
    colorChoice = InputBox( _
        "書式を番号で選択してください。" & vbCrLf & _
        "1：赤い太字" & vbCrLf & _
        "2：青い太字" & vbCrLf & _
        "3：緑の太字", _
        "書式選択", "1")
    
    Select Case colorChoice
        Case "1": chosenColor = vbRed
        Case "2": chosenColor = vbBlue
        Case "3": chosenColor = vbGreen
        Case Else
            MsgBox "正しい番号を入力してください。", vbExclamation
            Exit Sub
    End Select
    
    Application.ScreenUpdating = False
    foundCount = 0
    
    Set ws = ActiveSheet
    
    ' シート内のすべてのセルを対象
    For Each c In ws.UsedRange
        If c.Value <> "" Then
            startPos = 1
            ' セル内で複数回出現する場合も処理
            Do
                startPos = InStr(startPos, c.Value, targetStr, vbTextCompare)
                If startPos > 0 Then
                    With c.Characters(startPos, Len(targetStr)).Font
                        .Color = chosenColor
                        .Bold = True
                    End With
                    foundCount = foundCount + 1
                    startPos = startPos + Len(targetStr)
                End If
            Loop While startPos > 0
        End If
    Next c
    
    Application.ScreenUpdating = True
    
    ' 結果を表示
    MsgBox "「" & targetStr & "」は " & foundCount & " 件見つかりました。", vbInformation
End Sub


