Excel/VBA solution

Advertisement


Question
Sheet1
Sheet1  

Sheet2
Sheet2  
Hi Tom,

I have written a macro to create a bubble chart in Sheet2 from data that is in Sheet1. See pictures for better understanding.

In Sheet1 I have a lot of data that I want to use to create a Bubble Chart, but I don't want to use all the data in Sheet1. Specifically, I need rows 1,2,5, and 9 from columns E to the last column thatr is full. I am planning on adding more and more columns after the K (see picture), so I need something that is dynamic.

I currently select, copy and paste it onto Sheet2, and then from there I do the plotting. I want to avoid the copying and pasting altogether. Is there a way to select dynamically the data from Sheet1 to create the bubble chart in Sheet2?

Below is my VBA code.

Public Sub BubbleChart()
Dim BubbleChart As ChartObject, c As Long

   For Each BubbleChart In ActiveSheet.ChartObjects
       BubbleChart.Delete
   Next BubbleChart
   
   Sheets("Sheet1").Range("1:2,5:5,9:9").Copy
   Range("A1").PasteSpecial xlPasteValues
   Range("A:D").Delete
   Range("A1").CurrentRegion.Select

   If (Selection.Rows.Count <> 4 Or Selection.Columns.Count < 3) Then
       MsgBox "Please select your data. Selection must have 4 rows and at least 2 coloumns!"
       Exit Sub
   End If
   

   Set BubbleChart = ActiveSheet.ChartObjects.Add(Left:=50, Width:=1200, Top:=150, Height:=800)
   BubbleChart.Chart.ChartType = xlBubble
   d = Selection.Columns.Count
       With BubbleChart.Chart.SeriesCollection.NewSeries
       
         .Name = "Invisible1"
         .XValues = 4.5
         .Values = 6
         .BubbleSizes = 30
       End With
       
       
       With BubbleChart.Chart.SeriesCollection.NewSeries
         .Name = "Invisible2"
         .XValues = 4.5
         .Values = 6
         .BubbleSizes = 30
       End With
   'Dim c As Integer
   For c = 2 To Selection.Columns.Count
       With BubbleChart.Chart.SeriesCollection.NewSeries
       
         .Name = "=" & Selection.Cells(1, c).Address(External:=True)
         .XValues = Selection.Cells(2, c).Address(External:=True)
         .Values = Selection.Cells(3, c).Address(External:=True)
         .BubbleSizes = Selection.Cells(4, c).Address(External:=True)


       End With
       
   Next


Dim ser As Series

Set ser = BubbleChart.Chart.SeriesCollection(1)

 ser.ErrorBar Direction:=xlX, _
   Include:=xlErrorBarIncludeBoth, _
    Type:=xlErrorBarTypePercent, _
    Amount:=100
   ser.ErrorBars.Format.Line.ForeColor.RGB = RGB(7, 114, 255)
   ser.ErrorBars.Format.Line.Weight = 5
   ser.ErrorBars.Format.Line.DashStyle = msoLineSysDash
Set ser2 = BubbleChart.Chart.SeriesCollection(2)
 ser2.ErrorBar Direction:=xlY, _
   Include:=xlErrorBarIncludeBoth, _
    Type:=xlErrorBarTypePercent, _
    Amount:=100
   ser2.ErrorBars.Format.Line.ForeColor.RGB = RGB(7, 114, 255)
   ser2.ErrorBars.Format.Line.Weight = 5
   ser2.ErrorBars.Format.Line.DashStyle = msoLineSysDash

   BubbleChart.Chart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
   BubbleChart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(2, 1).Address(External:=True)
   
   BubbleChart.Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
   BubbleChart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(3, 1).Address(External:=True)
   
   BubbleChart.Chart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
   BubbleChart.Chart.Axes(xlCategory).MinimumScale = 2
   
   BubbleChart.Chart.Axes(xlCategory).Select
   BubbleChart.Chart.Axes(xlCategory).MinimumScale = 2.5
   BubbleChart.Chart.Axes(xlCategory).MaximumScale = 9
   BubbleChart.Chart.Axes(xlValue).Select
   BubbleChart.Chart.Axes(xlValue).MinimumScale = 3
   BubbleChart.Chart.Axes(xlValue).MaximumScale = 9
   BubbleChart.Chart.SeriesCollection(4).Select
   BubbleChart.Chart.ChartGroups(1).BubbleScale = 40
   BubbleChart.Chart.SetElement (msoElementChartTitleAboveChart)
   BubbleChart.Chart.ChartTitle.Text = "Projects"
       Selection.Format.TextFrame2.TextRange.Characters.Text = "Projects"
   With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
       .TextDirection = msoTextDirectionLeftToRight
       .Alignment = msoAlignCenter
   End With
   With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
       .BaselineOffset = 0
       .Bold = msoTrue
       .NameComplexScript = "+mn-cs"
       .NameFarEast = "+mn-ea"
       .Fill.Visible = msoTrue
       .Fill.ForeColor.RGB = RGB(0, 0, 0)
       .Fill.Transparency = 0
       .Fill.Solid
       .Size = 35
       .Italic = msoFalse
       .Kerning = 12
       .Name = "+mn-lt"
       .UnderlineStyle = msoNoUnderline
       .Strike = msoNoStrike
   End With
   


