简体   繁体   English

如何编辑此代码,使其仅适用于一张图表?

[英]How can I edit this code to make it only apply to one chart?

I found Hide data label containing series name if value is zero on Super User but it removes data labels that have a value of 0 for all charts: 我发现如果超级用户的值是零则隐藏包含系列名称的数据标签,但它会删除所有图表的值为0数据标签:

Sub RemoveZeroValueDataLabel()

'runs through every chart on the ActiveSheet
Dim cht As Chart
Dim chtObj As ChartObject

For Each chtObj In ActiveSheet.ChartObjects
    Set cht = chtObj.Chart

    Dim ser As Series
    For Each ser In cht.SeriesCollection

        Dim vals As Variant
        vals = ser.Values

        'include this line if you want to reestablish labels before deleting
        ser.ApplyDataLabels xlDataLabelsShowLabel, , , , True, False, False, False, False

        'loop through values and delete 0-value labels
        Dim i As Integer
        For i = LBound(vals) To UBound(vals)
            If vals(i) = 0 Then
                With ser.Points(i)
                    If .HasDataLabel Then
                        .DataLabel.Delete
                    End If
                End With
            End If
        Next i
    Next ser
Next chtObj
End Sub

I tried to edit it myself: 我试图自己编辑它:

Sub RemoveZeroValueDataLabelonlyonechart()
Dim cht As Chart
Dim chtObj As ChartObject

       Set cht = chtObj.Chart

    Dim ser As Series
    For Each ser In cht.SeriesCollection

        Dim vals As Variant
        vals = ser.Values

        'include this line if you want to reestablish labels before deleting
        ser.ApplyDataLabels xlDataLabelsShowLabel, , , , True, False, False, False, False

        'loop through values and delete 0-value labels
        Dim i As Integer
        For i = LBound(vals) To UBound(vals)
            If vals(i) = 0 Then
                With ser.Points(i)
                    If .HasDataLabel Then
                        .DataLabel.Delete
                    End If
                End With
            End If
        Next i
    Next ser
End Sub

But this returns: 但这返回:

Microsoft visual basic | Microsoft Visual Basic | Microsoft Run-time error '91' | 运行时错误“ 91” Object variable or With block variable not set 未设置对象变量或带块变量

How can I edit the code so it only removes data labels from the chart I have selected, not all charts in the sheet? 如何编辑代码,使其仅从我选择的图表中删除数据标签,而不是工作表中的所有图表?

Dim chtObj As ChartObject

In the original loop chtObj loops on all the chart objects in the ActiveSheet. 在原始循环中,chtObj在ActiveSheet中的所有图表对象上循环。 Here you want to set it only on a specific Chart object, so you removed the For loop, fine. 在这里,您只想在特定的Chart对象上进行设置,因此删除了For循环。 But your chtObj, which you defined as a reference to a ChartObject, references nothing up till now. 但是您的chtObj(定义为对ChartObject的引用)到目前为止没有引用任何内容。 You need to assign it to some Chart object. 您需要将其分配给某些Chart对象。 You need to know either the name or the index of the Chart object you want to modify. 您需要知道要修改的Chart对象的名称或索引。 Then you will add one simple line after the one above: 然后,您将在上面的一行之后添加一行简单的代码:

Set chtObj = ActiveSheet.ChartObjects("someName")

or, if the chart is the first one created within that worksheet: 或者,如果图表是在该工作表中创建的第一个图表:

Set chtObj = ActiveSheet.ChartObjects(1)

After you add one of these two lines, with the appropriate name or number that corresponds to the target Chart, the rest of the code should work fine. 在添加这两行之一后,使用与目标图表相对应的适当名称或数字,其余代码应能正常工作。

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

相关问题 在excel中,如何编辑现有图表? - In excel, how can I edit an existing chart? 如何使我的代码仅查看字符串的一部分 - How can I make my code only look at part of a string 如何编辑此代码以仅使用选择的行/单元格而不是整个工作表 - How can I edit this code to work only a selection of rows/cells and not the whole worksheet 创建一个图表,该图表可以将另一张工作表中的 plot 个值放入名为“countChart”的图表中。 我的工作簿中只想要一张图表 - Create one chart which can will plot values from another sheet into a chart named as "countChart". i want only one chart in my workbook 请帮忙。任何人都知道,只有值大于0时,我才能制作一张图表来汇总每周的所有数据吗? - Please help..Anyone know how can I make a chart to sum all the data of each week only if values are greater than 0? 如何使此宏录制应用于整个工作表? - How can I make this Macro Recording apply to entire sheet? 如何创建仅显示最近4周的动态图表? - How can I create a dynamic chart that only shows the last 4 weeks? 如何使用用户输入编辑代码中的参数? - How can I edit parameters in my code with user input? 如何将我的VBA代码应用于一系列单元格? - How can I apply my VBA code to a range of cells? 如何将循环和 if 语句应用于此代码以加快速度? - How can I apply loops and if statements to this code to speed it up?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM