loop of this formula

kingman29

Board Regular
Joined
Jun 22, 2021
Messages
50
Office Version
  1. 2016
Platform
  1. Windows
Hello
I have a Monster function,
is that possible to make this function inVBA ?
A1 = 15%
B1 = +POWER(A1;1)-POWER(A1;2)-POWER(A1;3)-POWER(A1;4)-POWER(A1;5)
witch mean every time we subtract A1 with the increase of power

So My Vba code is :
VBA Code:
Sub Calculate()
Range("B1") = Range("A1") ^ 1 - Range("A1") ^ 2 - Range("A1") ^ 3 - Range("A1") ^ 4 - Range("A1") ^ 5 - Range("A1") ^ 6

End Sub
Is that anyway to make this Code more professional ?
what If I have Power of 56 times ?
Thank you *
note : My windows and Office in French Language
 

Attachments

  • resultat.png
    resultat.png
    150.2 KB · Views: 9
The difference in calculation results is caused by the way Excel and VBA handle Doubles.
If the head line of @JackDanIce's UDF had been written like
Rich (BB code):
Public Function myFunction(my_input As Range, my_power As Long) As Double
instead of
Rich (BB code):
Public Function myFunction(my_input As Double, my_power As Long) As Double
the outcome of the UDF and the original code of @kingman29 would be the same.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Thanks @GWteB I read about this afterwards, that Excel converts to double, regardless of cell value so passing in a range as argument makes more sense. I think I was trying to be cute/clever to leave out that conversion step or at least the process from sheet level into calc and output at sheet level.

I'm guessing it's some data type/conversion and then raised to a power, with the number of DPs needed for accuracy (2DP is almost air tight but lets not go there!)

Appreciate the explanation, I was curious :)
 
Upvote 0
@JackDanIce, I owe you an apology since I was terribly wrong :eek::cry:

I should have looked into this further but didn't. The moment I would post my contribution I saw there were already enough possible solutions, so I didn't post it. When I read the OP had discovered a difference in the end result, while the end result of my solution (also a UDF) matched his, I noticed the difference in argument declaration. My premature conclusion was that this had to be the cause (indeed on account of type conversion), but nothing could be further from the truth!!

If I put your post #2 code and my code together (and use the same variable names for better comparison) I think I don't need to explain anything further. Cheers.
Rich (BB code):
Public Function MyFunction(my_input As Double, my_power As Long) As Double
    
    ' JackDanIce
    Dim x As Long
    MyFunction = my_input
    For x = 2 To my_power
        MyFunction = MyFunction - MyFunction ^ x
    Next x
End Function


Public Function MyFunction(my_input As Range, my_power As Range) As Double
    
    ' GWteB
    Dim x As Long
    MyFunction = my_input.Value
    For x = 2 To my_power.Value
        MyFunction = MyFunction - my_input.Value ^ x
    Next x
End Function
 
Upvote 0
There is a formula to sum up a geometric series, which can be adapted for this.

Book1
ABCD
10.150.155
20.1235430.123543
Sheet16
Cell Formulas
RangeFormula
A2A2=POWER(A1,1)-POWER(A1,2)-POWER(A1,3)-POWER(A1,4)-POWER(A1,5)
C2C2=C1-((C1^2)*(1-C1^(D1-1)))/(1-C1)


No loop or even UDF needed.
 
Upvote 0
Ahaaa! So the Column A data is the old version and the Column C Data is your submission. Thanks for confusing me. LOL

Genius as always @Eric W !
 
Upvote 0
Not all Excel users are math wizards :sneaky:
Nice formula!
 
Upvote 0
Sorry about that! I should have been more clear. It also occurred to me that Excel probably has a function for that, and it does. So here are the updated versions:

Book1
ABCD
10.150.155
20.123543<--Original version0.123543<--Coding the formula directly
30.123543<-- Using the built-in function
40.123543<-- Excel 365 version
Sheet16
Cell Formulas
RangeFormula
A2A2=POWER(A1,1)-POWER(A1,2)-POWER(A1,3)-POWER(A1,4)-POWER(A1,5)
C2C2=C1-((C1^2)*(1-C1^(D1-1)))/(1-C1)
C3C3=A1-SERIESSUM(A1,2,1,ROW(INDIRECT("1:"&D1-1))^0)
C4C4=A1-SERIESSUM(A1,2,1,SEQUENCE(D1-1,,,0))
Press CTRL+SHIFT+ENTER to enter array formulas.



(Yes, I'm definitely a big math geek!)
 
Upvote 0

Forum statistics

Threads
1,215,013
Messages
6,122,694
Members
449,092
Latest member
snoom82

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