macro for graphing a dynamic range

cying

New Member
Joined
Jun 26, 2014
Messages
29
Hi. I'm trying to add to a macro to create a graph with a changing range. Most of this is from a recorded macro, so please bear with the inefficiencies and many .selects!

Code:
Sheets("Graph Data").Select
Dim lr3 As Long
lr3 = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & 1 & ":B" & lr3).Select

'recorded graph
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlBarClustered

ActiveChart.SetSourceData Source:=Range("'Chart Data'!$A$1:$B" & lr3)
'the above line is what the debugger doesnt approve of

ActiveChart.ApplyLayout (2)
ActiveChart.Legend.Select
Selection.Delete

ActiveSheet.ChartObjects("Chart 4").Activate
'should i rename the graph first so it'll always select the correct graph in case it isn't called chart 4 anymore?

ActiveChart.ChartTitle.Select
Selection.Format.TextFrame2.TextRange.Font.Size = 10
ActiveChart.ChartTitle.Text = "Referrals by Branch"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Referrals by Branch"
With Selection.Format.TextFrame2.TextRange.Characters(1, 19).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 19).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 = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).Select
Selection.Format.TextFrame2.TextRange.Font.Size = 8
ActiveChart.Axes(xlCategory).Select
ActiveChart.Axes(xlCategory).ReversePlotOrder = True
ActiveChart.ChartArea.Select
ActiveChart.Location Where:=xlLocationAsObject, Name:="Analysis"

So I'm trying to get columns A and B graphed, and they'll have a changing number of rows. I'm trying to get the range for the graph to be a rowcount.

Thanks!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Forum statistics

Threads
1,224,592
Messages
6,179,787
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top