2017-09-09 166 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