Results 1 to 5 of 5

Thread: Excel VBA: UDF Not Working; Please Explain?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2014
    Posts
    74

    Question Excel VBA: UDF Not Working; Please Explain?

    I'm trying to learn how to perform User Defined Functions in Excel, and I thought this code (below) would be a winner on first pass; but it was not.

    Code:
    Public Function change2F() As Variant
    Dim result As Variant
    Dim sht As Worksheet
        change2F = (sht.Range("a:a") - 32) * 5 / 9
        change2F = result
            
    End Function
    Basically, I have in column A, a set of numbers from 0 to 100 (by 5's; representing Fahrenheit values) and I'm trying to get the Celsius equivalent to those numbers.
    Name:  Capture12.PNG
Views: 318
Size:  7.8 KB

    I have tried to toy with the code and keep getting "#VALUE!" errors

    Can someone explain why this code isn't working? I've tried several different data types and combinations to no avail: Integer, Variance, Long ... nothing seems to be working.

    What's going wrong with this code?
    Attached Images Attached Images  

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

    Re: Excel VBA: UDF Not Working; Please Explain?

    you finish by assigning an empty variant to your function
    your sht variable is never set to a worksheet object
    neither of which are required

    try like
    Code:
    Public Function change2F(r as range) As Variant
    
        change2F = (r - 32) * 9/5
    
            
    End Function
    though from your description the function should be change2C

    for this application, you do not really require a UDF
    =(a1 - 32) *9/5
    directly in the cell would work
    Last edited by westconn1; Nov 14th, 2014 at 03:40 PM.
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2014
    Posts
    74

    Re: Excel VBA: UDF Not Working; Please Explain?

    WestConn1,

    You are correct, I needed the change the desciption to "Change2C". I did try your code, but I received answers which were different from the In Cell references.

    Here's a screenshot:
    Name:  Capture12.PNG
Views: 250
Size:  9.1 KB

    Also, just so I'm learning correctly:
    your sht variable is never set to a worksheet object
    If I changed the line in my original code from:
    Code:
    Dim sht As Worksheet ...
    'changed to
    
    Dim sht as ActiveWorksheet ...
    would that be setting the variable to a worksheet object?
    Last edited by IGPOD; Nov 14th, 2014 at 04:18 PM. Reason: finish question

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Excel VBA: UDF Not Working; Please Explain?

    Code:
    change2C = (r - 32) * 9/5
    should be
    Code:
    change2C = (r - 32) * 5/9

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

    Re: Excel VBA: UDF Not Working; Please Explain?

    would that be setting the variable to a worksheet object?
    that is the declaration (or dimension of the variable), not assigning an object to the variable, which you were not doing

    Code:
    set sht = activesheet
    Last edited by westconn1; Nov 15th, 2014 at 01:40 AM.
    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

Tags for this Thread

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