web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / How to set background ...
Power Automate
Answered

How to set background color in Excel by VBScript

(0) ShareShare
ReportReport
Posted on by 73

Hi, All.

 

I try to set background color in Excel by VBScript while I look into the following website.

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

It seems that my flow can get each variable correctly.(cf → Fig.1)
But VBScript doesn't work.
I must make wrong program. But I can't find what makes this problem.(cf → Fig.2)
If someone has advice, I hope you will help me.
Regards,
〔Fig.1〕

Miyuki_0-1712737314611.png

〔Fig.2〕

Miyuki_3-1712738149333.png

 

 

  • CU31080729-3 Profile Picture
    502 Moderator on at

    Hi,

    you have doing some mistake into your code you can fallow this below code and try it.

    ' Create Excel Application object
    Set objExcel = CreateObject("Excel.Application")
    
    ' Make Excel visible (for testing purposes)
    objExcel.Visible = True
    
    ' Open an existing workbook
    Set objWorkbook = objExcel.Workbooks.Open("D:\Temp Folder\Test.xlsx")
    
    ' Select a worksheet
    Set objWorksheet = objWorkbook.Worksheets("Sheet1")
    
    ' Define the cell to be colored
    Set objCell = objWorksheet.Range("A1")
    
    ' Change the background color of the cell
    objCell.Interior.Color = RGB(255, 0, 0) ' Red color
    
    ' Save the workbook (if needed)
    objWorkbook.Save
    
    ' Close the workbook and Excel application
    objWorkbook.Close
    objExcel.Quit
    
    ' Clean up objects
    Set objCell = Nothing
    Set objWorksheet = Nothing
    Set objWorkbook = Nothing
    Set objExcel = Nothing

     

    (Note:- if you got your solution you can mark as solution and gives kudos)

     

    Thanks & Regards

    Chetan Ninghot

     

  • kinuasa Profile Picture
    803 Most Valuable Professional on at

    こんにちは。
    コメントが日本語だったので日本語で回答させていただきます。
    コードを見たところ、XlCellsを取得する処理の親オブジェクトの指定が抜けているようです。

     

    Dim XlApp
    Dim XlSheet
    Dim XlCells
    Set XlApp = GetObject(%FileName%).Application
    Set XlSheet = XlApp.Worksheets(%SheetName%)
    Set XlCells = XlSheet.Range(XlSheet.Cells(5, "A"), XlSheet.Cells(5, "E"))
    XlCells.Interior.Color = RGB(255, 255, 0)

     

    下記のようにWithステートメントを使用しても良いでしょう。

     

    Dim XlApp
    Dim XlSheet
    Dim XlCells
    Set XlApp = GetObject(%FileName%).Application
    Set XlSheet = XlApp.Worksheets(%SheetName%)
    With XlSheet
     Set XlCells = .Range(.Cells(5, "A"), .Cells(5, "E"))
    End With
    XlCells.Interior.Color = RGB(255, 255, 0)

     

    また、Applicationオブジェクトを使用する特別な理由が無いのであれば、GetObjectで取得できるWorkbookオブジェクトをそのまま使えば良いかと思います。

     

    Dim XlBook 'Excel.Workbook
    Dim XlSheet
    Dim XlCells
    Set XlBook = GetObject(%FileName%)
    Set XlSheet = XlBook.Worksheets(%SheetName%)
    Set XlCells = XlSheet.Range(XlSheet.Cells(5, "A"), XlSheet.Cells(5, "E"))
    XlCells.Interior.Color = RGB(255, 255, 0)
  • Verified answer
    Miyuki Profile Picture
    73 on at

    @Chetan_Ninghot 

    @kinuasa 
    Thanks for your quick reply a lot.
    I ended up using ChatGPT and got the hoped answer a little while ago.
    I can resolve my problem by the following.
    I should have made the use of ChatGPT before I posted on this...

    Miyuki_0-1712819623212.png

    -----------
    @kinuasa 様
    アドバイスありがとうございます。
    Excel上の範囲指定は、こちらの投稿後に更にググった結果、誤った記述だと分かりました。
    kinuasa様のアドバイスも試させていただこうと思います。
    VBScriptの知見が全くない故に、アドバイスは大変助かります。
    改めて有難うございました。

     



Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 389

#2
11manish Profile Picture

11manish 345 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 308

Last 30 days Overall leaderboard