【问题标题】:Add two different types of Charts to Excel and Format them将两种不同类型的图表添加到 Excel 并设置它们的格式
【发布时间】:2018-08-28 11:04:56
【问题描述】:

我有一个由两个主要内容组成的图表。

第一个是基于值创建一系列系列的循环。这些系列中的每一个都是带有线条的 XY 散点图。这些行中的每一个都根据使用 Excel 中的Vlookup 函数的条件进行着色。我需要更正的第一件事是Case 部分,因为它不喜欢 G 的第一个实例。这只发生在我添加第二个图表时。

接下来我想要创建一个带有另一个范围的 XY 散点图,然后仅将自定义数据标签应用于这些点。我可以使用以下已更新的答案来更改 Chart the Series 绘图的类型。

    Dim age1 As Variant
    Dim age2 As Variant
    Dim per1 As Variant
    Dim per2 As Variant
    Dim id as Variant
    Dim mp as Range
    Dim yd as Range
    id = Range(Range("A2"), Range("A2").End(xlDown)).Value2
    age1 = Range(Range("C2"), Range("C2").End(xlDown)).Value2
    age2 = Range(Range("D2"), Range("D2").End(xlDown)).Value2
    per1 = Range(Range("E2"), Range("E2").End(xlDown)).Value2
    per2 = Range(Range("E2"), Range("E2").End(xlDown)).Value2
    Set mp = Range(Range("J2"), Range("J2").End(xlDown))
    Set yd= Range(Range("E2"), Range("E2").End(xlDown))

    ln = UBound(id) - LBound(id) + 1

    Set cht = ws.ChartObjects(1).Chart

        With cht
            .ChartArea.ClearContents 'Clears the chart so a new one can be created
            .ChartType = xlXYScatterLines 'Defines the Chart as a Scatter with Lines
            For i = 1 To ln 'First Thing that creates many series
                xdata = Array(age1(i, 1), age2(i, 1))
                ydata = Array(per1(i, 1), per2(i, 1))
                .SeriesCollection.NewSeries
                .SeriesCollection(i).XValues = xdata
                .SeriesCollection(i).Values = ydata
                .SeriesCollection(i).Name = id(i, 1)
            Next i

            'Orginal method: .ChartType = xlXYScatter 
               .SeriesCollection.NewSeries
               .SeriesCollection(.SeriesCollection.Count).XValues = mp
               .SeriesCollection(.SeriesCollection.Count).Values = yd
               .SeriesCollection(.SeriesCollection.Count).Name = "Series"
               'New Method
               .SeriesCollection(.SeriesCollection.Count).ChartType = xlXYScatter

        End With
'end of creating charts

        Set drng = Range(Range("A2"), Range("B2").End(xlDown) 'For the Vlookup
        With ActiveSheet
            For Each xycht In .ChartObjects
                For Each mysrs In xycht.Chart.SeriesCollection
                    mysrs.MarkerStyle = xlMarkerStyleCircle
                    lnum = Application.VLookup(mysrs.Name, drng, 2, 0) 'This fails the first instance with G as a Type Mismatch Error.
'                    Select Case lnum
'                        Case "G"
'                            lColor = RGB(255, 0, 0)
'                        Case "D"
'                            lColor = RGB(0, 255, 0)
'                        Case "M"
'                            lColor = RGB(0, 0, 255)
'                        Case "A"
'                            lColor = RGB(0, 0, 0)
'                        Case Else
'                            lColor = RGB(255, 255, 255)
'                    End Select
'                    mysrs.MarkerBackgroundColor = lColor
'                    mysrs.Format.Line.Visible = msoFalse
'                    mysrs.Format.Line.Visible = msoTrue
'                    mysrs.Format.Line.ForeColor.RGB = lColor
                Next
                Set mypts = ws.ChartObjects(1).SeriesCollection(SeriesCollection.Count).Points(1).Apply 'This fails cause it needs an Object
                mypts(mypts.Count).ApplyDataLabels

                    With mypts(mypts.Count).DataLabel
                        .ShowSeriesName = False
                        .ShowCategoryName = False
                        .ShowValue = False 'I need this tonot show Values, but my own Values.
                        ' optional parameters
                        .Position = xlLabelPositionAbove
                        .Font.Name = "Helvetica"
                        .Font.Size = 10
                        .Font.Bold = False
                    End With
            Next
        End With

【问题讨论】:

    标签: excel vba charts formatting


    【解决方案1】:

    使用 Series 对象的 ChartType 属性...

    .SeriesCollection(.SeriesCollection.Count).ChartType = xlXYScatter
    

    【讨论】:

    • 好的。这是其中的一部分。
    • Application.Vlookup 在未找到匹配项时返回错误。如果是这样,您将在 Select Case 语句中收到类型不匹配错误。也许应该永远有一场比赛。在这种情况下,您需要进行必要的调整。否则,您可以使用If IsError(lnum) Then 测试错误,然后采取适当的措施。
    • 我可以使用 On Error GoTo 选项在别处设置颜色。我现在需要找到一种方法将该颜色设置为透明。为什么 vba 不自带rgba
    • 首先,我应该说Application.Vlookup 返回一个non-break 错误。所以你将无法使用On Error GoTo 进行测试。您需要使用IsError
    • 您想设置透明度还是只是想将 Visible 属性设置为 False?
    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多