使用VBA添加数据标签

时间:2018-01-17 13:41:58

标签: excel-vba vba excel

我有一个基于复选框 Sub ,我希望在图表上显示数据标签并隐藏它们之间切换。我录制了宏并将其修改为不再说"选择" " ActiveChart" 因为我不想要让用户选择图表。我认为这就是我的问题所在,因为它绝对没有。

我似乎无法缩小这个问题的范围,因为它是一个相当长的一块。任何帮助将不胜感激!

Sub Checkbox()
Dim Cht As ChartObject
Set Cht = Sheet5.ChartObjects("Chart 12")
    If Sheet1.Range("W11") = "TRUE" Then
    Cht.Chart.SetElement (msoElementDataLabelTop)
    Cht.Chart.ApplyDataLabels
    Cht.Chart.FullSeriesCollection(1).DataLabels.Select
    Cht.ShowCategoryName = True
    Cht.Separator = "" & Chr(13) & ""
    With Cht.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(127, 127, 127)
        .Solid
    End With
    With Cht.Format.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0
        .Transparency = 0
        .Solid
    End With
    With Cht.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(68, 114, 196)
        .Transparency = 0.75
        .Solid
    End With
    Application.CommandBars("Format Object").Visible = False
    Else: Cht.Chart.SetElement (msoElementDataLabelNone)
    End If
End Sub

我刚才被告知最好引用一个单元格而不是一个复选框,因此我让代码在W11中查找" TRUE"或者" FALSE"。如果看起来很实用,我宁愿使用复选框。

3 个答案:

答案 0 :(得分:1)

尝试以下操作,我相信您错误地引用了图表,通过使用Shapes对象,您可以访问所需的图表:

Sub Checkbox()
    If Sheet1.Range("W11") = True Then
        Sheet1.Shapes("Chart 1").Chart.SetElement (msoElementDataLabelShow)
        Sheet1.Shapes("Chart 1").Chart.ApplyDataLabels
        Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(1).DataLabels.Select
        Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(1).DataLabels.ShowCategoryName = True
        Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(1).DataLabels.Separator = "" & Chr(13) & ""
        With Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(1).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(127, 127, 127)
            .Solid
        End With
        With Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(1).Format.Fill
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorAccent1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0
            .Transparency = 0
            .Solid
        End With
        With Sheet1.Shapes("Chart 1").Chart.FullSeriesCollection(2).Format.Fill
            .Visible = msoTrue
            .ForeColor.RGB = RGB(68, 114, 196)
            .Transparency = 0.75
            .Solid
        End With
        Application.CommandBars("Format Object").Visible = False
    Else
        Sheet1.Shapes("Chart 1").Chart.SetElement (msoElementDataLabelNone)
    End If
End Sub

答案 1 :(得分:1)

问题在于

If Sheet1.Range("W11") = "TRUE"

不是true

如果您Debug.Print Sheet1.Range("W11") = "TRUE",它将返回false而不是预期的true。原因是Sheet1.Range("W11")在这种情况下返回boolean而不是string

您可以使用

验证返回类型
Debug.Print VarType(Sheet1.Range("W11"))

返回11,表示boolean(请参阅VarType Function)。

所以你应该把它改成

If Sheet1.Range("W11") = True

如果单元格W11包含true,则为TRUE

答案 2 :(得分:0)

感谢帮助,我能够找出问题所在。以下是最终代码:

Sub Checkbox()
    If Sheet1.Range("W11") = True Then
    Sheet5.Shapes("Chart 12").Chart.SetElement (msoElementDataLabelTop)
    Sheet5.Shapes("Chart 12").Chart.ApplyDataLabels
    Sheet5.Shapes("Chart 12").Chart.FullSeriesCollection(1).DataLabels.Select
    Selection.ShowCategoryName = True
    Selection.Separator = "" & Chr(13) & ""
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(127, 127, 127)
        .Solid
    End With
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0
        .Transparency = 0
        .Solid
    End With
    With Selection.Format.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(68, 114, 196)
        .Transparency = 0.75
        .Solid
    End With
    Application.CommandBars("Format Object").Visible = False
        Else
        Sheet5.Shapes("Chart 12").Chart.FullSeriesCollection(1).DataLabels.Select
        Sheet5.Shapes("Chart 12").Chart.SetElement (msoElementDataLabelNone)
    End If
End Sub