Need help with 'sub or function not defined'

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
97
I found this procedure (credit to Orrin White), that I thought was interesting to give a try, but I'm having an issue getting it to run in O365 with a sub or function not defined error.
It stops on this line: r = Root(r, (n))

It's way over my head, so I'm wondering if someone can help?


VBA Code:
Sub Stars()


Dim Imaginary As Boolean, n, h, c As Integer
Dim Pi, i, j, k, l, r, s, t, p, x, y As Single, SCin, SCout As Long
Dim u, v, w, z As Single, OO As Range
Dim Replace As Boolean: Replace = True
SCin = ActiveSheet.Shapes.Count: t = 0: c = 8
Set OO = ActiveCell
    u = OO.Height
    v = OO.Width
    w = OO.Top
    z = OO.Left
    s = Application.InchesToPoints(0.006): Pi = GetPi
    n = 6: r = Sqr(2) ^ n: Imaginary = True
If r < 0 Then t = Pi
If Imaginary Then
If r < 0 Then t = 3 * Pi / 2
If r > 0 Then t = Pi / 2
End If
    r = Abs(r)
    r = Root(r, (n))
    p = t


DrawStar: i = r * Cos(t) * v + z: j = -r * Sin(t) * u + w


For h = 2 To n + 1
    t = p + h * (2 * Pi / n)
    i = r * Cos(t) * v + z
    j = -r * Sin(t) * u + w
If lsEven((n)) Then
    t = t + (n - 2) / n * Pi
    Else:
    t = t + (n - 1) / n * Pi
End If
k = r * Cos(t) * v + z
i = -r * Sin(t) * u + w
ActiveSheet.Shapes.AddLine(k, i, i, j).Select
Selection.Name = "L" & h - 1
Next h
SCout = ActiveSheet.Shapes.Count
For h = SCout To SCin + 1 Step -1
ActiveSheet.Shapes(h).Select Replace: Replace = False
Next h
Selection.Group: c = n
ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Select
Selection.ShapeRange.Line.ForeColor.SchemeColor = c
OO.Select
End Sub


Function lsEven(n As Integer) As Boolean
If n Mod 2 = 0 Then lsEven = True
End Function


Thanks in advance,
Don
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
"Root" is not a standard VBA function. It is probably a User Defined Function or Procedure, whose code you need to copy over to (just like with the "IsEven" function at the bottom of your code).

So you might have missed copying something that you need. I would recommend going back to where you found this code, and see if there is a part of it you missed copying over.
 
Upvote 0
Solution
"Root" is not a standard VBA function. It is probably a User Defined Function or Procedure, whose code you need to copy over to (just like with the "IsEven" function at the bottom of your code).

So you might have missed copying something that you need. I would recommend going back to where you found this code, and see if there is a part of it you missed copying over.
Thanks Joe4....that makes sense as I couldn't find anything online for 'root' as it might refer to VBA
 
Upvote 0
You are welcome.

A lot of times, the error messages are cryptic and it isn't always evident what the problem might be. But in this case, it is actually quite helpful.
Basically, VBA does not recognize it as a standard VBA function, so it assumes that it must be a User Defined Function (or procedure).
It looks for it, but it cannot find it. So that is what it is telling you. That there is no User Defined Sub Procedure or Function named "root".

Note that in addition to User Defined Functions/Procedures, sometimes custom functions are stored in Add-Ins. So that is another possibility, that the original creator of this code is using an Add-In (but they would probably mention that somewhere in the link where you got this code).
 
Upvote 0
Just so you know, when you write a Dim statement like this...

Dim Pi, i, j, k, l, r, s, t, p, x, y As Single, SCin, SCout As Long

only y will be a Single and only SCout will be Long... all the rest will be Variants. Unlike other languages, each variable must be declare individually as to its data type.
 
Upvote 0
When you wrote this...

r = Root(r, ( n ) )

is the intention to take the nth root of r? If so, you don't need a separate function for that as you can write it this way directly in VBA...

r = r ^ (1 / n)

where I assume the parentheses you placed around n in your original code are functionally unnecessary.
 
Upvote 0
When you wrote this...

r = Root(r, ( n ) )

is the intention to take the nth root of r? If so, you don't need a separate function for that as you can write it this way directly in VBA...

r = r ^ (1 / n)

where I assume the parentheses you placed around n in your original code are functionally unnecessary.
Rick,

I can't take credit for the code as I found it online, but I thank you for the explanation. I couldn't even begin to be able to code this.
I did replace the line in the code with your line and now the sub at least runs thru to the end and did draw a line on the sheet, just not what I expected (single straight line vs a 'star').
I was able to see what you mean with the variable declarations as written in the local window. Another thing I noticed was that there was no value for GetPi, so I'm assuming that must be another UDF that wasn't included in what I found online.

Thanks for the education.
 
Upvote 0
Another thing I noticed was that there was no value for GetPi, so I'm assuming that must be another UDF that wasn't included in what I found online.
Assuming GetPi is meant to retrieve the value of Pi, you can use this...

[PI()]

in place of GetPi
 
Upvote 0

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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