Variable Range in VBA Regression

EnergyModeler

New Member
Joined
Aug 16, 2019
Messages
7
Hello,

I am trying to automate a linear regression but want it to work for variable sized data sets. This is what I have for a given set. The Y data always starts at X5 and the X data always starts at Y5, but the length of the column changes. Any ideas as to how I would allow for the regression to fit the data that is input (i.e. have the "$X$106 and $AG$106 change depending on data)?? Thanks for the help!

Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range("$X$5:$X$106"), _
ActiveSheet.Range("$Y$5:$AG$106"), False, False, , "", False, False, _
False, False, , False
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this

Code:
Sub test()
  Dim x As Long, y As Long
  x = Range("X" & Rows.Count).End(xlUp).Row
  y = Range("Y" & Rows.Count).End(xlUp).Row
  Application.Run "ATPVBAEN.XLAM!Regress", ActiveSheet.Range("$X$5:$X$" & x), _
    ActiveSheet.Range("$Y$5:$AG$" & y), False, False, , "", False, False, _
    False, False, , False
End Sub
 
Upvote 0
Thank you for your reply. I tried your suggestion but it returns an error "Regression - LINEST() function returns error. Please check input ranges again."
 
Upvote 0
I figured it out!! It was taking into account empty cells below that had a hidden error message. Your code worked!! Thank you very much. It is appreciated!
 
Upvote 0

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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