Results 1 to 7 of 7

Thread: [RESOLVED] VB Function: #Name? Error

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Resolved [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:
    1. Option Explicit
    2.  
    3. Function Test2(Tinit As Double)
    4.                                          
    5.   Dim cp1() As Double
    6.    
    7.   ' calculate specific heat
    8.     If (Tinit < 600) Then
    9.         cp1 = (425 + (0.773 * Tinit) - (1.69 * 10 ^ -3 * (Tinit ^ 2)) + (2.22 * 10 ^ -6 * (Tinit ^ 3)))
    10.     ElseIf ((600 <= Tinit) And (Tinit < 735)) Then
    11.         cp1 = 666 + 13002 / (738 - Tinit)
    12.     ElseIf ((735 <= Tinit) And (Tinit < 900)) Then
    13.         cp1 = (545 + 17820 / (Tinit - 731))
    14.     ElseIf ((900 <= Tinit) And (Tinit <= 1200)) Then
    15.         cp1 = 650
    16.     End If
    17.        
    18. Test2 = cp1
    19.      
    20. End Function

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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

  6. #6
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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!

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    2

    Resolved 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
  •  



Click Here to Expand Forum to Full Width