|
-
Nov 16th, 2011, 10:35 AM
#1
Thread Starter
New Member
[RESOLVED] VB Function: #Name? Error
Hi
I'm new to VB and think I'm missing something basic here. I've written a function called Test2, but whenever I type "=Test2(670)" into my worksheet for example, I get the #Name? error.
Do I need to set up/declare the variable somewhere else as well?
Any advice much appreciated. Thanks.
Test2 Code:
Option Explicit
Function Test2(Tinit As Double)
Dim cp1() As Double
' calculate specific heat
If (Tinit < 600) Then
cp1 = (425 + (0.773 * Tinit) - (1.69 * 10 ^ -3 * (Tinit ^ 2)) + (2.22 * 10 ^ -6 * (Tinit ^ 3)))
ElseIf ((600 <= Tinit) And (Tinit < 735)) Then
cp1 = 666 + 13002 / (738 - Tinit)
ElseIf ((735 <= Tinit) And (Tinit < 900)) Then
cp1 = (545 + 17820 / (Tinit - 731))
ElseIf ((900 <= Tinit) And (Tinit <= 1200)) Then
cp1 = 650
End If
Test2 = cp1
End Function
-
Nov 16th, 2011, 10:44 AM
#2
Re: VB Function: #Name? Error
Excel VBA question moved to Office Development
Welcome to the forums. 
If you write a function in code then you must call it from code. Using the Excel command line isn't going to excecut a function you wrote. It will only execute built in Excel commands.
Probably the easiest way to use your function would be from a command button which you could put on your spreadsheet.
-
Nov 16th, 2011, 03:38 PM
#3
Re: VB Function: #Name? Error
using a function as a custom worksheet function works well, note that it may not auto update as often as builtin worksheet functions
this function appears to work fine, except that you declare cp1 as an array of values, change to single value only
Dim cp1 As Double
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 17th, 2011, 01:49 AM
#4
Re: VB Function: #Name? Error
IMHO the code needs to be in a module!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 17th, 2011, 03:21 AM
#5
Re: VB Function: #Name? Error
IMHO the code needs to be in a module!
i assumed it was, but that may have been incorrect, it is not specified either way, but the error indicates that is the problem
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Nov 17th, 2011, 04:00 AM
#6
Re: VB Function: #Name? Error
If I wouldn't have made this mistake, I wouldn't have noticed!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 17th, 2011, 04:41 AM
#7
Thread Starter
New Member
Re: VB Function: #Name? Error
Thanks for all the help!
Its working now. I think it was because I declared the variable as an array. It was already in a Module. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|