見出し画像

受動喫煙対策があったらセルを青色にするよ

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

このVBAプロシージャは、選択されたセルの内容に「受動喫煙対策」が含まれているかを調べ、特定の条件に一致する場合、そのセルの背景色を青色に変更するものです。コードの中身を詳しく見てみましょう。


1. 画面更新の停止

最初に Application.ScreenUpdating を False に設定して、処理が終わるまで画面の更新を一時的に停止します。これにより、処理速度が向上し、画面のちらつきを防ぎます。

2. 正規表現オブジェクトの作成

次に、CreateObject("VBScript.RegExp") で正規表現を扱うオブジェクトを作成しています。このオブジェクトを使って、特定のパターンに一致するかどうかをチェックします。

3. 正規表現のパターン設定

reg.Pattern によって、「受動喫煙対策:あり」などの文字列を探すためのパターンを設定しています。このパターンは、「受動喫煙対策」の後に「:」「:」「あり」「有り」「有」「なし」「無し」「無」のいずれかが続く形式を対象にしています。

4. セルの内容をチェック

For Each myRng In Selection で、選択されたすべてのセルを順番に確認します。セルの内容を txt という変数に取り込み、reg.Test(txt) を使ってその内容が正規表現パターンに一致するかどうかをチェックします。

5. セルの背景色を青色に変更

条件に一致する場合、そのセルの背景色を ColorIndex = 5 で青色に変更します。

6. 画面更新の再開

最後に、Application.ScreenUpdating = True で画面の更新を再開し、処理が終了します。


このプロシージャの用途

このコードは、例えばExcelシート上で受動喫煙対策の有無に関するデータを視覚的に識別したい場合に便利です。特定のセルを青色に変更することで、対策の有無がひと目で分かるようになります。


Excel VBA リファレンス | Microsoft Learn
この記事のYouTube動画はこちら


#excel #できること #vba #受動喫煙対策 #セルの色 #正規表現 #データ整理 #業務効率化 #自動化 #テキスト検索 #エクセルマクロ #初心者向け #プログラミング入門 #オフィスワーク #Excelの使い方 #データ解析 #オートメーション #シート操作 #エクセル #色変更


English Translation


Change Cell Color to Blue If Passive Smoking Measures are Mentioned

This explanation was created using ChatGPT.

This VBA procedure checks the content of the selected cells for mentions of "Passive Smoking Measures" and changes the cell background color to blue if specific conditions are met. Let's take a closer look at how the code works.


1. Stopping Screen Updates

First, Application.ScreenUpdating is set to False to temporarily stop screen updates until the process is complete. This improves processing speed and prevents screen flickering.

2. Creating a Regular Expression Object

Next, CreateObject("VBScript.RegExp") creates an object to handle regular expressions. This object is used to check if the cell content matches a specific pattern.

3. Setting the Regular Expression Pattern

reg.Pattern is used to set a pattern that searches for strings like "Passive Smoking Measures: Yes" and other variations. This pattern targets formats where "Passive Smoking Measures" is followed by symbols like ":" or ":", and words like "Yes", "No", or similar terms.

4. Checking Cell Content

The For Each myRng In Selection loop goes through all selected cells. The content of each cell is stored in a variable called txt, and reg.Test(txt) is used to check if the content matches the regular expression pattern.

5. Changing the Cell Background Color to Blue

If the condition is met, the background color of the cell is changed to blue with ColorIndex = 5.

6. Resuming Screen Updates

Finally, Application.ScreenUpdating = True is set to resume screen updates, marking the end of the process.


Purpose of This Procedure

This code is useful for quickly identifying whether passive smoking measures are mentioned in a dataset on an Excel sheet. By changing specific cells to blue, the presence or absence of such measures can be recognized at a glance.


Excel VBA Reference | Microsoft Learn
YouTube video for this article


#excel #possibilities #vba #passivesmokingmeasures #cellcolor #regex #dataorganization #efficiency #automation #textsearch #excelmacros #beginners #programmingbasics #officework #howtouseexcel #dataanalysis #automation #sheetoperations #excel #colorchange


Total Character Count: 1358 characters.

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