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

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.
Try:
Excel Formula:
Public Function myFunction(my_input As Double, my_power As Long) As Double

    Dim x As Long
 
    myFunction = my_input
 
    For x = 2 To my_power
        myFunction = myFunction - myFunction ^ x
    Next x

End Function

Other.xlsm
ABC
10.15560.125144703
Main
Cell Formulas
RangeFormula
C1C1=myfunction(A1,B1)


Note, in either normal Excel formula or VBA, you do not need
Excel Formula:
=+
as you show for B1, if the next number already is positive.
i.e. no point writing
Excel Formula:
=+myFunction(A1,B1)
in above, when
Excel Formula:
=myFunction(A1,B1)
suffices

Positive numbers are implied, negative numbers are explicit
 
Upvote 0
Try:
Excel Formula:
Public Function myFunction(my_input As Double, my_power As Long) As Double

    Dim x As Long
 
    myFunction = my_input
 
    For x = 2 To my_power
        myFunction = myFunction - myFunction ^ x
    Next x

End Function

Other.xlsm
ABC
10.15560.125144703
Main
Cell Formulas
RangeFormula
C1C1=myfunction(A1,B1)


Note, in either normal Excel formula or VBA, you do not need
Excel Formula:
=+
as you show for B1, if the next number already is positive.
i.e. no point writing
Excel Formula:
=+myFunction(A1,B1)
in above, when
Excel Formula:
=myFunction(A1,B1)
suffices

Positive numbers are implied, negative numbers are explicit

you understand my desire
but my only question is
when I tried the power of 6 the result is 0.125145252029647
and in this vba code
VBA Code:
Range("B1") = Range("A1") ^ 1 - Range("A1") ^ 2 - Range("A1") ^ 3 - Range("A1") ^ 4 - Range("A1") ^ 5 - Range("A1") ^ 6
I get 0.123531421875
why it's not same result ?
Thank you verymuch
 
Upvote 0
How About:

VBA Code:
Sub Calculate()
'
    MaxPower = 6                                                                ' <--- Set this value to the highest power you want to use
    OurNumber = Range("A1")
    Result = Application.WorksheetFunction.Power(OurNumber, 1)
'
    For ThisPower = 2 To MaxPower
        Result = Result - Application.WorksheetFunction.Power(OurNumber, ThisPower)
    Next
'
    Range("B1") = Result
End Sub
 
Upvote 0
How About:

VBA Code:
Sub Calculate()
'
    MaxPower = 6                                                                ' <--- Set this value to the highest power you want to use
    OurNumber = Range("A1")
    Result = Application.WorksheetFunction.Power(OurNumber, 1)
'
    For ThisPower = 2 To MaxPower
        Result = Result - Application.WorksheetFunction.Power(OurNumber, ThisPower)
    Next
'
    Range("B1") = Result
End Sub
function is very well
why we have not same result ?
please
 
Upvote 0
We? as in you and I? we do have same result.
we, My vba code is not the same you Vba code, for that I said WE
My vba code result is =
0.123531422​
and your Vba code (function code) result is =
0.125145252​
 
Upvote 0
Error in my suggestion, try:
VBA Code:
Public Function myFunction(my_input As Double, my_power As Long) As Double

    Dim x As Double
   
    myFunction = my_input
   
    For x = 2 To my_power
        myFunction = myFunction - Application.Power(my_input, x)
    Next x

End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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