根据 Excel 中单元格中的值设置 RGB 颜色
Set RGB color from a value in a cell in Excel
我有一个宏可以为我绘制一堆图表。
我想做的是动态更改 Data series
的 color
。我使用 RGB
调色板和函数 .ForeColor.RGB
。
当我直接使用它时 .ForeColor.RGB = RGB(88, 88, 88)
- 一切正常。但是当我尝试从 cell
.ForeColor.RGB = Sheets(1).Cells(1, 1)
中获取 color
时,我得到了一个错误 Type missmatch
。
在 Cell(1,1)
我有价值:RGB(0, 0, 0)
.
如何从 cell
中为 Data series
选择 color
?
下面的宏和Excelwindow。
Sub Macro1()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.FullSeriesCollection(1).Select
MsgBox Sheets(1).Cells(1, 1)
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = Sheets(1).Cells(1, 1)
'.ForeColor.RGB = RGB(88, 88, 88)
.Transparency = 0
.Solid
End With
End Sub
您不能将函数名称作为 String
值传递。 RGB()
是一个 Function
试试这个。这将从 3 个不同的单元格中获取值,例如 D4、E4 和 F4。
.ForeColor.RGB = RGB( _
Sheets(1).Cells(4, 4), _
Sheets(1).Cells(4, 5), _
Sheets(1).Cells(4, 6) _
)
试试这个:
'/* have to add this line */
ret = Split(Replace(Replace(Sheets(1).Cells(1, 1), "RGB(", ""), ")", ""), ",")
.ForeColor.RGB = RGB(ret(0),ret(1),ret(2))
我有一个宏可以为我绘制一堆图表。
我想做的是动态更改 Data series
的 color
。我使用 RGB
调色板和函数 .ForeColor.RGB
。
当我直接使用它时 .ForeColor.RGB = RGB(88, 88, 88)
- 一切正常。但是当我尝试从 cell
.ForeColor.RGB = Sheets(1).Cells(1, 1)
中获取 color
时,我得到了一个错误 Type missmatch
。
在 Cell(1,1)
我有价值:RGB(0, 0, 0)
.
如何从 cell
中为 Data series
选择 color
?
下面的宏和Excelwindow。
Sub Macro1()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.FullSeriesCollection(1).Select
MsgBox Sheets(1).Cells(1, 1)
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.RGB = Sheets(1).Cells(1, 1)
'.ForeColor.RGB = RGB(88, 88, 88)
.Transparency = 0
.Solid
End With
End Sub
您不能将函数名称作为 String
值传递。 RGB()
是一个 Function
试试这个。这将从 3 个不同的单元格中获取值,例如 D4、E4 和 F4。
.ForeColor.RGB = RGB( _
Sheets(1).Cells(4, 4), _
Sheets(1).Cells(4, 5), _
Sheets(1).Cells(4, 6) _
)
试试这个:
'/* have to add this line */
ret = Split(Replace(Replace(Sheets(1).Cells(1, 1), "RGB(", ""), ")", ""), ",")
.ForeColor.RGB = RGB(ret(0),ret(1),ret(2))