2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Excel VBAを使ったバブルチャート作成

Posted at

「すべてExcelでできる! 経営力・診療力を高めるDPCデータ活用術」を読んでバブルチャートを作成するのに手間がかかるのでマクロを作成した。
方法として下記のデータを作成してから、バブルチャートにしたいデータを選択してからマクロを実行するとバブルチャートが作成されるというもの。

vba1.png
vba2.jpeg

Sub バブルチャート作成()
    Dim chartObj As ChartObject
    Dim ns As Series
    Dim r As String
    Dim s As String
    Dim sr As Range
    Dim wkAdr As String
    Dim nameAdr As String
    Dim xAdr As String
    Dim yAdr As String
    Dim bAdr As String
    Dim leng As Integer
    Dim line As Integer
    Dim cnt As Integer
    Dim i As Integer
    
    r = Selection.Address
    s = ActiveSheet.Name
    Set sr = Worksheets(s).Range(r)
    
    wkAdr = sr.Cells(1, 1).Address
    leng = InStr(2, wkAdr, "$")
    line = Int(Mid(wkAdr, leng + 1))
    nameAdr = "=" & s + "!" + Left(wkAdr, leng)
    wkAdr = sr.Cells(1, 2).Address
    leng = InStr(2, wkAdr, "$")
    xAdr = "=" & s + "!" + Left(wkAdr, leng)
    wkAdr = sr.Cells(1, 3).Address
    leng = InStr(2, wkAdr, "$")
    yAdr = "=" & s + "!" + Left(wkAdr, leng)
    wkAdr = sr.Cells(1, 4).Address
    leng = InStr(2, wkAdr, "$")
    bAdr = "=" & s + "!" + Left(wkAdr, leng)
    
    cnt = sr.Rows.Count
    
    Set chartObj = ActiveSheet.ChartObjects.Add(100, 80, 500, 300)
    chartObj.Chart.ChartType = xlBubble3DEffect
    chartObj.Chart.Legend.Delete
    
    For i = line To line + cnt - 1
        Set ns = chartObj.Chart.SeriesCollection.NewSeries
        ns.ChartType = xlBubble3DEffect
        ns.Name = nameAdr & i
        ns.XValues = xAdr & i
        ns.Values = yAdr & i
        ns.BubbleSizes = bAdr & i
        ns.ApplyDataLabels
        ns.DataLabels.ShowSeriesName = True
        ns.DataLabels.ShowValue = False
    Next
    
End Sub

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?