how to select current range

M

mikerr

Hi all,
While I'm comfortable with programming, I'm rusty and I have never see
VBA before.

I'm trying to make a macro that makes a graph with trendline based o
whatever range of numbers I have selected. I guess that means that i
the code below, I want to change the sheet and range references t
whatever is currently selected. Your answer will be appreciated.
Thanks.

Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("R
INT").Range("AC12:AC15")
ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear
Forward:=0, _
Backward:=0, DisplayEquation:=True
DisplayRSquared:=False).Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 141
Selection.Top = 1
End Su
 
T

Tom Ogilvy

Possibly:

Charts.Add
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Selection
ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear,
Forward:=0, _
Backward:=0, DisplayEquation:=True,
DisplayRSquared:=False).Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 141
Selection.Top = 1
End Sub
 
M

mikerr

Thanks, Tom

Strangely, I'm getting an error of "run-time error 13, type-mismatch. "
The debugger then throws me back to the line:

ActiveChart.SetSourceData Source:=Selection

This seems to be happening regardless of what sheet or cell range I am
on. As far as I can tell, this is the only code below that you
recommended changed.

Again, I do appreciate your help (or anyone else's) as this would save
me quite a few keystrokes.
 
T

Tom Ogilvy

Then try:

ActiveChart.SetSourceData Source:=Selection.Address(1,1,xlR1C1,True)
 
M

mikerr

Well, as it turns out, I managed to find a different way:

Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:="R1 INT"
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Trendlines.Add(Type:=xlLinear
Forward:=0, _
Backward:=0, DisplayEquation:=True
DisplayRSquared:=False).Select
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Selection.Left = 142
Selection.Top = 1
End Sub

I'm not quite sure how it works, but it does. Peltiertech.co
recommended changing the order of 2 of the lines, and that did th
trick.

Only 1 problem left: It puts these graphs on the "R1 Int" Sheet, whe
I want it on whatever is my active sheet. I tried:

ActiveChart.Location Where:=xlLocationAsObject, Name:=ActiveSheet

but it throws me back a "run-time error 5". Any ideas? Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top