For Counter = 3 To (d + 1)
   ActiveChart.SeriesCollection(Counter).Points(1).Select
   ActiveChart.SeriesCollection(Counter).Points(1).ApplyDataLabels
   ActiveChart.SeriesCollection(Counter).DataLabels.Select
   Selection.ShowSeriesName = True
   Selection.ShowValue = False
   Selection.Format.TextFrame2.TextRange.Font.Size = 15
   Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue

   
Next Counter
   
   
For e = 1 To 2
   ActiveChart.SeriesCollection(e).Points(1).Select
   With Selection.Format.Fill
       .Visible = msoTrue
       .ForeColor.RGB = RGB(153, 204, 0)
       .Transparency = 1
       .Solid
   End With
Next e

For e = 3 To (d + 1)
   ActiveChart.SeriesCollection(e).Points(1).Select
   With Selection.Format.Fill
       .Visible = msoTrue
       .ForeColor.RGB = RGB(153, 204, 0)
       .Transparency = 0
       .Solid
   End With
Next e

   
   BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 63, 77, 138, 60). _
       Select
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
       "One"
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 3).ParagraphFormat. _
       FirstLineIndent = 0
   Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
   Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
   Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue

   
   BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 1100, 77, 80, 30). _
       Select
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
       "Two"
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 3).ParagraphFormat. _
       FirstLineIndent = 0
   Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
   Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
   Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
   
   
   BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 1100, 670, 80, 30). _
       Select
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
       "Three"
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 5).ParagraphFormat. _
       FirstLineIndent = 0
   Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
   Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
   Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
   
   
   BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 63, 660, 140, 60). _
       Select
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
       "Four"
   Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).ParagraphFormat. _
       FirstLineIndent = 0
   Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
   Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
   Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
   
   
   ActiveChart.Legend.Select
   Selection.Delete
   ActiveChart.Axes(xlCategory).AxisTitle.Select
   Selection.Format.TextFrame2.TextRange.Font.Size = 18
   ActiveChart.Axes(xlValue).AxisTitle.Select
   Selection.Format.TextFrame2.TextRange.Font.Size = 18
   
   ActiveChart.Axes(xlCategory).Select
   With Selection.TickLabels.Font
       .Size = 13
      ' .Bold = True
   End With
   ActiveChart.Axes(xlValue).Select
   With Selection.TickLabels.Font
       .Size = 13
      ' .Bold = True
   End With
   
   
   
'ActiveChart.Shapes.AddConnector(msoConnectorStraight, 395, 58, 395, 748).Select
'   With Selection.ShapeRange.Line
 '      .Visible = msoTrue
  '     .DashStyle = msoLineDash
   '    .Weight = 2.75
    '   .ForeColor.RGB = RGB(7, 114, 255)
     '  .Transparency = 0
  ' End With

   'ActiveChart.Shapes.AddConnector(msoConnectorStraight, 45, 400, 1175, 400).Select
   'With Selection.ShapeRange.Line
    '   .Visible = msoTrue
     '  .DashStyle = msoLineDash
      ' .Weight = 2.75
       '.ForeColor.RGB = RGB(7, 114, 255)
      ' .Transparency = 0
  ' End With
   


End Sub

Answer
Felipe,

this worked for me (using your data)

Public Sub BubbleChart()
Dim BubbleChart As ChartObject, c As Long
Dim lastcolumn As Long

