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

前端 未结 3 2006
礼貌的吻别
礼貌的吻别 2021-01-18 14:31

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 th

相关标签:
3条回答
  • 2021-01-18 15:23

    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
    
    0 讨论(0)
  • 2021-01-18 15:28

    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.

    0 讨论(0)
  • 2021-01-18 15:29

    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

    0 讨论(0)
提交回复
热议问题