早安的人,
我希望使用VBA和用户表单切换图表系列以选择特定系列,我已经成功使用折线图。但是,当我尝试使用条形图时,它似乎不会更新数据,只会更新图例。
我使用的VBA代码是:
模块:
Option Explicit
Sub ChartContent() 'Excel VBA process to select the chart and show the userform.
ActiveSheet.ChartObjects.Select
ufChart.Show
End Sub
用户窗体:
Option Explicit
Private Sub cmdApply_Click()
Dim iSres As Integer
Application.ScreenUpdating = False
With ActiveChart
.HasLegend = False
.HasLegend = True
For iSres = .SeriesCollection.Count To 1 Step -1
If ListBox1.Selected(iSres - 1) Then
.SeriesCollection(iSres).Border.LineStyle = xlAutomatic
.SeriesCollection(iSres).MarkerStyle = xlAutomatic
Else
.SeriesCollection(iSres).Border.LineStyle = xlNone
.SeriesCollection(iSres).MarkerStyle = xlNone
.Legend.LegendEntries(iSres).Delete
End If
Next
.Deselect
End With
Unload Me
End Sub
Private Sub cmdCancel_Click()
Unload Me
End Sub
Private Sub ListBox1_Click()
End Sub
Private Sub UserForm_Initialize()
Dim iSres As Integer
With ActiveChart
For iSres = 1 To .SeriesCollection.Count
ListBox1.AddItem .SeriesCollection(iSres).Name
ListBox1.Selected(ListBox1.ListCount - 1) = Not (.SeriesCollection(iSres).Border.LineStyle = xlNone)
Next
End With
End Sub
有人会对为什么会出现这种情况以及可能的解决方案有任何建议,所以我可以将这个VBA应用于条形图,我不太确定为什么它不会用条形图更新。
非常感谢,
答案 0 :(得分:0)
通过替换某些代码解决了这个问题,如果需要条形图,请使用:
With ActiveChart
.HasLegend = False
.HasLegend = True
For iSres = .SeriesCollection.Count To 1 Step -1
If ListBox1.Selected(iSres - 1) Then
.SeriesCollection(iSres).fill.Visible = msoTrue
.SeriesCollection(iSres).MarkerStyle = xlAutomatic
Else
.SeriesCollection(iSres).fill.Visible = msoFalse
.SeriesCollection(iSres).MarkerStyle = xlNone
.Legend.LegendEntries(iSres).Delete
End If
Next
.Deselect
End With
Unload Me
End Sub
答案 1 :(得分:0)