首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA气泡图

VBA气泡图
EN

Stack Overflow用户
提问于 2017-09-09 14:44:16
回答 1查看 2.4K关注 0票数 0

我有四根由几行组成的柱子。有些是充满信息的,有些是空的,如下所示。

代码语言:javascript
复制
 w     x     y   z
Blue   23   74   120
White  50   25   34
Grey   11   45   
Yellow 25   12   12
Black  11   22 

我想要做的是让每一行代表一个‘泡泡’在一个泡泡excel图。气泡的大小将等于z。多亏了汤姆·霍兰德( Tom Hollander ),我找到了一种管理它的方法,我调整了代码,使我正在寻找的东西,在下面发现汤姆的one.Here每个气泡代表一个意甲,所以有自己的标签。我现在唯一的问题是,当Z轴为空时,我想告诉我的代码不要创建气泡,也就是不要创建标签。

有什么想法吗?

我还试图找到一种方式,标签顺序遵循气泡顺序,即最高的气泡将首先有标签,等等。

代码语言:javascript
复制
Public Sub CreateMultiSeriesBubbleChart()
    If (selection.Columns.Count <> 4 Or selection.Rows.Count < 3) Then
        MsgBox "Selection must have 4 columns and at least 2 rows"
        Exit Sub
    End If

    Dim bubbleChart As ChartObject
    Set bubbleChart = ActiveSheet.ChartObjects.Add(Left:=selection.Left, 
Width:=600, Top:=selection.Top, Height:=400)
    bubbleChart.chart.ChartType = xlBubble
    Dim r As Integer
    For r = 2 To selection.Rows.Count
        With bubbleChart.chart.SeriesCollection.NewSeries
            .Name = "=" & selection.Cells(r, 1).Address(External:=True)
            .XValues = selection.Cells(r, 2).Address(External:=True)
            .Values = selection.Cells(r, 3).Address(External:=True)
            .BubbleSizes = selection.Cells(r, 4).Address(External:=True)
        End With

    Next

    bubbleChart.chart.SetElement 
(msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    bubbleChart.chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "=" & 
selection.Cells(1, 2).Address(External:=True)

    bubbleChart.chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
    bubbleChart.chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "=" & 
selection.Cells(1, 3).Address(External:=True)

    bubbleChart.chart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
    bubbleChart.chart.Axes(xlCategory).MinimumScale = 0
End Sub

非常感谢您的光临或帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-09 15:56:11

只需添加if语句即可。

代码语言:javascript
复制
Public Sub CreateMultiSeriesBubbleChart()
    If (Selection.Columns.Count <> 4 Or Selection.Rows.Count < 3) Then
        MsgBox "Selection must have 4 columns and at least 2 rows"
        Exit Sub
    End If

    Dim bubbleChart As ChartObject
    Set bubbleChart = ActiveSheet.ChartObjects.Add(Left:=Selection.Left, Width:=600, Top:=Selection.Top, Height:=400)
    bubbleChart.Chart.ChartType = xlBubble
    Dim r As Integer
    For r = 2 To Selection.Rows.Count
        If Selection.Cells(r, 4) <> "" Then '<~~ z is not empty
            With bubbleChart.Chart.SeriesCollection.NewSeries
                .Name = "=" & Selection.Cells(r, 1).Address(External:=True)
                .XValues = Selection.Cells(r, 2).Address(External:=True)
                .Values = Selection.Cells(r, 3).Address(External:=True)
                .BubbleSizes = Selection.Cells(r, 4).Address(External:=True)
            End With
        End If
    Next

    bubbleChart.Chart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
    bubbleChart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(1, 2).Address(External:=True)

    bubbleChart.Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
    bubbleChart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(1, 3).Address(External:=True)

    bubbleChart.Chart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
    bubbleChart.Chart.Axes(xlCategory).MinimumScale = 0
End Sub
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46131824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档