VBA存储msoThemeColor中的variables
我正在寻找将mso主题颜色存储在variables中,以便图表颜色和模式可以dynamic变化(在这种情况下variablesTh)。 这是我现在的代码:
Dim Th As Long Th = "msoThemeColorAccent" & ActiveCell.Offset(-5, 0) If ActiveCell = "Realized" Then ActiveChart.SeriesCollection(Srs).Select With Selection.Format.Fill .ForeColor.ObjectThemeColor = Th .Solid End With With Selection.Format.Line .ForeColor.ObjectThemeColor = Th End With End If
我在想这个问题是,我没有使用正确的昏暗的分类。 我已经看到在这里存储RGB作为variables的问题(使用Dim Long似乎是解决scheme),但没有在msoThemeColors。 谢谢,让我知道,如果我可以提供任何其他细节!
由于msoThemeColorAccent是一个枚举,下面的重构代码将工作。
Dim lThemeColor As Long lThemeColor = ActiveCell.Offset(-5, 0) + 4 'msoThemeColor1 enum is 5, 2 is 6, 3 is 7 ... so add 4 to the value If ActiveCell = "Realized" Then With ActiveChart.SeriesCollection(Srs).Format With .Fill .ForeColor.ObjectThemeColor = lThemeColor .Solid End With With .Line .ForeColor.ObjectThemeColor = lThemeColor End With End With End If
很难说出是怎么回事,因为你的代码严重依赖于默认的成员 。 无论如何:
Dim Th As Long
这是对的。
Dim themeColorIndex As MsoThemeColorIndex
这是正确和明确的(请参阅MSDN上的MsoThemeColorIndex )。
正如Scott Holtzman所说 ,这里可能的值是Enum
值,而不是string:通过将string连接到一个定义的Enum
名称中,您无法生成正确的值。
所以通过这样做:
Th = "msoThemeColorAccent" & ActiveCell.Offset(-5, 0)
我的猜测是ActiveCell.Offset(-5, 0)
必须包含一个介于1和6之间的数字。这是一个危险的假设: ActiveCell
实际上可以是任何东西 。 如果该值在特定的单元格中,请参考其地址:
themeColorIndex = Sheet1.Range("B12").Value
这稍微好一些,但是仍然假定B12
中的值可以隐式转换为Long
整数。
Dim selectedValue As Variant selectedValue = Sheet1.Range("B12").Value If Not IsNumeric(selectedValue) Then MsgBox "Invalid value!" Exit Sub End If If Sheet1.Range("E12").Value <> "Realized" Then Exit Sub Dim themeColorIndex As MsoThemeColorIndex themeColorIndex = selectedValue
如果您只对msoThemeColorAccent1
至msoThemeColorAccent6
,那么您将希望B12
包含值5
到10
,这是您正在查找的基础枚举值 。
如果您的工作表出于可用性原因必须允许值1
到6
,那么您可以这样做:
Dim themeColorIndex As MsoThemeColorIndex 'msoThemeColorAccent1 underlying value is 5, so we add 4 to the value: themeColorIndex = selectedValue + 4
那么你又犯了另一个危险的假设:你认为有一个活跃的图表! 假设图表存在于Sheet1
,则最好再次明确指出它:
Dim theChart As ChartObject Set theChart = Sheet1.ChartObjects(1) With theChart.SeriesCollection(srs) 'whatever srs means With .Format .Fill.ForeColor.ObjectThemeColor = themeColorIndex .Fill.Solid .Line.ForeColor.ObjectThemeColor = themeColorIndex End With End With
- Python中的Microsoft Graph API for SharePoint:Microsoft.SharePoint.Client.UnknownError
- Excel :: Writer :: XLSX ::图表 – add_series():名称属性的问题
- 如何在工作簿中定位图表
- 使用Delphi在Excel中创build图表的疑问
- Excel VBA中无法刷新图表(Excel 2016)
- 从C#中的Excel中绘制graphics
- VBA错误:获取运行时错误“91”:对象variables或未设置块variables尝试更改图表数据时
- 在EXCEL透视图中显示小计
- Excel – 柱状图标签作为最高栏的百分比