
試用期間3パターンを隣列にだすよ
この説明は、ChatGPTで作成しています。
このVBAプロシージャは、選択したセルの内容から特定の期間(ヶ月、週間、日間)を見つけて、その見つけた期間を隣の列に表示するものです。
仕組みの説明
画面更新を停止:コードの実行中に画面の更新を停止して、処理速度を速くします。
```Application.ScreenUpdating = False```正規表現オブジェクトの作成:
VBScript.RegExp というオブジェクトを作成して、正規表現のパターンを設定します。
```Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
```正規表現のパターン設定:
* \d{1,2}(ヶ月|週間) | \d{1,3}日間* というパターンを設定します。このパターンは、1~2桁の数字の後に「ヶ月」または「週間」、または1~3桁の数字の後に「日間」が続くテキストにマッチします。
```With reg
.Pattern = "\d{1,2}(ヶ月|週間)|\d{1,3}日間"
.IgnoreCase = True
.Global = True
End With
```選択範囲のセルを順にチェック:
選択したセル範囲を順番にチェックし、正規表現にマッチするテキストがあれば、それを隣の列に表示します。
```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
Next myRng
```画面更新を再開:
最後に、画面更新を再開します。
```Application.ScreenUpdating = True
```
これにより、例えばセルに「3ヶ月」と書かれている場合、その「3ヶ月」が隣の列に表示されます。
ハッシュタグ
#excel #できること #vba #正規表現 #期間抽出 #日付操作 #データ処理 #エクセル #自動化 #業務効率化 #プログラミング #スクリプト #マクロ #初心者向け #簡単操作 #学習 #ビジネスツール #関数 #データ分析 #作業効率化
Explanation in English
Display Probation Periods in Adjacent Column
This explanation is created by ChatGPT.
This VBA procedure takes content from the selected cells, identifies specific periods (such as months, weeks, or days), and displays the found periods in the adjacent column.
How It Works
Stop Screen Updating: Stops the screen from updating during code execution to speed up the process.
```vba
Application.ScreenUpdating = False
```Create Regular Expression Object: Creates a VBScript.RegExp object and sets up the regular expression pattern.
```vba
Dim reg As Object
Set reg = CreateObject("VBScript.RegExp")
```Set Regular Expression Pattern: The pattern \d{1,2}(ヶ月|週間) | \d{1,3}日間 matches text containing 1 to 2 digits followed by "months" or "weeks", or 1 to 3 digits followed by "days".
```vba
With reg
.Pattern = "\d{1,2}(ヶ月|週間)|\d{1,3}日間"
.IgnoreCase = True
.Global = True
End With
```Check Each Selected Cell: Iterates through each cell in the selected range, and if the text matches the pattern, displays the match in the adjacent column.
```vba
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
Next myRng
```Resume Screen Updating: Finally, resumes screen updating.
```vba
Application.ScreenUpdating = True
```
As a result, if a cell contains "3 months", this "3 months" will be displayed in the adjacent column.
Hashtags
#excel #できること #vba #regular_expression #extract_period #date_operations #data_processing #excel #automation #business_efficiency #programming #script #macro #for_beginners #easy_operations #learning #business_tool #function #data_analysis #task_efficiency