Greg
Greg

Reputation: 1070

VBA - use color already select in Excel to fill cell interior

I know how to set the interior color of a cell to a certain color, but is there a way to do it so that it (i.e., the cell color) defaults to the color already selected in the color window of the ribbon?

Upvotes: 5

Views: 2849

Answers (3)

Johnny MMXVII
Johnny MMXVII

Reputation: 21

You can get the currently selected , but I wish it was easier..

This is an example of capturing it based on the code supplied by @justpassingthrough.

Instead of Debug.print - you could save the value into a global variable perhaps?

      Sub HiddenSheetGetColor()


      Application.ScreenUpdating = False  ' :: STOP SCREEN FLASHES

      Dim HiddenSheetName  As String   ':: VARIABLE TO SHEET NAME

      HiddenSheetName = Format(Now(), "__YYYYMMDD_HH_MM_SS_.00")  ' TIMESTAMP FOR SHEET SO IT'LL NEVER DUPLICATE

      Worksheets.Add.Name = HiddenSheetName  ' CREATE NEW SHEET AND SET NAME TO TIMESTAMP REFERENCED ABOVE

      Sheets(HiddenSheetName).Select   'SELECT IT

      Range("A1").Select                  'SELECT A CELL

      Application.CommandBars.ExecuteMso "CellFillColorPicker"   ' APPLY CURRENT TOOLBAR 'FILL' COLOUR TO CELL

      Debug.Print Range("A1").Interior.Color  ' :: PRINT THIS VALUE TO LOG/IMMEDIATE WINDOW ::

      Application.DisplayAlerts = False  ' :: STOP ERROR WHEN DELETING SHEET

      Sheets(HiddenSheetName).Delete ' :: DELETE SHEET

      Application.DisplayAlerts = True ' :: ALLOW ERROR WHEN DELETING SHEET

      Application.ScreenUpdating = True ' :: UPDATE SCREEN AGAIN!

      End Sub

Upvotes: 1

justpassingthrough
justpassingthrough

Reputation: 56

I'm getting to this very late but since it popped up in a google search for something else, I thought I'd mention the selected answer is incorrect when it says you cannot do what the submitter wanted. You can activate the control directly with...

Application.CommandBars.ExecuteMso "CellFillColorPicker"

...and it will apply the currently selected color to the selected range.

I'm actually looking around to see if there is a way to select another color in the control's gallery via VBA so I can make the default "no fill" rather than yellow when first opening a workbook.

Upvotes: 4

JMax
JMax

Reputation: 26611

I'm afraid you can't

Anyway, you can use a custom palette using this code:

The modColorFunctions module contains a function named ChooseColorDialog that will display a Windows Color Picker dialog and return the RGB Long color value. If the user cancels the dialog, the result is -1. For example,

Dim RGBColor As Long
Dim Default As Long
Default = RGB(255, 0, 255) 'default to purple
RGBColor = ChooseColorDialog(DefaultColor:=Default)
If RGBColor < 0 Then
    Debug.Print "*** USER CANCELLED"
Else
    Debug.Print "Choice: " & Hex(RGBColor)
End If

Taken from the chapter Displaying A Color Picker Dialog of http://www.cpearson.com/Excel/Colors.aspx

You need to add the color module to make it work

Upvotes: 0

Related Questions