ChatGPTが優秀過ぎる件(ほとんどVBA書いてくれる)

この記事でやること

これまで書いたプログラムを、ChatGPTで改善してみました。

ChatGPTとは

OpenAIが2022年11月に公開した人工知能チャットボットです。

Googleアカウントがあれば、簡単にログインできます。

https://chat.openai.com/

これまで書いたVBAを改修してみる

「【Excelネタ】指定した文字の色を変更する」にて頂いたコメントを元に以下の内容に対応してみます。

・色付きイタリック調太字で目立たせる

ChatGPTに以下のような形で、質問とプログラム一式を貼り付けて聞いてみました。

以下のVBAでColorIndexだけでなくイタリック調太字にもしたい。
どう変更したらいい?

Sub ChangeTextColor()
~ 割愛

 

すると以下の回答が来ました。

動かしてみると完璧に出来てました。。

Sub ChangeTextColor()

    '色を変更するテキスト
    Dim ans1 As String
    ans1 = InputBox("色を変更するテキストを入力してください" & vbCr & "半角スペース区切りで複数入力可能", "テキストの指定", "")
    If ans1 = "" Then
        MsgBox "テキストを入力してください"
        End
    End If

    '色の指定
    Dim ans2 As String
    ans2 = InputBox("ColorIndexを入力してください" & vbCr & "例(赤:3、青:5、緑:10)", "色の指定", "3")
    If ans2 = "" Then
        MsgBox "ColorIndexを入力してください"
        End
    End If
    
    Dim MyBoom() As String
    Dim CelLen As String
    Dim MyData As String
    Dim MyDataLen As String
    Dim Poji As String
    Dim I As Integer
    Dim j As Integer
    Dim Cel As Object
    
    MyBoom() = Split(ans1)
    For Each Cel In Selection
        CelLen = Len(Cel) 'セル内の文字数
        For I = 0 To UBound(MyBoom)
            MyData = MyBoom(I)  '検索対象文字
            MyDataLen = Len(MyData) '検索対象文字数

            Poji = InStr(1, Cel.Value, MyData)  '検索してヒットしたら色変更
            If Poji > 0 Then
                Cel.Characters(Poji, MyDataLen).Font.ColorIndex = ans2
                Cel.Characters(Poji, MyDataLen).Font.Italic = True ' イタリック調太字に変更
                Cel.Characters(Poji, MyDataLen).Font.Bold = True
            End If

            For j = 0 To CelLen
                Poji = InStr((Poji + MyDataLen + j), Cel.Value, MyData)  '検索してヒットしたら色変更
                If Poji > 0 Then
                    Cel.Characters(Poji, MyDataLen).Font.ColorIndex = ans2
                    Cel.Characters(Poji, MyDataLen).Font.Italic = True ' イタリック調太字に変更
                    Cel.Characters(Poji, MyDataLen).Font.Bold = True
                End If
            Next

        Next
    Next
    
    MsgBox "完了しました"

End Sub

 

さらに、適当にざっと書いたプログラムだったので以下のお願いもしました。
「上記プログラムの変数名を適切な内容に全て変えてほしい」

すると以下の回答が来ました。素晴らしすぎる。。

Sub ChangeTextColorAndStyle()

    '色とスタイルを変更するテキスト
    Dim targetText As String
    targetText = InputBox("色とスタイルを変更するテキストを入力してください" & vbCr & "半角スペース区切りで複数入力可能", "テキストの指定", "")
    If targetText = "" Then
        MsgBox "テキストを入力してください"
        End
    End If

    '色の指定
    Dim targetColorIndex As String
    targetColorIndex = InputBox("ColorIndexを入力してください" & vbCr & "例(赤:3、青:5、緑:10)", "色の指定", "3")
    If targetColorIndex = "" Then
        MsgBox "ColorIndexを入力してください"
        End
    End If
    
    Dim targetTextList() As String
    Dim textLength As String
    Dim searchText As String
    Dim searchTextLength As String
    Dim position As String
    Dim i As Integer
    Dim j As Integer
    Dim cell As Object
    
    targetTextList() = Split(targetText)
    For Each cell In Selection
        textLength = Len(cell) 'セル内の文字数
        For i = 0 To UBound(targetTextList)
            searchText = targetTextList(i)  '検索対象文字
            searchTextLength = Len(searchText) '検索対象文字数

            position = InStr(1, cell.Value, searchText)  '検索してヒットしたら色変更
            If position > 0 Then
                cell.Characters(position, searchTextLength).Font.ColorIndex = targetColorIndex
                cell.Characters(position, searchTextLength).Font.FontStyle = "Italic Bold"
            End If

            For j = 0 To textLength
                position = InStr((position + searchTextLength + j), cell.Value, searchText)  '検索してヒットしたら色変更
                If position > 0 Then
                    cell.Characters(position, searchTextLength).Font.ColorIndex = targetColorIndex
                    cell.Characters(position, searchTextLength).Font.FontStyle = "Italic Bold"
                End If
            Next

        Next
    Next
    
    MsgBox "完了しました"

End Sub

さらに下部にはこんな解説まで。

 

優秀過ぎる!

最後まで読んでいただきありがとうございました^^

1件のコメント

  • くぼた

    なるほど

コメントを残す

メールアドレスが公開されることはありません。* が付いている欄は必須項目です

CAPTCHA


このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください。