![見出し画像](https://assets.st-note.com/production/uploads/images/152079174/rectangle_large_type_2_f5461f2605d3aab9725b8dc16bd030a0.png?width=1200)
四角で囲まれた文章を隣列に移動して削除するよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、エクセルのセル内にある四角(■)で囲まれた文章を見つけて、その文章を隣の列に移動し、元のセルからは削除するという処理を行います。具体的な動作の流れを以下に説明します。
プロシージャの流れ
画面更新を停止
プロシージャが実行されている間、エクセル画面の更新を止めることで処理を速くします。
正規表現オブジェクトを作成
文字列のパターンを見つけるための「正規表現」オブジェクトを作成します。この正規表現を使って、四角(■)で囲まれた部分を見つけます。
正規表現の設定
`.Pattern = "■■■■■■■■■■([\s\S]?)■■■■■■■■■■"`*
このパターンでは、*「■が10個」で始まり、その間にあるどんな文字(改行含む)でも、「■が10個」*で終わる部分を見つけます。
選択範囲の各セルに対する処理
選択されたセルの中で、四角で囲まれた文章を見つけ、その文章を隣のセルに移動します。見つけた部分は元のセルから削除されます。
結果の反映
最後に、エクセル画面の更新を再開し、結果を表示します。
具体的な動作例
例えば、セルに「これは普通のテキスト■■■■■■■■■■ここは移動■■■■■■■■■■です」と書かれていたとします。このプロシージャを実行すると、「ここは移動」の部分が隣のセルに移動し、元のセルには「これは普通のテキストです」と残ります。
Sub 四角で囲まれた文章を隣列に移動して削除するよ()
Application.ScreenUpdating = False
Dim reg
Set reg = CreateObject("VBScript.RegExp") 'オブジェクト作成
Dim myRng As Range
Dim txt As String
Dim i As Long
Dim match As Object
With reg
.Pattern = "■■■■■■■■■■([\s\S]*?)■■■■■■■■■■"
.IgnoreCase = True
.Global = True
End With
On Error Resume Next
For Each myRng In Selection
txt = myRng.Value
Set match = reg.Execute(txt)
If match.count > 0 Then
myRng.Offset(0, 1).Value = match(0).Value ' マッチしたテキストを隣の列に出力
End If
txt = reg.Replace(txt, "")
myRng.Value = txt
Next myRng
Application.ScreenUpdating = True
End Sub
Excel VBA リファレンス | Microsoft Learn
この記事のYouTube動画はこちら
キーワード
#VBA #Excelマクロ #正規表現 #テキスト操作 #セル操作 #オートメーション #作業効率化 #プログラミング初心者 #エクセル機能 #データ処理 #マクロ活用 #コード解説 #VBA学習 #オフィス活用 #PCスキル向上 #セル操作 #仕事効率化 #できること #excel #vba
English Translation
Moving and Deleting Text Enclosed in Squares to the Adjacent Column
This explanation is created using ChatGPT.
This VBA procedure scans for text enclosed within squares (■) in selected cells, moves that text to the adjacent column, and removes it from the original cell. Below is an explanation of how it works.
Procedure Flow
Stopping Screen Updating
The procedure pauses Excel’s screen updates during execution to speed up the process.
Creating a Regular Expression Object
It creates a "Regular Expression" object to identify specific patterns in text. Here, it’s used to find sections enclosed within squares (■).
Setting the Regular Expression
`.Pattern = "■■■■■■■■■■([\s\S]?)■■■■■■■■■■"`*
This pattern identifies sections that start with 10 squares, followed by any characters (including line breaks), and end with 10 squares.
Processing Each Selected Cell
It scans each selected cell, finds the enclosed text, moves it to the adjacent column, and removes it from the original cell.
Reflecting the Results
Finally, it resumes screen updating, displaying the results.
Example of Operation
For example, if a cell contains "This is normal text ■■■■■■■■■■Move this part■■■■■■■■■■", after running the procedure, "Move this part" will be moved to the adjacent cell, leaving "This is normal text" in the original cell.
Excel VBA Reference | Microsoft Learn
Watch the YouTube video here
Keywords
#VBA #ExcelMacro #RegularExpression #TextManipulation #CellOperations #Automation #EfficiencyBoost #ProgrammingBasics #ExcelFeatures #DataProcessing #MacroUsage #CodeExplanation #VBALearning #OfficeUtilization #PCSkillEnhancement #CellOperations #WorkEfficiency #ThingsYouCanDo #excel #vba