見出し画像

丁目の漢数字を数値にするChatGPTと一緒につくったやつ

この説明は、ChatGPTで作成しています。

このプロシージャは、Excelのセルにある「一丁目」「二丁目」などの漢数字を数値に変換するものです。具体的には、選択範囲の各セルをチェックし、「一」「二」「三」などの漢数字を対応する数値に置き換えます。

仕組みの説明

  1. 辞書の作成:
    漢数字と対応する数値を辞書に登録します。例えば、「一」は1、「二」は2というように。

```vba
Set kanjiToNum = CreateObject("Scripting.Dictionary")
kanjiToNum.Add "一", 1
kanjiToNum.Add "二", 2
' 他の漢数字も同様に追加
```

2. 正規表現の設定:
「一丁目」「二丁目」などのテキストパターンを見つけるために正規表現を設定します。

```vba
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = False
regex.Pattern = "[一二三四五六七八九十]{1,2}丁目"
```

3.選択範囲のセルをループ:
各セルをチェックし、正規表現に一致するテキストを見つけた場合、そのテキストを処理します。

```vba
For Each cell In Selection
    If regex.Test(cell.Value) Then
        ' 一致するテキストを処理
    End If
Next cell
```

4. 漢数字を数値に変換:
一致したテキスト(例えば「三丁目」)から漢数字を抽出し、それを数値に変換します。

```vba
kanjiText = match.Value
total = 0
tempTotal = 0
For i = 1 To Len(kanjiText)
    c = Mid(kanjiText, i, 1)
    If kanjiToNum.exists(c) Then
        num = kanjiToNum(c)
        If num = 10 Then
            If tempTotal = 0 Then tempTotal = 1
            total = total + tempTotal * num
            tempTotal = 0
        Else
            tempTotal = tempTotal * 10 + num
        End If
    End If
Next i
total = total + tempTotal
```

5.数値に置換:
元のテキストを数値に変換し、セルの内容を更新します。

```vba
cell.Value = Replace(cell.Value, kanjiText, total & "丁目")
```

以上がプロシージャの基本的な流れです。このプロシージャを使うことで、Excelのセルにある漢数字を簡単に数値に変換することができます。


Convert Kanji Street Numbers to Numeric Values

This explanation is created with ChatGPT.

This procedure converts kanji numerals (e.g., "一丁目", "二丁目") in Excel cells to their numeric equivalents. Specifically, it checks each cell in the selected range and replaces kanji numerals with corresponding numbers.

Explanation of the Mechanism

  1. Creating a Dictionary:
    Register kanji numerals and their corresponding numbers in a dictionary. For example, "一" corresponds to 1, "二" corresponds to 2.

```vba
Set kanjiToNum = CreateObject("Scripting.Dictionary")
kanjiToNum.Add "一", 1
kanjiToNum.Add "二", 2
' Add other kanji numerals similarly
```

2.Setting Up Regular Expression:
Set up a regular expression to find text patterns like "一丁目", "二丁目".

```vba
Set regex = CreateObject("VBScript.RegExp")
regex.Global = True
regex.IgnoreCase = False
regex.Pattern = "[一二三四五六七八九十]{1,2}丁目"
```

3.Loop Through Selected Cells:
Check each cell and process the text if it matches the regular expression.

```vba
For Each cell In Selection
    If regex.Test(cell.Value) Then
        ' Process matching text
    End If
Next cell
```

4.Convert Kanji to Numbers:
Extract kanji numerals from the matching text (e.g., "三丁目") and convert them to numbers.

```vba
kanjiText = match.Value
total = 0
tempTotal = 0
For i = 1 To Len(kanjiText)
    c = Mid(kanjiText, i, 1)
    If kanjiToNum.exists(c) Then
        num = kanjiToNum(c)
        If num = 10 Then
            If tempTotal = 0 Then tempTotal = 1
            total = total + tempTotal * num
            tempTotal = 0
        Else
            tempTotal = tempTotal * 10 + num
        End If
    End If
Next i
total = total + tempTotal
```

5.Replace with Numbers:
Convert the original text to numbers and update the cell content.

```vba
cell.Value = Replace(cell.Value, kanjiText, total & "丁目")
```

This is the basic flow of the procedure. By using this procedure, you can easily convert kanji numerals in Excel cells to numbers.

ハッシュタグ

#excel #vba #できること #漢数字 #数値変換 #丁目 #正規表現 #辞書作成 #セルの内容 #テキスト処理 #選択範囲 #自動化 #VBAスクリプト #プログラミング #Excel自動化 #数値置換 #簡単変換 #丁目 #正規表現パターン #ExcelVBA

この記事が気に入ったらサポートをしてみませんか?