Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

RGB setting in a cell in Excel

jerzy.bajor
Contributor

RGB setting in a cell in Excel

jerzy.bajor
Contributor
Contributor

Hello everyone
I created a program for myself that sends tables to Excel. For me it works great.
While formatting cells in Excel, I wanted to have fun with colors and I found out how to color the cell contents with basic VBA colors [(vlax-put (vlax-get cell 'Font) 'ColorIndex NoColor)).]
https://learn.microsoft.com/en-us/office/vba/api/excel.colorindex
However, this color set is incompatible with autocad colors and I wanted to switch to the RGB color set.
I think, I can use:
a) (vlax-put (vlax-get cell 'Font) 'RGB ???)
or
b) (vlax-put (vlax-get cell 'Font) 'Interior.Color ???)
but unfortunately I don't know what to put instead ??? - my English is insufficient.
Maybe one of you knows how to do it.

Jerzy

0 Likes
Reply
Accepted solutions (1)
978 Views
15 Replies
Replies (15)

pbejse
Mentor
Mentor

@jerzy.bajor wrote:

However, this color set is incompatible with autocad colors and I wanted to switch to the RGB color set.


Have a look here --> Colour Conversion Functions : Lee Mac and let us know how it goes.

0 Likes

jerzy.bajor
Contributor
Contributor

What is shown there are activities within AutoCAD. I'm talking about changing the color to RGB in an Excel cell.

Jerzy

0 Likes

pbejse
Mentor
Mentor

@jerzy.bajor wrote:

I created a program for myself that sends tables to Excel. For me it works great.


Are you running this program in AutoCAD?

Just so you know, RGB codes are universal. You can pass the value to your program as an argument for the color assignment.

 

0 Likes

jerzy.bajor
Contributor
Contributor

I don't want to change the standard color to RGB in AutoCAD, but give the cell an RGB color from AutoCAD/Lisp

0 Likes

pbejse
Mentor
Mentor

@jerzy.bajor wrote:

I don't want to change the standard color to RGB in AutoCAD, but give the cell an RGB color from AutoCAD/Lisp


Are you running this program in AutoCAD/lisp?

 

0 Likes

jerzy.bajor
Contributor
Contributor

Yes. I collect information about tables contained in a dwg file and export them to Excel.

0 Likes

pbejse
Mentor
Mentor

@jerzy.bajor wrote:

Yes. I collect information about tables contained in a dwg file and export them to Excel.



@jerzy.bajor wrote:

...but unfortunately I don't know what to put instead ??


Post your code then. we will fil in the blanks for you.

 

 

 

0 Likes

jerzy.bajor
Contributor
Contributor

The program works as you can see in the attachment.

I have the text colors of each cell.

With the help of (vlax-put (vlax-get cell 'Font) 'ColorIndex NoColor)), where NoColor is the color number according to VBA, gives the cell a color.

The problem is that VBA has 56 colors and Autocad has 256 colors.

I know how to switch from AutoCAD color to RGB. I just don't know how to transfer this color to Excel.

0 Likes

MunteanStefan
Contributor
Contributor

RGB is in general represented by 3 bytes, but some applications use them in reverse order.

This function will change the color of the interior of a cell in the specified RGB values.

(defun set_cell_RGB (cell r g b / interior)
  (if
    (setq interior (vlax-get-property cell "Interior"))
    (vlax-put-property
      interior
      "color"
      (apply 'logior
        (mapcar 'lsh
          (list r g b)
          (list 0 8 16)
        )
      )
    )
  )
)

The same transformation can be used to apply a color in Autocad, but one of the last 2 lists must be reversed.

For example, for R=80, G=208, B=146, there are the color codes in Excel and Autocad

_$ (apply 'logior (mapcar 'lsh '(80 208 146) '(0 8 16)))
9621584 - Excel color
_$ (apply 'logior (mapcar 'lsh '(80 208 146) '(16 8 0)))
5296274 - AutoCAD color
_$ 

 

pbejse
Mentor
Mentor

@jerzy.bajor wrote:

The program works as you can see in the attachment.


Neat program you have there. .NET? or Lisp?

 


@jerzy.bajor wrote:

The problem is that VBA has 56 colors and Autocad has 256 colors.


And of this 56 colors does not even match the color index number in AutoCAD

Are these colors will always be the same?

Are you saying you CAN assign a color using an index number but no luck with an RGB color? or not even with an index number?

 

It would be interesting to write this in Vlisp alone but i think its doable. I'll investigate further.

 

0 Likes

pbejse
Mentor
Mentor

@MunteanStefan wrote:
_$ (apply 'logior (mapcar 'lsh '(80 208 146) '(0 8 16)))
9621584 - Excel color
_$ (apply 'logior (mapcar 'lsh '(80 208 146) '(16 8 0)))
5296274 - AutoCAD color
_$ 

Well there you go 👍

 

0 Likes

jerzy.bajor
Contributor
Contributor

Program written in Vlips.

I can color the text in the cell, but only the color of the text in VBA.

0 Likes

jerzy.bajor
Contributor
Contributor

This is great. However, I wanted to change color the contents of the cell, not the cell itself.

jerzybajor_1-1721822998571.png

 

Jerzy

0 Likes

MunteanStefan
Contributor
Contributor
Accepted solution

@jerzy.bajor wrote:

This is great. However, I wanted to change color the contents of the cell, not the cell itself.

jerzybajor_1-1721822998571.png

 

Jerzy


Change "Interior" with "Font". Or better, create a new function just for "Font" property of a cell.

0 Likes

jerzy.bajor
Contributor
Contributor

Now it works elegantly.

Thank you for your help.

0 Likes