曜日の部分をカッコつきの省略にするよ
この説明は、ChatGPTで作成しています。
このプロシージャは、Excelのセル内にある曜日の文字列を探して、その部分を括弧(カッコ)で囲むものです。たとえば、「月曜日」という文字があれば、それを「(月)」に変えるという動きをします。
ポイント:
画面の更新を停止
Application.ScreenUpdating = False は、処理中に画面のチラつきを防ぐために、Excelの画面更新を一時的に停止するコードです。処理が終わった後に True に戻します。正規表現オブジェクトの作成
Set reg = CreateObject("VBScript.RegExp") は、文字列のパターンを探して変更するための「正規表現オブジェクト」を作成しています。パターンの設定
reg.Pattern = "(月|火|水|木|金|土|日)曜日" は、探したい文字列のパターンを設定しています。このパターンは「月曜日」から「日曜日」までの文字列を探し出します。ループでセルを処理
For Each myRng In Selection で、選択されたセル範囲を一つずつ取り出して処理します。それぞれのセル内にある文字列から、「月曜日」や「火曜日」などを括弧で囲んだ形式に変換します。文字列の変換
txt = reg.Replace(txt, "($1)") で、見つかった曜日の部分を括弧で囲むように文字列を変換します。
このプロシージャを実行すると、選択したセルの中にある曜日が括弧で囲まれた形式に自動で変換されます。
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, "($1)")
myRng.Value = txt
Next myRng
Application.ScreenUpdating = True
End Sub
Excel VBA リファレンス | Microsoft Learn
https://learn.microsoft.com/ja-jp/office/vba/api/overview/excel
この記事のYouTube動画はこちら
キーワード
#excel #できること #vba #曜日 #正規表現 #RegExp #括弧 #セル操作 #テキスト変換 #スクリーンアップデート #セル範囲 #文字列操作 #プログラミング #VBAマクロ #初心者向け #日付操作 #セルの書式 #日付フォーマット #Excel自動化 #エクセル操作
English Translation:
Modify Day Names with Parentheses
This explanation is generated by ChatGPT.
This procedure modifies the day names in selected Excel cells by surrounding them with parentheses. For example, if a cell contains "Monday," it changes it to "(Mon)."
Key Points:
Stop Screen Updates
Application.ScreenUpdating = False temporarily stops Excel from updating the screen to prevent flickering during processing.Create a Regular Expression Object
Set reg = CreateObject("VBScript.RegExp") creates a "regular expression object" that searches for and modifies specific string patterns.Define the Pattern
reg.Pattern = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun)" defines the pattern to search for in the cell contents, specifically looking for day names like "Monday."Process Each Cell in the Selection
The For Each myRng In Selection loop goes through each selected cell, changing day names like "Monday" into "(Mon)."Replace Text
txt = reg.Replace(txt, "($1)") changes the matched day names to be enclosed in parentheses.
When you run this procedure, it automatically modifies the day names in the selected cells to be in the format with parentheses.
Excel VBA Reference | Microsoft Learn
https://learn.microsoft.com/en-us/office/vba/api/overview/excel
Watch the YouTube video here
https://youtu.be/PEFJOws1b30
Keywords
#excel #whatyoucando #vba #daynames #regex #RegExp #parentheses #cellmanipulation #textconversion #screenupdating #cellrange #stringmanipulation #programming #VBAmacros #beginners #datehandling #cellformatting #dateformat #excelautomation #excelskills
この記事が気に入ったらサポートをしてみませんか?