基于当前字体颜色在两种字体颜色(或个案)之间切换的VBA代码

我想写一个macros,这将允许我在字体颜色之间切换。 为了保持这个简单,只要说两种字体的颜色。 从黑到蓝或从蓝到黑。

但是,我想让excel告诉我的macros是否已经select了黑色或蓝色的字体(无论是在当前的单元格中,还是一般的)。 并从那里确定要更改的颜色。

因此,如果工具栏上的字体颜色select为黑色,则它将运行将字体更改为蓝色(通过运行下面的脚本的相应部分)。 如果工具栏上的字体颜色select为蓝色,则它将运行将字体更改为黑色(通过运行下面的脚本的相应部分)。

下面,我有用于将字体颜色更改为蓝色或黑色的代码。 这里的问题是我不知道正确的语法或代码,以确定工具栏(或单元格,如果这样做工具栏上是不可能的)当前选定的字体颜色。 确定字体颜色是macros的第一部分,这是我有麻烦。

感谢帮助!

Sub toggle() 'need code to determine font color, then to execute appropriate code below With Selection.font .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0 End With Exit Sub blackpath: With Selection.font .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 End With End Sub 

继@SeanC之后:

 Sub toggle() With Selection.font If .ThemeColor = xlThemeColorLight1 Then .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0 ElseIf .ThemeColor = xlThemeColorAccent1 Then .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 End If End With End Sub 

这里是找出在button中select哪种颜色的方法

这是一个kludge虽然

 Option Explicit Sub test() Dim aaa As Long Dim fontColorButton As Long aaa = Range("a1").Font.Color Range("a1").Select Application.CommandBars.ExecuteMso ("FontColorPicker") ' click the "font color" button ' get the command name from the "customize ribbon" dialog fontColorButton = Range("a1").Font.Color Range("a1").Font.Color = aaa Debug.Print fontColorButton End Sub 

我能想到的唯一方法将是一个可怕的kludge:使用Workbook_SheetChange获取当前的字体颜色。

 Option Explicit Dim CurCol As Long ' default is: -16776961 you could set this on Workbook_Open Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) CurCol = Target.Font.Color End Sub