Ask Your Question
4

How can I use VBA to retrieve the interior cell color of the active cell and then insert the corresponding hex number into that cell?

asked 2022-04-01 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-02-26 11:00:00 +0000

plato gravatar image

Here's a code snippet that retrieves and inserts the hex color code of the active cell:

Sub GetActiveCellColor()
    Dim activeCellColor As Long
    Dim activeCellHexColor As String

    activeCellColor = ActiveCell.Interior.Color
    activeCellHexColor = Hex(activeCellColor)

    'Insert the hex color code into the active cell
    ActiveCell.Value = activeCellHexColor
End Sub

This code retrieves the color of the active cell's interior by using the .Color property of the Interior object. The color is returned as a Long integer, which is then converted to its hex representation using the Hex function.

Finally, the hex color code is inserted into the active cell using the .Value property.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-01 11:00:00 +0000

Seen: 14 times

Last updated: Feb 26 '22