2

Please help , this is my first try to code something useful by VBA and I am self-learning now. And I got that above error . Please help

Sub Bezier()
    Dim C As Double, k As Integer, ansx As Double, ansy As Double, t As Double, n As Integer
    n = 3
    For i = 0 To 100
        t = i * 0.01
        ansx = 0
        ansy = 0
        For k = 0 To n
            C = (WorksheetFunction.Fact(n) / WorksheetFunction.Fact(k)) / WorksheetFunction.Fact(n - k)
            ansx = ansx + Cells(k + 2, 1).Value * C * WorksheetFunction.Power(t, k) * WorksheetFunction.Power(1 - t, n - k)
            ansy = ansy + Cells(k + 2, 2).Value * C * WorksheetFunction.Power(t, k) * WorksheetFunction.Power(1 - t, n - k)
        Next
        Cells(i + 2, 6).Value = ansx
        Cells(i + 2, 7).Value = ansy
    Next

End Sub

2 Answers 2

2

First of all, you should know, that some of functions, used on the worksheet, have limitations. So my point is avoid of using them in VBA, if it is not necessary.
For example, function POWER() returns error on attempt to raise a zero to zero. An alternative is to use 0 ^ 0 combination, which is exactly doing the same, but looks more simply and operates without such error.
But also there is no embedded alternative in VBA to the FACT() function, so you can use it, or simply add your own function factor() - it's uppon your choise.

If you just have started learning VBA, I would recomend you to use Option Explicit. It will help you to find out, which variables are not defined, and sometimes to avoid errors related to variable names missprint.

Here is your code, fixed and a little bit optimized:

Option Explicit' It is an option that turns on check for every used variable to be defined before execution. If this option is not defined, your code below will find undefined variables and define them when they are used. Good practice is to use this option, because it helps you, for example to prevent missprinting errors in variable names.

Sub Bezier()
    Dim C as Double , t As Double
    Dim k  As Long, n As Long, i As Long
    n = 3
    For i = 0 To 100
        t = i * 0.01
        Cells(i + 2, 6) = 0
        Cells(i + 2, 7) = 0
        For k = 0 To n
            C = (WorksheetFunction.Fact(n) / WorksheetFunction.Fact(k)) / WorksheetFunction.Fact(n - k)
            Cells(i + 2, 6) = Cells(i + 2, 6).Value + Cells(k + 2, 1).Value * C * (t ^ k) * ((1 - t) ^ (n - k))
            Cells(i + 2, 7) = Cells(i + 2, 7).Value + Cells(k + 2, 2).Value * C * (t ^ k) * ((1 - t) ^ (n - k))
        Next
    Next
End Sub

UPDATE
Here are some examples of factorial calculations.

    Public Function fnFact(number) ' a simple cycle example of Factorial function
    Dim tmp As Long ' new temporary variable to keep the "number" variable unchanged
    tmp = number
    fnFact = number
    While tmp > 1
        tmp = tmp - 1
        fnFact = fnFact * tmp
    Wend
End Function

Public Function fnFactR(number) ' a simple example of recursive function for Factorial calculation
    If number > 0 Then
        fnFactR = fnFactR(number - 1) * number ' function calls itself to continue calculations
    Else
        fnFactR = 1 ' function returns {1} when calculations are over
    End If
End Function

Sub Factor_test() 'RUN ME TO TEST ALL THE FACTORIAL FUNCTIONS
    Dim number As Long
    number = 170 ' change me to find Factorial for a different value
    MsgBox "Cycle Factorial:" & vbNewLine & number & "!= " & fnFact(number)
    MsgBox "WorksheetFunction Factorial:" & vbNewLine & number & "!= " & WorksheetFunction.Fact(number)
    MsgBox "Recursive Factorial:" & vbNewLine & number & "!= " & fnFactR(number)
End Sub

All those functions are available to calculate Factorial only for numbers before 170 inclusively, because of large result value.
So for my PC the limitation for WorksheetFunction.Fact() function is also 170.
Let me know, if your PC has different limitation for this function, - it's quite interesting thing. :)

UPDATE2
It is recomended to use Long data type instead of Integer each type when integer (or whole number) variable is needed. Long is slightly faster, it has much wider limitations and costs no additional memory. Here are proof links:
1. MSDN:The Integer, Long, and Byte Data Types
2. ozgrid.com:Long Vs Integer
3. pcreview.co.uk:VBA code optimization - why using long instead of integer?
Thanks for @Ioannis and @chris neilsen for the information about Long data type and proof links!

Good luck in your further VBA actions!

Sign up to request clarification or add additional context in comments.

6 Comments

Note that Dim k, n, i As Integer only declares i as Integer, k and n as Variant. You need to be explicit for each variable. Also you should use Long rather than Integer. It will be slightly faster and uses no more memory.
@chris neilsen, yes, you are right about declarations! I have updated my answer. But I am not agree about Long: Integer variables are stored as 16-bit, when Long (long integer) variables are stored as signed 32-bit.
@TooroSan check this and this out.
@Ioannis, Thanks for the links, but they are not official and I cannot find any confirmation for this on the MSDN or Technet resourses. My experimental code operating with 7 INTEGER arrays, filled by 64k entities (all of the Integer values) shown me at least the same productivity between Long and Integer variants (using very slow notebook with winXP, Intel Celeron(M) 1.4GHz and 256MB RAM). So I am still don't agree with this sentence about speed for Long, sorry. I would rather start another question for this. ;)
@TooroSan +1 for not believing me and for testing instead :) Here is the official link.
|
0

I can't find a pow method on the WorksheetFunction object. There is a Power method though. Perhaps you meant that?

6 Comments

I changed it , the same thing happened to me.
Now I have : runtime error 1004 application-defined or object-defined error
Typing ?WorksheetFunction.Power(0,0) into the immediate window results in an error. The Power function doesn't like your arguments.
0 to the power 0 is undefined, so you can't. Check your math.
Nope. From the linked page: [...] it is undefined (since x^y as a function of 2 variables is not continuous at the origin).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.