+ Reply to Thread
Results 1 to 9 of 9

Returned values from UDFs don't respect the format style of the cell

  1. #1
    Registered User
    Join Date
    11-08-2018
    Location
    London
    MS-Off Ver
    Microsoft for Mac 2011
    Posts
    14

    Returned values from UDFs don't respect the format style of the cell

    I have a cell who's format I've defined in a sub as a percentage and the values I type manually into the cell are automatically formatted to percentages. However, I also have a function that returns a value and when I run the function in that cell the value it returns doesn't update as a percentage even if I manually select "Percentage" as the format for that value.

    The function I'm using is designed to return a range of different data types (e.g. Currency's, Dates, Percentages, Values, Strings) so I'm using Variant as the return type for the function.

    Is there a way that I can ensure the function respects the format of the cell?

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Returned values from UDFs don't respect the format style of the cell

    Please attach an Excel sample and show your UDF's code
    - Battle without fear gives no glory - Just try

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

    Re: Returned values from UDFs don't respect the format style of the cell

    Functions don't return data types. They return numbers or text (or boolean). The cell then takes that number and applies the number format indicated by the user.

    What is the code for your UDF?
    Or ... Fluff's request for a sample workbook is even better, and it would include the code.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Returned values from UDFs don't respect the format style of the cell

    Is there a way that I can ensure the function respects the format of the cell?
    Make sure that the UDF is not returning a "number as text". For example, this
    Please Login or Register  to view this content.
    returns a number as text, and text does not respond to number formatting.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Returned values from UDFs don't respect the format style of the cell


    Or ... Fluff's request for a sample workbook is even better, and it would include the code.
    I haven't said anything.

  6. #6
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: Returned values from UDFs don't respect the format style of the cell

    So, what about the code and the file ...!

  7. #7
    Registered User
    Join Date
    11-08-2018
    Location
    London
    MS-Off Ver
    Microsoft for Mac 2011
    Posts
    14

    Re: Returned values from UDFs don't respect the format style of the cell

    Here is the code:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    11-08-2018
    Location
    London
    MS-Off Ver
    Microsoft for Mac 2011
    Posts
    14

    Re: Returned values from UDFs don't respect the format style of the cell

    mikerickson, thank you for your reply. What you are saying is what I expect to happen, I want the UDF to return the value and then for the cell to handle formatting separately (either by the user deciding the format, or having a sub preset the format). In practice this doesn't seem to be happening as the function is ignoring any custom formats.
    Last edited by gordonbe; 12-14-2018 at 08:18 AM.

  9. #9
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Returned values from UDFs don't respect the format style of the cell

    The first thing I notice in your UDF is that you are assigning the value "123.456" as the result. Note that this is a text string, which is often vastly different from the number 123.456 (no quotation marks). This is exactly what I mentioned in my post -- make sure your UDF is returning actual numbers when it should. change the assignment statement in your example to returnValue=123.456 Obviously your posted code is "dummy" code, so you will need to go through your real code to see how you might inadvertently be returning numbers as text strings that are not really numbers.

+ 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: 10
    Last Post: 12-14-2017, 09:58 AM
  2. false values returned when I copy formulae from one cell to another
    By brian.ward in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-08-2014, 06:28 AM
  3. Excel PivotChart, values don't respect filters?
    By sam452 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 08-26-2013, 03:13 PM
  4. Format cell value based on color(style) of another cell
    By regreading in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 02-21-2013, 08:10 AM
  5. VB to gather mulitiple values from a cell that is soft returned
    By skrepsky in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-24-2012, 12:09 PM
  6. Excel 2007 : Format cell to specific number style
    By BigGPL in forum Excel General
    Replies: 3
    Last Post: 01-29-2012, 11:01 AM
  7. Replies: 3
    Last Post: 04-18-2006, 11:30 AM

Tags for this Thread

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