在userform vba excel中使用owc图表空间绘制图表

时间:2011-04-20 19:04:46

标签: excel-vba vba excel

我的owc图表空间有点问题,我想在图片中绘制一个图表,但我的问题是它只绘制了一个系列,我想为1和2和3绘制它不知道该怎么做。

我有一个列表框和一个组合框,我从列表框中选择1,2,3,我从组合框y或z中选择,使x固定。

然后我将数据放在plage(1)中为x和plage(2)为y,但问题是它只适用于我从列表框中选择的第一个项目(在此图片中为“1”)

你能告诉我的代码有什么问题吗?

enter image description here

用于将图表绘制到用户表单中的vba代码是:

      Private Sub drow()
                                                                                                     Dim i, k As Integer, x As Integer
          Dim j As Integer
          Dim Table(), Plage(2)
          Dim id As Integer

          id = 1

         Do While ComboBox.Value <> idi(id, 1) 
           id = id + 1
          Loop 

          For i = Cht.SeriesCollection.Count To 1 Step -1
             Cht.SeriesCollection.Delete i - 1
          Next i

              k = 1
           ReDim Table(ListBox.ListCount)

        For i = 0 To ListBox.ListCount - 1 
          If ListBox.Selected(i) = True Then
             Table(k) = ListBox.List(i)
            k = k + 1
          End If
          Next i

    With Cht
    .HasLegend = True
     .Legend.Position = chLegendPositionBottom
         .HasTitle = True
        .Title.Caption = ComboBox.Text
       End With

  Cht.Type = C.chChartTypeColumnClustered3D

    With Cht
        'first serie
        .SeriesCollection.Add
        .SeriesCollection(0).Caption = sheet.Cells(2, 15 + id) 
        .SeriesCollection(0).DataLabelsCollection.Add
        .SeriesCollection(0).DataLabelsCollection(0).Position = chLabelPositionCenter
        .SeriesCollection(0).DataLabelsCollection(0).Font.Color = RGB(255, 255, 255)

        .SeriesCollection.Add
        .SeriesCollection(1).Caption = sheet.Cells(2, 20)      .SeriesCollection(1).DataLabelsCollection.Add
        .SeriesCollection(1).DataLabelsCollection(0).Position = chLabelPositionCenter
        .SeriesCollection(1).DataLabelsCollection(0).Font.Color = RGB(255, 255, 255)

        .SetData C1.chDimCategories, C1.chDataLiteral, Table

        End With

   For j = 0 To ListBox.ListCount - 1
    If ListBox.Selected(j) = True Then

     Plage(1) = sheet.Cells(j + 3, 15 + id) 'the Xs
     Plage(2) = sheet.Cells(j + 3, 20) 'Les 'the Ys

      With Cht

        .SeriesCollection(0).SetData C1.chDimValues, C1.chDataLiteral, Plage(1)
        .SeriesCollection(1).SetData C1.chDimValues, C1.chDataLiteral, Plage(2)


      End With
    Erase Plage
   End If
  Next j

  End Sub

1 个答案:

答案 0 :(得分:0)

我对整个owc和VB事情都很陌生,我自己也遇到了一些麻烦,但你是否尝试过使用C1.chDimXValues和C1.chDimYValues而不是下面语句中的chDimValues:

  

.SeriesCollection(0).SetData   C1.chDimValues,C1.chDataLiteral,   Plage(1).SeriesCollection(1).SetData   C1.chDimValues,C1.chDataLiteral,   谱斑(2)

很抱歉,如果这听起来微不足道,我可以看到你的编码技巧比我的更先进。祝你好运!