Extracting R Sqaured value from a graph using CODE

TimeToSplit

New Member
Joined
Mar 30, 2012
Messages
4
Hi all,

I'm a bit stuck at the moment. I must graph a series of data points in an X-Y scatter point chart multiple times, then fit every type of trendline to that a seperate graph. Using VBA, I must then extract the R Sqaured value from each trendline, and find the best trendline suitable for the job.

I have no idea how to extract the R Sqaured value from the chart though, I have looked all around on forums online and in the Excel Help, and I'm still really stuck.

Thank you in advance,

TimeToSplit
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi,

You don't need to extract the R2 value from the chart, it can be derived from the worksheet function RSQ.

Assuming x's in A1:A5 and y's in B1:B5

Code:
R2Linear = Application.WorksheetFunction.RSq(Range("B1:B5"), Range("A1:A5"))
R2Exponential = Application.WorksheetFunction.RSq(Ln(Range("B1:B5")), Range("A1:A5"))
R2Log = Application.WorksheetFunction.RSq(Range("B1:B5"), Ln(Range("A1:A5")))
R2Power = Application.WorksheetFunction.RSq(Ln(Range("B1:B5")), Ln(Range("A1:A5")))
 
Upvote 0
Thanks, that does make it a lot easier. However, the macro won't compile now because of the Ln before the range in the exponential, log and power equations? I'm not sure why? I'm using excel 2010.
 
Upvote 0
Sorry - I was assuming some read across from WB functions into VBA

This will do it for you, assuming your data is in A1:B5, with 5 points - adjust as required.

Code:
Sub a()
Dim i As Integer

Dim x(1 To 5) As Double, y(1 To 5) As Double, xLog(1 To 5) As Double, yLog(1 To 5) As Double

'fill arrays
For i = 1 To 5
x(i) = Range("A" & i).Value
y(i) = Range("B" & i).Value
xLog(i) = Log(x(i))
yLog(i) = Log(y(i))
Next

'derive r squared
R2Linear = Application.WorksheetFunction.RSq(y(), x())
R2Exponential = Application.WorksheetFunction.RSq(yLog(), x())
R2Log = Application.WorksheetFunction.RSq(y(), xLog())
R2Power = Application.WorksheetFunction.RSq(yLog(), xLog())

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,341
Members
452,638
Latest member
Oluwabukunmi

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