首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的VBA代码创建的是折线图而不是散点图?

为什么我的VBA代码创建的是折线图而不是散点图?
EN

Stack Overflow用户
提问于 2019-06-06 03:37:47
回答 1查看 103关注 0票数 0

我试图从两个数据列生成散点图,但我得到的是一个线状图,其中第一列的数据被忽略(即:如果我有1000个点,在x轴上我看到的值从1到1000,而不管第一列中存储的数据是什么)。我找不到我的代码中的错误。怎么了?

代码语言:javascript
复制
Public Sub Graph_Refresh()

    Dim cht As Chart
    Dim i As Integer
    Dim seriesIndex As Integer
    Set cht = Sheets("Graph").ChartObjects("Chart 1").Chart
    seriesIndex = 0

    ' ***** CLEAR OLD CONTENT *****
    cht.ChartArea.ClearContents

    ' ***** NON CHANGEABLE PARAMETERS *****
    'Format Font Type and Size
    cht.ChartType = xlXYScatterLinesNoMarkers                                   ' scatter plot
    cht.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Arial"
    cht.ChartArea.Format.TextFrame2.TextRange.Font.Size = 12
    cht.HasTitle = False      ' No chart title
    'cht.SetElement (msoElementPrimaryValueGridLinesMajor)   'Gridlines

    'Adjust x-axis
    cht.HasAxis(xlCategory, xlPrimary) = True
    cht.Axes(xlCategory, xlPrimary).HasTitle = True
    cht.Axes(xlCategory).AxisTitle.Text = "Frequency [MHz]"
    cht.Axes(xlCategory).MinimumScale = Sheets("Graph").Range("AI7").Value
    cht.Axes(xlCategory).MaximumScale = Sheets("Graph").Range("AI8").Value

    'Adjust y-axis
    cht.HasAxis(xlValue, xlPrimary) = True
    cht.Axes(xlValue, xlPrimary).HasTitle = True
    cht.Axes(xlValue).AxisTitle.Text = "S-Parameters [dB]"
    cht.Axes(xlValue).MinimumScale = Sheets("Graph").Range("AI9").Value
    cht.Axes(xlValue).MaximumScale = Sheets("Graph").Range("AI10").Value
    cht.Axes(xlValue).CrossesAt = -100

    ' Data Series
    For i = 1 To 5
                    seriesIndex = seriesIndex + 1
                    cht.SeriesCollection.NewSeries
                    With Sheets("Graph")
                        cht.SeriesCollection(seriesIndex).Name = .Cells(6 + (i - 1) * 4).Value & " S11"
                    End With
                    cht.SeriesCollection(seriesIndex).XValues = "='" & Sheets("Data" & CStr(i)).Name & "'!$K$4:$K$10004"
                    cht.SeriesCollection(seriesIndex).Values = "='" & Sheets("Data" & CStr(i)).Name & "'!$L$4:$L$10004"
                    ' Set line size and color
                    With cht.SeriesCollection(seriesIndex)
                        .Format.Line.Weight = 2.25
                        .Format.Line.Visible = msoFalse
                        .Format.Line.Visible = msoTrue
                        .Format.Line.ForeColor.RGB = RGB(255,0,0)
                        .MarkerStyle = xlMarkerStyleNone
                    End With

    Next i


    ' Legend


End Sub

数据存储在"Data1“- "Data5”表中,范围应该是ok的。图表“图表1”已经存在(这就是我不创建它的原因)。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

发布于 2019-06-06 18:14:51

经过进一步的调查,我找到了答案。我把它留给可能感兴趣的人。

这个问题是因为我提供的数据区域包含空单元格(或者,更准确地说,包含返回空格的函数的单元格)。调整XValues和值的大小以仅包含具有数据的单元格解决了此问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56466937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档