使用Excel列表框中的选定值来制定图例

我正在尝试将选定的值从Excellist box传递到图表中的图例。 具体而言,我有以下格式的某些公司的数据

在这里输入图像说明

我还有一个列表框globalList,其中包含可以select的公司名称。 所选公司的数据将被使用VBA传递到图表上。

在这里输入图像说明

但是,我遇到的问题在以下部分:

初始化variables以保存globalList中选定的值

 listMax = globalList.ListCount - 1 `this creates the upper bound for the list box For i = 0 To (globalList.ListCount - 1) If globalList.Selected(i) = True Then companiesSelected = companiesSelected + 1 End If If i = listMax Then Exit For End If Next i `the above is used to retrieve the number of companies that have been selected by a user - whether =0 or > 0 Dim myLegend() As String ReDim myLegend(0 To (globalList.ListCount - 1)) For i = 0 To (globalList.ListCount - 1) If globalList.Selected(i) = True Then myLegend(i) = globalList.List(i) End If If i = listMax Then Exit For End If Next i `this is the array object in which I intend to store company names selected in the list box. 

问题是,即使上面创buildmyLegendstring数组,它也包含用户可能没有在列表框中select的公司的空数组项目。 即使我能够从数组中删除这些空项目,会发生以下问题

将我的variables中的值保存到我的图表中

  For i = 1 To companiesSelected myChart.SeriesCollection(i).Name = myLegend(i) Next i 

这里的问题是, myLegend数组从0开始,而SeriesCollection似乎从1开始。所以我无法将选定项目的string值传递给图表的图例。

请有人指出如何规避这些问题? 提前谢谢了!

这里是一个代码提取选定的项目成一个基于string的数组(无空项目):

 Dim i As Integer Dim iCount As Integer Dim myLegend() As String iCount = 0 With globalList ReDim myLegend(1 To .ListCount) For i = 0 To .ListCount - 1 If .Selected(i) = True Then iCount = iCount + 1 myLegend(iCount) = .List(i) End If Next i End With If iCount > 0 Then ReDim Preserve myLegend(1 To iCount) Else ReDim myLegend(1 To 1) myLegend(1) = "Nothing here!" End If