Sign in to follow this  
Followers 0
luckyluke

How can i set excel cell color with RGB color

3 posts in this topic

#1 ·  Posted (edited)

Hello

I get problem with excel cell color. I'm trying to use this code:

#include <ExcelCOM_UDF.au3>
$oExcel = _ExcelBookNew()
$oExcel.ActiveWorkbook.Sheets(1).cells(1,1).Interior.Color = "RGB(255, 0, 0)"

this code work in VB excel

And i got this error:

C:Documents and SettingsbinhngoDesktopautoitexhtml.au3 (73) : ==> The requested action with this object has failed.:
$oExcel.ActiveWorkbook.Sheets(1).cells(1,1).Interior.Color = "RGB(255, 0, 0)"
$oExcel.ActiveWorkbook.Sheets(1).cells(1,1).Interior^ ERROR

when i try use

$oExcel.ActiveWorkbook.Sheets(1).cells(1,2).Interior.Color = 702623
;or
$oExcel.ActiveWorkbook.Sheets(1).cells(1,2).Interior.ColorIndex = 16

it's worked. But the problem here is: i have 6 digit hex color and i can only convert it to RGB color (for ex:d6834d).

how can i set RGB color for excel cell using Autoit, please help

Edited by luckyluke

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I just found a solution:

Color = (256 * 256 * B)   + G * 256 + R

this is how excel convert RGB to string.

Thanks all

Edited by luckyluke

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I got confused by the non-standard ordering of luckyluke's formula (BGR vs. RGB).

I think it's more natural when written in RGB order, as most people are used to:
 

R + (G*256) + (B*256*256)

 
I assigned the result of this calculation to:
 

$range.interior.color = ...
Edited by MC256

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0