Calculate results from vertical range and horizontal range

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
If have two ranges. The first range is B2:B12 and when used against the UDF below, it produces Y = [-23.08 + 9.499(X1) + 1.979(X2)]

=BuildEquation(B3:B12,B2)

VBA Code:
Function BuildEquation(rng As Range, Rng2 As Range) As String
    Dim Cl As Range
    For Each Cl In rng
        If Cl.Value <> "" Then
            BuildEquation = BuildEquation & Cl.Value & "(" & Cl.Offset(0, -1).Value & ")" & " + "
        End If
    Next Cl
        BuildEquation = "Y = [" & Rng2.Value & " + " & Replace(Left(BuildEquation, Len(BuildEquation) - 3), "(a)", "") & "]"
End Function

The second range runs D2:M2

The above equation is one piece I need, but now the other is the actual mathematical answer. Right now I've used this formula

=B2+B3*D2+IF($B$4<>"",$B$4*E2,0)+IF($B5<>"",$B5*F2,0)+IF($B6<>"",$B6*G2,0)+IF($B7<>"",$B7*H2,0)+IF($B8<>"",$B8*I2,0)+IF($B9<>"",$B9*J2,0)+IF($B10<>"",$B10*K2,0)+IF($B11<>"",$B11*L2,0)+IF($B12<>"",$B12*M2,0)

but was hoping there was something more succinct to use whether it be another UDF or an alternative formula. The answer with this data is 817.98

Book1 (Recovered).xlsb
AB
1Coefficients
2Intercept-23.08
3X19.499
4X21.979
Sheet1


Book1 (Recovered).xlsb
DEFGHIJKLM
1X1X2X3X4X5X6X7X8X9X10
2756581761794835630
Sheet1
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
Excel Formula:
=B2+SUMPRODUCT(B3:B12,TRANSPOSE(D2:M2))
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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