セルの値を置換・書式設定(Replace メソッド,ReplaceFormat プロパティ)
2015/10/19
セルの値の置換と書式設定
検索条件に一致するセルの書式を設定するには、まず、Application オブジェクト の ReplaceFormat プロパティを使用して、設定する書式を指定します。そして、Replace メソッドを、引数 replaceformat に True を設定し、実行します。
この場合、置換する文字列を設定する、引数 replacement に「””」(空白文字列)を指定すると、値の置換は行われず、書式のみ変更されます。
書式を定義するには、Borders プロパティ、Font プロパティ、または Interior プロパティ を使用します。
Replace メソッド
object.Replace ( what, replacement [, replaceformat ] )
引数・戻り値
- object ・・・ 対象となる Range オブジェクトを指定します。
- what ・・・ 検索する文字列、値など、セル内のデータに該当する値を指定します。省略不可。
- replacement ・・・ 置き換える文字列を指定します。省略不可。
- replaceformat ・・・ 書式を設定する場合、True を設定します。
- 戻り値 ・・・ ブール型(Boolean)の値(基本的に、True )を返します。
検索・置換条件をさらに細かく指定する方法については、セルの値を置換(Replace メソッド) を参照してください。
ReplaceFormat プロパティ
object.ReplaceFormat
引数・戻り値
- object ・・・ 対象となる Application オブジェクトを指定します。
- 戻り値 ・・・ 置換後の書式を設定する CellFormat オブジェクト。(CellFormat 型)
ReplaceFormat.Clear メソッド
FindFormat プロパティ 同様、、書式設定する前に、Clear メソッド を使って初期化をします。これをしないと前に設定した条件が重なり、意図した結果にならない場合があります。
(例) Application.ReplaceFormat.Clear
Replace メソッド,ReplaceFormat プロパティ 使用例 1
サンプルVBAソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Sub Sample01_ReplaceFormat() '置換後の書式を設定(セル背景色を変更) With Application.ReplaceFormat .Clear .Interior.Color = rgbChartreuse End With '「EEE」を「AAA」に変更し、書式も変更する Cells.Replace _ What:="EEE", _ Replacement:="AAA", _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ MatchCase:=False, _ MatchByte:=False, _ SearchFormat:=False, _ ReplaceFormat:=True '置換後の書式を設定(フォントの太さ、色を変更) With Application.ReplaceFormat .Clear .Font.Bold = True .Font.Color = rgbCrimson End With '「広」から始まる文字列に当てはまるセルの書式を変更 ' 引数 Replacement に、""(空文字列)を指定(書式のみの変更で、文字列置換は行わない) Cells.Replace _ What:="広*", _ Replacement:="", _ LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ MatchCase:=False, _ MatchByte:=False, _ SearchFormat:=False, _ ReplaceFormat:=True End Sub |
実行結果 1
サンプルVBAソース 実行前
実行後
Replace メソッド,ReplaceFormat プロパティ 使用例 2
サンプルVBAソース
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Sub Sample02_ReplaceFormat() '検索する書式の設定 With Application.FindFormat .Clear .Font.Color = rgbRed End With '置換後の書式を設定(フォントの太さ、色を変更) With Application.ReplaceFormat .Clear .Font.Bold = True .Font.Name = "AR Pゴシック体S" .Font.Size = 16 .Interior.Color = rgbPowderBlue End With '書式を置換 Cells.Replace _ What:="*", _ Replacement:="", _ LookAt:=xlWhole, _ SearchFormat:=True, _ ReplaceFormat:=True End Sub |
実行結果 2
サンプルVBAソース 実行前
実行後
関連記事
-
-
A1参照形式、R1C1参照形式の設定・取得(ReferenceStyle プロパティ)
ReferenceStyle プロパティ ReferenceStyle プロパテ …
-
-
使用中のセル範囲を取得(CurrentRegion プロパティ)
CurrentRegion プロパティ CurrentRegion プロパティは …
-
-
対象のセルを直接参照しているセル全てを取得(DirectDependents プロパティ)
DirectDependents プロパティ DirectDependents …
-
-
任意のセルを含む行または列全体を参照(EntireRow プロパティ,EntireColumn プロパティ)
EntireRow プロパティ・EntireColumn プロパティ Entir …
-
-
行・列の表示・非表示(Hidden プロパティ)
Hidden プロパティ Hidden プロパティに True を設定することで …
-
-
セル範囲を結合・結合を解除(Merge メソッド・UnMerge メソッド)
Merge メソッド・UnMerge メソッド セルを結合するには、指定したセル …
-
-
セル・セル範囲を切り取る(Cut メソッド)
Cut メソッド Cut メソッドは、指定したセル範囲を切り取り、引数に指定した …
-
-
引数に指定したセル範囲の共通のセル範囲を取得(Intersect メソッド)
Intersect メソッド Intersect メソッドは、引数に指定したセル …
-
-
文字の向きを表す値を取得・設定する(Orientation プロパティ)
Orientation プロパティ Orientation プロパティで、文字列 …
-
-
外枠罫線の設定(BorderAround メソッド)
外枠罫線の設定 罫線の設定には、Borders プロパティを使用しますが、範囲内 …