Need help with 'sub or function not defined'

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
60
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
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,744
Office Version
  1. 365
Platform
  1. Windows
"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.
 
Solution

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
60
"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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,744
Office Version
  1. 365
Platform
  1. Windows
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).
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,896
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

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.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,896
Office Version
  1. 2010
Platform
  1. Windows
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.
 

ddander54

Board Regular
Joined
Oct 18, 2012
Messages
60
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.
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,896
Office Version
  1. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,128,126
Messages
5,628,859
Members
416,345
Latest member
sayad

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
Top