【问题标题】:How can I create a chart from unique values in a range如何从范围内的唯一值创建图表
【发布时间】:2021-01-28 07:59:43
【问题描述】:

我有一些项目正在通过用户表单填充到工作表中。当我打开工作簿时,我试图让工具转到工作表并获取数据并在主登录表上生成图表/仪表板。

在数据范围内包含状态。我希望 VBA 浏览一列数据并创建一个图表来计算每个不同的状态并将其放入条形图中。

yaxis = 不同的状态
xaxis = 计数

到目前为止我的代码

Sub populatecharts()
    Dim ws As Worksheet
    Dim ch As Chart
    Dim tablerng As Range
    Dim rng1 As Range
    Dim rng2 As Range
    Dim rng3 As Range
    Dim sh As String

    Set ws = ActiveSheet

    'When the workbook opens it should always check the data and populate the BA Dashboard
    'I need to check for sheets and if they exist generate a chart from the data

    sh = "Action"
    On Error Resume Next

    Worksheets("Action").Visible = True

    If CheckSheetExist(sh) = False Then
        GoTo nextchart1
    Else
        Worksheets(sh).Activate
        'Set ws = ActiveSheet
        Set rng1 = Range("G4", Range("G4", "G4").End(xlDown))
        rng1.Select
        'Set rng2 = Range("B2")
        'Set rng3 = Range("C3")
        'Set tablerng = rng1 '& rng2 & rng3
        Set ch = ws.Shapes.AddChart2(Width:=200, Height:=200, Left:=Range("B4").Left, Top:=Range("B4").Top).chart

        With ch
            .SetSourceData Source:=rng1
            .ChartType = xlBarClustered
            .ChartTitle.Text = "Action Items by Status"
        End With

        ws.Activate
        Worksheets("Action").Visible = False
    End If

看起来很简单,但我无法考虑清楚,即使我定义了顶部和底部以及大小,位置也会受到影响。有时它在我选择作为左侧的单元格的右侧。

【问题讨论】:

  • 首先删除On Error Resume Next这一行隐藏了所有错误信息,但错误仍然出现。这就像说 VBA “如果有错误不要告诉任何人”,但是如果您没有看到错误,您将无法修复它们,因此您的代码将无法工作。 • 删除它,看看你得到了哪些错误,然后修复它们(或告诉它们是什么以及你在哪一行得到它们)。 • 您可能会从阅读VBA Error Handling – A Complete Guide 中受益。
  • 感谢关于 on 错误的提示。我想我可能已经发布了未保存的版本。 tablerng 不应该等于 range() 并且我将 .chart 添加到设置图表对象的末尾。代码执行,但图表不是我想要的,(见图)一列数据多个状态,我想要一列代表每个状态,y 轴提供每个状态有多少的计数。谢谢
  • 看什么图片?要看到什么“保存”版本?您是否编辑了您的问题并进行了上述更改?我看不出有什么改变。如果您尝试编辑,您是否保存了它?
  • 找到了编辑按钮。并进行了编辑。图像有状态和一些数据选项“打开”、“关闭”、“进行中”,这些将是 y 轴
  • 我仍然看不到任何图表图像......无论如何,在你这样做之前,我发布了一个答案代码。你测试了吗?它不会返回您需要的东西吗?

标签: excel vba unique excel-charts


【解决方案1】:

请尝试下一个方法。它使用字典来提取唯一值及其计数和数组以提供必要的系列。尝试在活动工作表上运行它,并在确认返回的内容是您需要的内容后,才根据您的情况调整它:

Sub populatecharts()
  Dim shT As Worksheet, ch As Chart, lastRow As Long
  Dim arrY, arrX, i As Long, dict As Object
  
  Set shT = ActiveSheet 'use here the sheet you need
  lastRow = shT.Range("G" & shT.Rows.count).End(xlUp).row
  arrX = shT.Range("G4:G" & lastRow).Value        'put the range in a array
  Set dict = CreateObject("Scripting.Dictionary") 'needed for the next step
  
  On Error Resume Next
   shT.ChartObjects("MyChartXY").Delete   'for the case of re running need
  On Error GoTo 0

  For i = 1 To UBound(arrX)
    If Not dict.Exists(arrX(i, 1)) Then
        dict(arrX(i, 1)) = 1                    'create the unique keys
    Else
        dict(arrX(i, 1)) = dict(arrX(i, 1)) + 1 'increment the key next occurrrence
    End If
  Next i
  arrX = dict.Keys: arrY = dict.Items           'extract the necessary arrays
  
  Set ch = shT.ChartObjects.Add(left:=shT.Range("B4").left, _
            top:=shT.Range("B4").top, width:=200, height:=200).Chart
  With ch
    .ChartType = xlBarClustered
    .HasTitle = True
    .ChartTitle.Text = "Action Items by Status"
    .SeriesCollection.NewSeries.Values = arrY   'feed it with the array elements
    .SeriesCollection(1).XValues = arrX         'feed it with the array elements
  End With
End Sub

请进行测试并发送一些反馈。

【讨论】:

  • @John Sanders 你没有时间来测试上面的代码吗?如果经过测试,它是否会返回您需要的 wgat?
  • 嗨,对不起,我在 PST,所以我今天要回去了。现在测试。将很快更新结果。谢谢。
  • 是的,这确实有效,给了我我需要的东西!谢谢!。我需要尝试获得最佳外观的选项,但这取决于我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
相关资源
最近更新 更多