2017-09-09 128 views
0

我有四列由几行组成。如下所示,有些信息是空的。VBA气泡图

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,我找到了一种方法来管理它,我调整了代码,使我正在寻找的东西,找到下面的汤姆一个。这里每个泡泡代表一个系列,所以有自己的标签。我现在唯一的问题是,我想告诉我的代码不要创建气泡,也就是说,当Z轴为空时,不要创建标签。

有什么想法?

我也试图找到标签顺序按照顺序泡的方式,也就是最高泡沫将首先等标签...

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 

非常感谢你对任何评析或帮助。

回答

2

只需添加if语句即可。

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