![見出し画像](https://assets.st-note.com/production/uploads/images/151806127/rectangle_large_type_2_8c053fb702f087392a5bd22c2ce1a84d.png?width=1200)
文字化けした「入社」を編集する
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、Excelで選択されたセル範囲に対して、文字化けした「?社」を「入社」に自動で置き換えるものです。
手順の概要
画面更新の停止
処理中の画面更新を停止することで、操作をスムーズに進めます。正規表現オブジェクトの作成
VBScript.RegExp という特別な機能を使って、特定の文字パターンを探す設定を行います。この例では、「?社」という文字列を探す設定をしています。選択されたセルの範囲を繰り返し処理
Excelで選択された範囲の各セルに対して、一つずつ処理を行います。各セルの内容を「txt」という変数に入れ、正規表現を使って「?社」を「入社」に置き換えます。置き換えた内容をセルに反映
置き換えが完了したら、新しい内容をセルに書き戻します。画面更新の再開
最後に、画面の更新を再開して、処理結果が画面に表示されるようにします。
Sub 文字化けした入社を編集する()
Application.ScreenUpdating = False
Dim reg
Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
Dim myRng As Range
Dim txt As String
Dim i As Long
With reg
.Pattern = "\?社"
.IgnoreCase = True
.Global = True
End With
On Error Resume Next
For Each myRng In Selection
txt = myRng.Value
txt = reg.Replace(txt, "入社")
myRng.Value = txt
Next myRng
Application.ScreenUpdating = True
End Sub
これにより、「?社」と表示されてしまう文字化けを一括で修正し、「入社」に戻すことができます。
ハッシュタグ
#excel #できること #vba #入社 #文字化け #セル操作 #範囲選択 #正規表現 #VBScript #RegExp #自動化 #テキスト置換 #効率化 #画面更新 #セル範囲 #データ修正 #エラーハンドリング #VBAチュートリアル #VBAコード #Officeスクリプト
English Translation
Editing Garbled Entry
This explanation is created using ChatGPT.
This VBA procedure automatically replaces garbled text "?社" with "入社" within a selected range of cells in Excel.
Procedure Overview
Stopping Screen Updates
Screen updates are paused to ensure the operation runs smoothly.Creating a Regular Expression Object
A special feature, VBScript.RegExp, is used to configure a pattern search. In this case, it is set to search for the string "?社".Processing Each Selected Cell
The procedure loops through each cell in the selected range. The content of each cell is stored in the variable "txt", and the regular expression is used to replace "?社" with "入社".Reflecting Changes in the Cells
After the replacement, the new content is written back to the cells.Resuming Screen Updates
Finally, screen updates are resumed to display the processed results.
This allows for quick correction of garbled text, changing "?社" back to "入社".