If ActiveSheet.Name <> Worksheets("Sheet2").Name Then
  MsgBox "Make Sheet2 the ActiveSheet, then run the macro"
  Exit Sub
End If

  For Each BubbleChart In ActiveSheet.ChartObjects
      BubbleChart.Delete
  Next BubbleChart
  
'   Sheets("Sheet1").Range("1:2,5:5,9:9").Copy
'   Range("A1").PasteSpecial xlPasteValues
'   Range("A:D").Delete
'   Range("A1").CurrentRegion.Select
  With Worksheets("Sheet1")
    lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
  End With
'   If (Selection.Rows.Count <> 4 Or Selection.Columns.Count < 3) Then
'       MsgBox "Please select your data. Selection must have 4 rows and at least 2 coloumns!"
'       Exit Sub
'   End If
  

  Set BubbleChart = ActiveSheet.ChartObjects.Add(Left:=50, Width:=1200, Top:=150, Height:=800)
  BubbleChart.Chart.ChartType = xlBubble
  d = lastcolumn - 4
      With BubbleChart.Chart.SeriesCollection.NewSeries
      
        .Name = "Invisible1"
        .XValues = 4.5
        .Values = 6
        .BubbleSizes = 30
      End With
      
      
      With BubbleChart.Chart.SeriesCollection.NewSeries
        .Name = "Invisible2"
        .XValues = 4.5
        .Values = 6
        .BubbleSizes = 30
      End With
  'Dim c As Integer

  For c = 6 To lastcolumn
      With BubbleChart.Chart.SeriesCollection.NewSeries
      
        .Name = "=" & Worksheets("Sheet1").Cells(1, c).Address(External:=True)
        .XValues = "=" & Worksheets("Sheet1").Cells(2, c).Address(External:=True)
        .Values = "=" & Worksheets("Sheet1").Cells(5, c).Address(External:=True)
        .BubbleSizes = "=" & Worksheets("Sheet1").Cells(9, c).Address(External:=True)
      End With
      
  Next


Dim ser As Series

Set ser = BubbleChart.Chart.SeriesCollection(1)

ser.ErrorBar Direction:=xlX, _
  Include:=xlErrorBarIncludeBoth, _
   Type:=xlErrorBarTypePercent, _
   Amount:=100
  ser.ErrorBars.Format.Line.ForeColor.RGB = RGB(7, 114, 255)
  ser.ErrorBars.Format.Line.Weight = 5
  ser.ErrorBars.Format.Line.DashStyle = msoLineSysDash
Set ser2 = BubbleChart.Chart.SeriesCollection(2)
ser2.ErrorBar Direction:=xlY, _
  Include:=xlErrorBarIncludeBoth, _
   Type:=xlErrorBarTypePercent, _
   Amount:=100
  ser2.ErrorBars.Format.Line.ForeColor.RGB = RGB(7, 114, 255)
  ser2.ErrorBars.Format.Line.Weight = 5
  ser2.ErrorBars.Format.Line.DashStyle = msoLineSysDash

  BubbleChart.Chart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
  BubbleChart.Chart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(2, 1).Address(External:=True)
  
  BubbleChart.Chart.SetElement (msoElementPrimaryValueAxisTitleRotated)
  BubbleChart.Chart.Axes(xlValue, xlPrimary).AxisTitle.Text = "=" & Selection.Cells(3, 1).Address(External:=True)
  
  BubbleChart.Chart.SetElement (msoElementPrimaryCategoryGridLinesMajor)
  BubbleChart.Chart.Axes(xlCategory).MinimumScale = 2
  
  BubbleChart.Chart.Axes(xlCategory).Select
  BubbleChart.Chart.Axes(xlCategory).MinimumScale = 2.5
  BubbleChart.Chart.Axes(xlCategory).MaximumScale = 9
  BubbleChart.Chart.Axes(xlValue).Select
  BubbleChart.Chart.Axes(xlValue).MinimumScale = 3
  BubbleChart.Chart.Axes(xlValue).MaximumScale = 9
  BubbleChart.Chart.SeriesCollection(4).Select
  BubbleChart.Chart.ChartGroups(1).BubbleScale = 40
  BubbleChart.Chart.SetElement (msoElementChartTitleAboveChart)
  BubbleChart.Chart.ChartTitle.Text = "Projects"
      Selection.Format.TextFrame2.TextRange.Characters.Text = "Projects"
  With Selection.Format.TextFrame2.TextRange.Characters(1, 8).ParagraphFormat
      .TextDirection = msoTextDirectionLeftToRight
      .Alignment = msoAlignCenter
  End With
  With Selection.Format.TextFrame2.TextRange.Characters(1, 8).Font
      .BaselineOffset = 0
      .Bold = msoTrue
      .NameComplexScript = "+mn-cs"
      .NameFarEast = "+mn-ea"
      .Fill.Visible = msoTrue
      .Fill.ForeColor.RGB = RGB(0, 0, 0)
      .Fill.Transparency = 0
      .Fill.Solid
      .Size = 35
      .Italic = msoFalse
      .Kerning = 12
      .Name = "+mn-lt"
      .UnderlineStyle = msoNoUnderline
      .Strike = msoNoStrike
  End With
  


