VBA:用 "Select Case" 给图表系列上色
VBA: Coloring chart series with "Select Case"
我是 VBA 的新手,因此非常感谢您提供这方面的帮助。 :)
如问题 header 所示,我尝试为图表中的每个系列赋予不同的颜色。到目前为止,我得到的效果很好,但有一个小限制。该系列的名称有时会更改,所以我希望 excel 直接从单元格中取出名称,而不是每次都更改代码。例如,不是手写 "dormakaba",而是将值写入工作表单元格 "A2"。
如果有人有解决方案,那就太好了。
Sub Color()
Dim iSrs As Long, nSrs As Long
If ActiveChart Is Nothing Then
MsgBox "No chart selected. Please try again.", vbExclamation, "No Active Chart"
Else
With ActiveChart
nSrs = .SeriesCollection.Count
For iSrs = 1 To nSrs
' only format series whose names are found
Select Case LCase$(.SeriesCollection(iSrs).Name)
Case "dormakaba" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(255, 130, 171)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "georg fischer" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(155, 48, 255)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "clariant" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "givaudan" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(202, 225, 255)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "galencia" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(67, 205, 128)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "lonza" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(238, 230, 133)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
End Select
Next
End With
End If
End Sub
在您的代码中将 "dormakaba" 更改为 Range("A2")
:
Select Case LCase$(.SeriesCollection(iSrs).Name)
Case LCase$(Range("A2")) '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(255, 130, 171)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
既然你在避免这种情况,那么LCase$(Range("A2"))
会更好。
我是 VBA 的新手,因此非常感谢您提供这方面的帮助。 :) 如问题 header 所示,我尝试为图表中的每个系列赋予不同的颜色。到目前为止,我得到的效果很好,但有一个小限制。该系列的名称有时会更改,所以我希望 excel 直接从单元格中取出名称,而不是每次都更改代码。例如,不是手写 "dormakaba",而是将值写入工作表单元格 "A2"。 如果有人有解决方案,那就太好了。
Sub Color()
Dim iSrs As Long, nSrs As Long
If ActiveChart Is Nothing Then
MsgBox "No chart selected. Please try again.", vbExclamation, "No Active Chart"
Else
With ActiveChart
nSrs = .SeriesCollection.Count
For iSrs = 1 To nSrs
' only format series whose names are found
Select Case LCase$(.SeriesCollection(iSrs).Name)
Case "dormakaba" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(255, 130, 171)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "georg fischer" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(155, 48, 255)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "clariant" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(0, 255, 0)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "givaudan" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(202, 225, 255)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "galencia" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(67, 205, 128)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
Case "lonza" '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(238, 230, 133)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
End Select
Next
End With
End If
End Sub
在您的代码中将 "dormakaba" 更改为 Range("A2")
:
Select Case LCase$(.SeriesCollection(iSrs).Name)
Case LCase$(Range("A2")) '******CHANGE HERE******
.SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = RGB(255, 130, 171)
.SeriesCollection(iSrs).Format.Line.Visible = True
.SeriesCollection(iSrs).Format.Line.Visible = False
既然你在避免这种情况,那么LCase$(Range("A2"))
会更好。