1
0

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 3 years have passed since last update.

Power Automate for desktop「Excelセルの背景に色付け」

Last updated at Posted at 2022-01-30

概要

Power Automate for desktopを利用して何らかの入力作業をおこなうとき、返される結果をExcelに出力することがあります。確認作業がある場合、視認性を高めるため条件によってセルや文字の色を変えたいところです。さまざまな方法がありますが、今回は「VBScriptの実行」アクションを使います。複数起動しているExcelブックから任意のExcelワークシートに色付け処理をします。
cell.gif

環境

  • Windows 10 Pro 21H2
  • Power Automate for desktop 2.16.215.22020
  • Microsoft Excel for Microsoft 365バージョン2112ビルド16.0.14729.20254

VBScript

VBScriptリファレンス

https://docs.microsoft.com/ja-jp/previous-versions/windows/scripting/cc392193(v=msdn.10)

注意等

  • Windows 11では未検証です。
  • あくまでサンプルとご理解ください。
  • 自己責任でお願いします。
  • 2022年1月の情報です。

フロー

Excelを2つ起動し、意図したインスタンス(サンプルフローでは%ExcelInstance2%)に作業します。1~100の乱数を20個出力し、50以上の場合、セル背景に色付けします。

flow

  1. 特別なフォルダーを取得
    01

  2. Excelの起動
    1つめの空ドキュメントとして%ExcelInstance1%とします。
    02

  3. Excelの起動
    2つめの空ドキュメントとして%ExcelInstance2%とします。
    03

  4. Loop
    今回は1~21まで20回ループします。
    04

  5. 乱数の発生
    1~100の乱数を発生させます。
    05

  6. Excelワークシートに書き込み
    乱数%RandomNumber%を%ExcelInstance2%のA列に書き込みます。
    06

  7. If
    %RandomNumber%が50のときVBScriptを実行するようにします。
    07

  8. VBScriptの実行
    GetObjectで取得するオブジェクトのファイル名を指定します。
    vbsimage.png

    VBScript
    Option Explicit
    '変数宣言
    Dim XlApp
    Dim XlSheet
    Dim XlCells
    
    '取得するオブジェクトにBook2を指定
    Set XlApp = GetObject("Book2").Application
    '1番目のシートを指定
    Set XlSheet = XlApp.Worksheets(1)
    'セルを指定(A列のPAD変数LoopIndex行目)
    Set XlCells = XlSheet.Cells(%LoopIndex%,"A")
    'セルに色付け
    XlCells.Interior.Color = RGB(53, 184, 41)
    
    '変数クリア
    Set XlCells = Nothing
    Set XlSheet = Nothing
    Set XlApp = Nothing
    
  9. End
    If節のEnd

  10. End
    LoopのEnd

  11. Excelを閉じる
    ひとつめのExcelインスタンスを終了
    image.png

  12. Excelを閉じる
    ふたつめのExcelインスタンスを終了
    image.png

まとめ

セルの背景に色付けできました。しかしループごとに処理を行うのはいまいちです。そこで次回は列に条件書式を設定します。
今更VBScriptとは思いますが、Power Automate for desktopの標準アクションではできない部分をカバーする方法のひとつとしてよさそうです。
ただ・・VBScriptリファレンスがなかなか見つからなくて困りました。いつまで使えるのか気になるところです・・・。

参考

Docs

VBA

GetObject

https://docs.microsoft.com/ja-jp/office/vba/language/reference/user-interface-help/getobject-function

ワークシートオブジェクト

https://docs.microsoft.com/ja-jp/office/vba/api/excel.worksheet

VS Codeの機能拡張

VBScriptを書くときに以下の機能拡張を使いました。

Code Runner

VS CodeからVBScriptが実行できます。

https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

VBScript Extension for Visual Studio Code

入力補助機能。RGB Colorの指定もできます。

https://marketplace.visualstudio.com/items?itemName=Serpen.vbsvscode

1
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?