For Counter = 3 To (d + 1)
  ActiveChart.SeriesCollection(Counter).Points(1).Select
  ActiveChart.SeriesCollection(Counter).Points(1).ApplyDataLabels
  ActiveChart.SeriesCollection(Counter).DataLabels.Select
  Selection.ShowSeriesName = True
  Selection.ShowValue = False
  Selection.Format.TextFrame2.TextRange.Font.Size = 15
  Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue

  
Next Counter
  
  
For e = 1 To 2
  ActiveChart.SeriesCollection(e).Points(1).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(153, 204, 0)
      .Transparency = 1
      .Solid
  End With
Next e

For e = 3 To (d + 1)
  ActiveChart.SeriesCollection(e).Points(1).Select
  With Selection.Format.Fill
      .Visible = msoTrue
      .ForeColor.RGB = RGB(153, 204, 0)
      .Transparency = 0
      .Solid
  End With
Next e

  
  BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 63, 77, 138, 60). _
      Select
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
      "One"
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 3).ParagraphFormat. _
      FirstLineIndent = 0
  Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
  Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
  Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue

  
  BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 1100, 77, 80, 30). _
      Select
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
      "Two"
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 3).ParagraphFormat. _
      FirstLineIndent = 0
  Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
  Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
  Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
  
  
  BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 1100, 670, 80, 30). _
      Select
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
      "Three"
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 5).ParagraphFormat. _
      FirstLineIndent = 0
  Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
  Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
  Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
  
  
  BubbleChart.Chart.Shapes.AddTextbox(msoTextOrientationHorizontal, 63, 660, 140, 60). _
      Select
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
      "Four"
  Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).ParagraphFormat. _
      FirstLineIndent = 0
  Selection.ShapeRange.TextFrame2.TextRange.Font.Bold = msoTrue
  Selection.ShapeRange.TextFrame2.TextRange.Font.Size = 20
  Selection.ShapeRange.TextFrame2.TextRange.Font.Italic = msoTrue
  
  
  ActiveChart.Legend.Select
  Selection.Delete
  ActiveChart.Axes(xlCategory).AxisTitle.Select
  Selection.Format.TextFrame2.TextRange.Font.Size = 18
  ActiveChart.Axes(xlValue).AxisTitle.Select
  Selection.Format.TextFrame2.TextRange.Font.Size = 18
  
  ActiveChart.Axes(xlCategory).Select
  With Selection.TickLabels.Font
      .Size = 13
     ' .Bold = True
  End With
  ActiveChart.Axes(xlValue).Select
  With Selection.TickLabels.Font
      .Size = 13
     ' .Bold = True
  End With
  
  
  
'ActiveChart.Shapes.AddConnector(msoConnectorStraight, 395, 58, 395, 748).Select
'   With Selection.ShapeRange.Line
'      .Visible = msoTrue
 '     .DashStyle = msoLineDash
  '    .Weight = 2.75
   '   .ForeColor.RGB = RGB(7, 114, 255)
    '  .Transparency = 0
 ' End With

  'ActiveChart.Shapes.AddConnector(msoConnectorStraight, 45, 400, 1175, 400).Select
  'With Selection.ShapeRange.Line
   '   .Visible = msoTrue
    '  .DashStyle = msoLineDash
     ' .Weight = 2.75
      '.ForeColor.RGB = RGB(7, 114, 255)
     ' .Transparency = 0
 ' End With
  


End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.