Excel/VBA plotting

Advertisement


Question
Sheet1
Sheet1  

Sheet2
Sheet2  
Hi Damon,

I don't know if you would consider this hard, but I have asked other experts an so far no one seems to be able to help. 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
Hi Felipe,

I suggest you simply add a "plot flag" column to your sheet.  In this column you can enter a value or flag in each cell to indicate whether you want the data in that row to plot (could be a TRUE/FALSE, 0/1, "yes"/"no" -- whatever you want).  Then simply do a Data > Filter on this column, causing Excel to hide the rows you don't want to plot on the bubble chart.  Excel automatically excludes hidden rows data from charts.  Whenever you want to change the rows that plot you can just edit the values in the plot flag column.

I hope you find this helpful.

Damon
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


Damon Ostrander

Expertise

I have extensive experience with VBA programming in Excel 5 through Excel 2013. As a former aerospace engineer with a large aerospace corporation and consultant in a small defense technology services company, I have developed a wide range of applications in VBA, including simulations involving mixed-language programming, satellite orbit mechanics, graphics and animation, and real-time applications. I am interested in moderate to hard VBA-related questions only.

Experience

I have developed and taught several courses in Excel VBA programming and also VBA programming in Office 97, 2000, and 2007. I have developed a number of large technical applications in Excel VBA for use within the aerospace industry.

Education/Credentials
B.S. in Electrical Engineering and Computer Science, University of California, Berkeley.

©2016 About.com. All rights reserved.