Could we make an excel formula shorter with use UDF function?

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
I have this formula in excel, which is very long:
1576873918422.png


and this is the BVA code, which is generated once I record a macro:

VBA Code:
Sub tESTpRICE()
'
' tESTpRICE Macro
'

'
    Range("AB2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-26]=1,RC[-1],IF(RC[-26]=2,RC[-1]+R[1]C[-1],IF(RC[-26]=3, RC[-1]+R[1]C[-1]+R[2]C[-1],IF(RC[-26]=4, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1],IF(RC[-26]=5, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1],IF(RC[-26]=6, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1],IF(RC[-26]=7, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1],IF(RC[-26]=8" & _
        "+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1],IF(RC[-26]=9, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1],IF(RC[-26]=10, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1]+R[9]C[-1],IF(RC[-26]=11, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[" & _
        "[8]C[-1]+R[9]C[-1]+R[10]C[-1],IF(RC[-26]=12, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1]+R[9]C[-1]+R[10]C[-1]+R[11]C[-1],IF(RC[-26]=13, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1]+R[9]C[-1]+R[10]C[-1]+R[11]C[-1]+R[12]C[-1],IF(RC[-26]=14, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-" & _
        "-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1]+R[9]C[-1]+R[10]C[-1]+R[11]C[-1]+R[12]C[-1]+R[13]C[-1],IF(RC[-26]=15, RC[-1]+R[1]C[-1]+R[2]C[-1]+R[3]C[-1]+R[4]C[-1]+R[5]C[-1]+R[6]C[-1]+R[7]C[-1]+R[8]C[-1]+R[9]C[-1]+R[10]C[-1]+R[11]C[-1]+R[12]C[-1]+R[13]C[-1]+R[14]C[-1])))))))))))))))"
    Range("AB2").Select
    Selection.AutoFill Destination:=Range("AB2:AB12459")
    Range("AB2:AB12459").Select
End Sub

Is it possible to create a UDF formula, because there are a problem with using this long formula

Thank you very much
 

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
No need for a UDF, assuming that col B will always be 1 or greater you can use
=SUM(AA2:INDEX(AA2:AA100,B2))
 
Upvote 0
Thank you,
The new formula is working, and the Marco is working too

but do you thinks this error will affect?
1576876390539.png
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,986
Messages
6,182,156
Members
453,093
Latest member
Soffy

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