简体   繁体   English

VBA - 已经在 Excel 中使用颜色 select 来填充单元内部

[英]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 (ie, the cell color) defaults to the color already selected in the color window of the ribbon?我知道如何将单元格的内部颜色设置为某种颜色,但是有没有办法让它(即单元格颜色)默认为色带颜色 window 中已经选择的颜色?

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.我实际上正在环顾四周,看看是否有办法通过 VBA 在控件库中使用 select 另一种颜色,这样我可以在第一次打开工作簿时设置默认的“无填充”而不是黄色。

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.这是基于@justpassingthrough 提供的代码捕获它的示例。

Instead of Debug.print - you could save the value into a global variable perhaps?而不是 Debug.print - 您可以将值保存到全局变量中吗?

      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

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. modColorFunctions 模块包含一个名为 ChooseColorDialog 的 function,它将显示一个 Windows 颜色选择器对话框并返回 RGB Long 颜色值。 If the user cancels the dialog, the result is -1.如果用户取消对话框,结果为 -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摘自http://www.cpearson.com/Excel/Colors.aspx的“显示颜色选择器对话框”一章

You need to add the color module to make it work您需要添加颜色模块才能使其工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM