1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VBScriptでExcelに条件付き書式を設定する

Posted at

今回はVBScriptでExcelに条件付き書式を設定する方法について説明したいと思います。
VBScriptではなくVBAでのやり方はネットでたくさん見つかるのですが、VBScriptはなかなか見つからなかったのでQiitaの記事にしてみました。

指定したシートの全体に「空白セルの場合、背景色をグレーにする」という条件付き書式を設定するSub関数です。

'*********************************************************************
' Purpose: 指定されたシートに空白セルの背景をグレーにする条件付き書式を設定する
' Inputs: excelApp: Excelアプリケーションのオブジェクト
'                   Selectionを利用するために必要になる
'         workSheet: 条件付き書式の設定対象となるシートのオブジェクト
' Returns: なし
'*********************************************************************
Sub BlankToGray(ByRef excelApp, ByRef workSheet)
    workSheet.Cells.Select
    excelApp.Selection.FormatConditions.Add 10
    excelApp.Selection.FormatConditions(excelApp.Selection.FormatConditions.Count).SetFirstPriority
    excelApp.Selection.FormatConditions(1).Interior.ColorIndex = 15
    excelApp.Selection.FormatConditions(1).StopIfTrue = False
End Sub

条件付き書式の定数についてはマイクロソフトのサイトで公開されています。
今回は空白の条件なのでxlBlanksCondition定数の値である10を指定しています。

XlFormatConditionType 列挙 (Excel)

名前 説明
xlAboveAverageCondition 12 平均以上の条件
xlBlanksCondition 10 空白の条件
xlCellValue 1 セルの値
xlColorScale 3 カラー スケール
xlDatabar 4 データバー
xlErrorsCondition 16 エラー条件
xlExpression 2 演算
XlIconSet 6 アイコン セット
xlNoBlanksCondition 13 空白の条件なし
xlNoErrorsCondition 17 エラー条件なし
xlTextString 9 テキスト文字列
xlTimePeriod 11 期間
xlTop10 5 上から 10 個の値
xlUniqueValues 8 一意の値

FormatConditions.Addの詳細についてはFormatConditions.Add メソッド (Excel)を参照ねがいます。

1
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?