Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

RGB setting in a cell in Excel

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
jerzy.bajor
730 Views, 15 Replies

RGB setting in a cell in Excel

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

15 REPLIES 15
Message 2 of 16
pbejse
in reply to: jerzy.bajor


@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.

Message 3 of 16
jerzy.bajor
in reply to: pbejse

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

Jerzy

Message 4 of 16
pbejse
in reply to: jerzy.bajor


@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.

 

Message 5 of 16
jerzy.bajor
in reply to: pbejse

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

Message 6 of 16
pbejse
in reply to: jerzy.bajor


@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?

 

Message 7 of 16
jerzy.bajor
in reply to: pbejse

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

Message 8 of 16
pbejse
in reply to: jerzy.bajor


@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.

 

 

 

Message 9 of 16
jerzy.bajor
in reply to: pbejse

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.

Message 10 of 16
MunteanStefan
in reply to: jerzy.bajor

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
_$ 

 

Message 11 of 16
pbejse
in reply to: jerzy.bajor


@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.

 

Message 12 of 16
pbejse
in reply to: MunteanStefan


@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 👍

 

Message 13 of 16
jerzy.bajor
in reply to: pbejse

Program written in Vlips.

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

Message 14 of 16
jerzy.bajor
in reply to: MunteanStefan

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

jerzybajor_1-1721822998571.png

 

Jerzy

Message 15 of 16
MunteanStefan
in reply to: jerzy.bajor


@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.

Message 16 of 16
jerzy.bajor
in reply to: MunteanStefan

Now it works elegantly.

Thank you for your help.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report