1

我正在通过一个选择列表框来确定要选择的系列。之前的开发人员做了以下事情:

For i = 0 To ListBox4.ListCount - 1
   If ListBox4.Selected(i) Then
      Series_Msg = Series_Msg & ListBox4.List(i) & vbNewLine
      ActiveChart.SeriesCollection(i + 1).Select

      'other commands

  Next i

为了清理他的代码,我尝试执行以下操作:

   If ListBox4.ListIndex <> -1 then

       ActiveChart.SeriesCollection(ListBox4.ListIndex + 1).Select

但我得到一个需要对象的错误。我尝试将 i 声明为一个对象并为其分配 ListIndex 值,但这不起作用。

谁能建议我如何在没有循环的情况下做到这一点?我不敢相信这个循环是必要的。

任何帮助是极大的赞赏!

拉斯

4

3 回答 3

1

确保您已正确选择图表:

Worksheets("Sheet1").ChartObjects("Chart 1").Activate

If ListBox4.ListIndex <> -1 Then
    Series_Msg = Series_Msg & ListBox4.List(ListBox4.ListIndex, 0)
    ActiveChart.SeriesCollection(ListBox4.ListIndex + 1).Select
End If
于 2013-03-22T00:34:30.223 回答
0

尝试使用: ActiveChart.SeriesCollection(ListBox4.List(ListBox4.ListIndex)).Select

于 2013-03-22T00:26:54.483 回答
0

这段代码对我来说很好,只要有一个ActiveChart. 添加一些处理以确保 ActiveChart 不是 Nothing。否则,您将收到该错误,Object Variable or With Block not set.

Sub UserForm_Activate()
    ListBox1.AddItem "First"
    ListBox1.AddItem "Second"
    ListBox1.AddItem "Third"
End Sub

Private Sub ListBox1_AfterUpdate()
Dim i As Integer

If ActiveChart Is Nothing Then 
    MsgBox "There is no ActiveChart!"
    Exit Sub
End If

i = ListBox1.ListIndex + 1

ActiveChart.SeriesCollection(i).Select
'Alternatively, you can ignore "i" and just use ActiveChart.SeriesCollection(ListBox1.ListIndex+1).Select

Unload UserForm1

End Sub
于 2013-03-22T00:35:22.077 回答