Excel VBA-在颜色选择器中更改当前颜色
如何在Excel的颜色选择器中更改当前颜色?我在谷歌上搜索过,完全不知道该怎么做。我看过这个Excel VBA-在颜色选择器中更改当前颜色,excel,vba,Excel,Vba,如何在Excel的颜色选择器中更改当前颜色?我在谷歌上搜索过,完全不知道该怎么做。我看过这个应用程序。Dialogs(xlDialogEditColor)但它不是我想要的 基本上,当我运行宏时,它会将颜色选择器(字体和填充颜色选择器)中的颜色设置为指定的颜色。例如,如果指定的颜色为红色,则会发生这种情况 任何帮助都将不胜感激,感谢电子表格专家能帮助您实现这一目标。如果您想在调色板的“最近使用的颜色”部分添加多种颜色,则可以轻松地将该方法用于单一颜色(通过在下面的代码中编辑数组以仅包含一个RG
应用程序。Dialogs(xlDialogEditColor)
但它不是我想要的
基本上,当我运行宏时,它会将颜色选择器(字体和填充颜色选择器)中的颜色设置为指定的颜色。例如,如果指定的颜色为红色,则会发生这种情况
任何帮助都将不胜感激,感谢电子表格专家能帮助您实现这一目标。如果您想在调色板的“最近使用的颜色”部分添加多种颜色,则可以轻松地将该方法用于单一颜色(通过在下面的代码中编辑数组以仅包含一个RGB颜色代码),这将导致该颜色为用户选择的颜色
本文中建议的代码如下所示:
'Declare Sleep()API
#如果是VBA7,则为“Excel 2010或更高版本”
公共声明PtrSafe子睡眠库“kernel32”(ByVal毫秒为LongPtr)
#Else的Excel 2007或更早版本
公共声明子睡眠库“kernel32”(字节毫秒长)
#如果结束
子加载最近的颜色()
'用途:使用RGB代码列表将颜色加载到调色板的“最近使用的颜色”部分
来源:www.TheSpreadsheetGuru.com/the-code-vault
变暗颜色列表
Dim CurrentFill作为变量
'要添加到最近颜色部分的RGB颜色代码数组列表(最多10个)
颜色列表=数组(“066174093”、“184055038”、“046062081”、“056160133”)
'存储ActiveCell的填充颜色(如果适用)
如果ActiveCell.Interior.ColorIndex xlNone,则CurrentFill=ActiveCell.Interior.Color
'优化代码
Application.ScreenUpdating=False
'循环浏览RGB代码列表并添加到最近的颜色
对于x=LBound(颜色列表)到UBound(颜色列表)
ActiveCell.Interior.Color=RGB(左(颜色列表(x),3),中(颜色列表(x),5,3),右(颜色列表(x,3))
多芬特
发送密钥“%h”
睡眠500'暂停半秒(单位为毫秒)
发送键“h”
睡眠500'暂停半秒(单位为毫秒)
发送键“m”
睡眠500'暂停半秒(单位为毫秒)
发送键“~”
睡眠500'暂停半秒(单位为毫秒)
多芬特
下一个x
'返回ActiveCell原始填充颜色
如果CurrentFill=空,则
ActiveCell.Interior.ColorIndex=xlNone
其他的
ActiveCell.Interior.Color=currentColor
如果结束
端接头
但是,我建议始终将SendKeys方法的wait
参数设置为true
,以便让VBA知道在继续之前必须等待按键处理(例如SendKeys“%h”,true
)。这将增加您的按键被正确注册和执行的可能性
我不确定,但由于整个过程都在Excel中运行,并且没有涉及其他应用程序,因此在每次按键后甚至可能不需要使用Sleep
功能,但使用SendKeys方法时要过于谨慎,这绝不是一个坏主意
因此,这将适用于填充工具。现在,对于字体颜色,快捷方式应该是Alt+H、FC、M
'Declare Sleep() API
#If VBA7 Then ' Excel 2010 or later
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else ' Excel 2007 or earlier
Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
#End If
Sub LoadRecentColors()
'PURPOSE: Use A List Of RGB Codes To Load Colors Into Recent Colors Section of Color Palette
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim ColorList As Variant
Dim CurrentFill As Variant
'Array List of RGB Color Codes to Add To Recent Colors Section (Max 10)
ColorList = Array("066,174,093", "184,055,038", "046,062,081", "056,160,133")
'Store ActiveCell's Fill Color (if applicable)
If ActiveCell.Interior.ColorIndex <> xlNone Then CurrentFill = ActiveCell.Interior.Color
'Optimize Code
Application.ScreenUpdating = False
'Loop Through List Of RGB Codes And Add To Recent Colors
For x = LBound(ColorList) To UBound(ColorList)
ActiveCell.Interior.Color = RGB(Left(ColorList(x), 3), Mid(ColorList(x), 5, 3), Right(ColorList(x), 3))
DoEvents
SendKeys "%h"
Sleep 500 'Pause half-second (units in milliseconds)
SendKeys "h"
Sleep 500 'Pause half-second (units in milliseconds)
SendKeys "m"
Sleep 500 'Pause half-second (units in milliseconds)
SendKeys "~"
Sleep 500 'Pause half-second (units in milliseconds)
DoEvents
Next x
'Return ActiveCell Original Fill Color
If CurrentFill = Empty Then
ActiveCell.Interior.ColorIndex = xlNone
Else
ActiveCell.Interior.Color = currentColor
End If
End Sub