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

前端 未结 3 2010
礼貌的吻别
礼貌的吻别 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
    

提交回复
热议问题