+ Reply to Thread
Results 1 to 4 of 4

User defined function not working properly

  1. #1
    Registered User
    Join Date
    01-10-2017
    Location
    Sydney
    MS-Off Ver
    2013
    Posts
    2

    User defined function not working properly

    Nice to meet all the fellows.

    I am new to excel Macro VBA, and I just tried to write my first VBA code for the user defined function, however, its not 100% working as the way I would like it to.

    I am trying to create a formula to copy the value of next right cell into current activecell, and if the next right cell is blank, copy the value from the one above of the current activecell. (like the screenshot below, the yellow part is the result I want)

    Name: Capture.JPG Views: 0 Size: 28.2 KB

    Below is my vba code for the function

    Function CopyfromNext(x As String) As String
    If x <> "" Then
    CopyfromNext = ActiveCell(1, 2).Value
    Else
    CopyfromNext = ActiveCell(0, 1).Value
    End If
    End Function

    It is working fine if I run the code from the vba editor ( running the function each time separately for each cell).
    However, if I run the function from the excel function list (just like the excel build in functions) and I drag and copy the formula down to the column ( like the way people normally run for the vlookup), it is not give me correct value (my excel in auto calculation).

    Could anyone help me to find why that, I really can’t figure out.

    Thanks
    Attached Images Attached Images

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: User defined function not working properly

    No need for a UDF


    =LOOKUP("zzzzz",E$1:E2)
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    01-10-2017
    Location
    Sydney
    MS-Off Ver
    2013
    Posts
    2

    Re: User defined function not working properly

    Thanks Mikerrickson for your reply.

    however, I am still not sure why my UDF not working properly.

    Do you think you could kindly look my code, is there anything not correct?

    Thanks

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,526

    Re: User defined function not working properly

    This forum can be strict about rules like putting code in code tags. Edit your OP to include the code tags, lest someone come along and lock your topic.

    As a thought to prod your thinking, what does "ActiveCell" mean when a function is called from a spreadsheet cell? Note in particular that ActiveCell does NOT mean "the cell calling the UDF" or anything like that. For a UDF intended to be called from a spreadsheet cell, I would not ever expect to use ActiveCell.

    As an introductory practice into debugging tools and strategies, Put a Stop statement at the top of your UDF, enter the function into a cell, then add a watch for "ActiveCell.Address" or execute debug.print activecell.address in the immediate window or something to help you see what is the ActiveCell.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 12-03-2015, 04:12 AM
  2. [SOLVED] Max Function Not Working Properly
    By STU22 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-25-2015, 07:41 PM
  3. User Defined Function Not Working Without Quotations
    By PrizeGotti in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-03-2013, 11:38 AM
  4. User defined function not updating properly - where to put application.volatile?
    By gehawk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-08-2013, 06:47 AM
  5. How to ensure a self-defined Function is working properly
    By kwfine in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-29-2011, 01:00 AM
  6. user defined fuction not recognized / not working
    By ztodd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2009, 03:09 PM
  7. vlookup path not working in UDF (User Defined Function)
    By miagovino in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-15-2007, 01:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1