简体   繁体   English

运行时错误 424 需要对象(点对象)VBA

[英]Run-time error 424 Object required (Points Object) VBA

I'm trying to make the final data point in my scatter plot "highlight" and be identified.我试图使散点图中的最终数据点“突出显示”并被识别。 Using: https://docs.microsoft.com/en-us/office/vba/api/excel.points(object) I came up with the line of code .FullSeriesCollection(1).Points(Points.Count).ApplyDataLabels Type:=xlShowValue which is supposed to add a label to the last point in series 1.使用: https : .FullSeriesCollection(1).Points(Points.Count).ApplyDataLabels Type:=xlShowValue我想出了一行代码.FullSeriesCollection(1).Points(Points.Count).ApplyDataLabels Type:=xlShowValue应该将标签添加到系列 1 的最后一点。

Not sure why but I get the error不知道为什么,但我收到错误

Run-time error '424': Object required运行时错误“424”:需要对象

Here is my full code:这是我的完整代码:

Sub Graph()

    Dim my_range As Range, t, co As Shape 

    t = Selection.Cells(1, 1).Value + " - " + ActiveSheet.Name

    Dim OldSheet As Worksheet
    Set OldSheet = ActiveSheet

    Set my_range = Union(Selection, ActiveSheet.Range("A:A"))

    Set co = ActiveSheet.Shapes.AddChart2(201, xlLine) 'add a ChartObject

    With co.Chart
        .FullSeriesCollection(1).ChartType = xlXYScatter
        .FullSeriesCollection(1).AxisGroup = 1
        .FullSeriesCollection(1).Points(Points.Count).ApplyDataLabels Type:=xlShowValue
        .FullSeriesCollection(2).ChartType = xlLine
        .FullSeriesCollection(2).AxisGroup = 1
        .SetSourceData Source:=my_range
        'highlight final dot of data
        .HasTitle = True
        .ChartTitle.Text = t
        ResolveSeriesnames co.Chart
        .Location Where:=xlLocationAsObject, Name:="Graphs"

    End With

    OldSheet.Activate
End Sub

Here is my sample data这是我的示例数据

在此处输入图片说明

1. Selection.Cells(1, 1).Value + " - " + ActiveSheet.Name Avoid the use of + for concatenation. 1. Selection.Cells(1, 1).Value + " - " + ActiveSheet.Name避免使用+进行连接。 Use & .使用& + will give an error if the Selection.Cells(1, 1).Value is a numeric value.如果Selection.Cells(1, 1).Value是一个数值Selection.Cells(1, 1).Value+会报错。

2 Points.Count will give an error since it is not fully qualified. 2 Points.Count会报错,因为它不是完全合格的。 Use .FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count).ApplyDataLabels Type:=xlShowValue .使用.FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count).ApplyDataLabels Type:=xlShowValue

3. One small thing. 3.一件小事。 To show the datalabel of the last point use .FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count - 1).ApplyDataLabels Type:=xlShowValue .要显示最后一个点的数据标签,请使用.FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count - 1).ApplyDataLabels Type:=xlShowValue The point counting is 0 based.点数以0基础。 co.Chart.FullSeriesCollection(1).Points.Count will give you the total number of points which is always 1 less than what you can physically count. co.Chart.FullSeriesCollection(1).Points.Count会给你总点数,总点数总是比你可以实际计算的点数少 1。 What I mean is that the first point count will start at 0我的意思是第一个点数将从0开始

4. Similarly if you want to show the datalabel for 2nd series, use .FullSeriesCollection(2).Points(.FullSeriesCollection(2).Points.Count - 1).ApplyDataLabels Type:=xlShowValue 4.同样,如果要显示第二系列的数据标签,请使用.FullSeriesCollection(2).Points(.FullSeriesCollection(2).Points.Count - 1).ApplyDataLabels Type:=xlShowValue

5. From Chat: If your last data point doesn't have value then you will not see any data label for it (obviously). 5.来自聊天:如果您的最后一个数据点没有价值,那么您将看不到任何数据标签(显然)。 So if you want to backtrack and show the datalabel for the last value then find the last row in that column as shown Here and then ascertain whether you have to use (-1) or (-3) in .FullSeriesCollection(2).Points.Count - 1 to get the desired result.所以,如果你想走回头路,显示了最后一个值datalabel然后找到该列中的最后一排,如图在这里,然后确定是否必须使用(1)或(-3) .FullSeriesCollection(2).Points.Count - 1得到想要的结果。

Are you trying to achieve this?您是否正在努力实现这一目标?

Option Explicit

Sub Graph()
    Dim my_range As Range, t, co As Shape
    Dim OldSheet As Worksheet
    Set OldSheet = ActiveSheet

    t = Selection.Cells(1, 1).Value & " - " & ActiveSheet.Name

    Set my_range = Union(Selection, ActiveSheet.Range("A:A"))

    Set co = ActiveSheet.Shapes.AddChart2(201, xlLine) 'add a ChartObject

    With co.Chart
        .FullSeriesCollection(1).ChartType = xlXYScatter
        .FullSeriesCollection(1).AxisGroup = 1
        .FullSeriesCollection(1).Points(.FullSeriesCollection(1).Points.Count - 1).ApplyDataLabels Type:=xlShowValue

        .FullSeriesCollection(2).ChartType = xlLine
        .FullSeriesCollection(2).AxisGroup = 1

        .SetSourceData Source:=my_range
        'highlight final dot of data
        .HasTitle = True
        .ChartTitle.Text = t
        'ResolveSeriesnames co.Chart
        .Location Where:=xlLocationAsObject, Name:="Graphs"
    End With

    OldSheet.Activate
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM