Consulting

Results 1 to 19 of 19

Thread: Solved: NumberFormat not working in a UDF

  1. #1
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location

    Solved: NumberFormat not working in a UDF

    Hi, I lost my mind in this UDF. I can't change the numberformat of a cell calling the formula Frazione.

    [VBA]Function Frazione(Numer As Integer, Denom As Integer)
    Application.Volatile (True)
    Dim RgCaller As Range
    Set RgCaller = Application.Caller
    Frazione = (Numer / Denom)
    RgCaller.NumberFormat = "# ?/" & Denom
    End Function[/VBA]

    The fact is that it doesn't work with any NumberFormat.
    Can't you change a cell while it is calculating?
    ALe
    Help indigent families: www.bancomadreteresa.org

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I don't believe you can do that. You can however, format the cell as you would like, or include the format in your UDF and keep it as string.

  3. #3
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Quote Originally Posted by firefytr
    I don't believe you can do that. You can however, format the cell as you would like, or include the format in your UDF and keep it as string.
    What do you mean by "include the format in your UDF and keep it as string" ?

    My aim is to change the numberformat.
    ALe
    Help indigent families: www.bancomadreteresa.org

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I know your aim, you already stated that. I mean that you can keep it as a string, specifically placing each numerator, the seperator and the denomenator. Of course it would be easiest (IMO) to just manually format the cells as you would with any other.

  5. #5
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Thanks Zack.
    I'll have to find another solution because I need calculation on cells (strings don't allow it).
    ALe
    Help indigent families: www.bancomadreteresa.org

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Is there more to this formula? Seems like you could do this natively and it would work better for you. That's what I would recommend (if this is the extent of the issues).

  7. #7
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Alberto

    You can't use a UDF to alter a worksheet.

    All they do is return values.

    What is it you are actually trying to do?

    I assume it's something to do with fractions?

  8. #8
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Why not combine it with a Worksheet event?
    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If UCase(Left(Target.Formula, 5)) = "=FRAZ" Then
    Target.NumberFormat = "# ?/?"
    End If
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Quote Originally Posted by Norie
    You can't use a UDF to alter a worksheet.
    All they do is return values.
    Yes, but incomplete. A VBA function *called from a worksheet cell* cannot do anything except return a value. A function called directly from VBA can alter stuff in the Excel UI quite readily.

    That said, a while back someone (I want to say Matt Vidas) posted an example (here or at Experts Exchange) of a UDF called from a cell that did seem to do more than just return a value, but I cannot remember the details, and it was very obscure...
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  10. #10
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Patrick

    So what I said was incorrect?

  11. #11
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Quote Originally Posted by Norie
    Patrick

    So what I said was incorrect?
    I prefer 'incomplete'

    It's a matter of what the caller is. If the caller is a cell, then you're right: a function can only return a value. If the caller is from the VB project itself, then a function can alter any Excel object.
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  12. #12
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Patrick

    As far as I can see the OP was using the UDF in a cell, not from any other code.

    Personally, but of course I'm probably wrong/and or incomplete, but I wouldn't call a function called from other code a UDF.

    If I did why wouldn't I call a sub a UFS(ub) or a UDP(rocedure)?

  13. #13
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Here's an example of using a UDF to add/modify a chart in the cell.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  14. #14
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Thanks all. It's clear to me now that I can't change a cell with a UDF if this cell is the caller.

    As you understood I'm working with fractions. My aim is to display a fraction as it is inserted in a cell (for example 4/10) and not as it is diplayed by excel in its simple fraction 2/5. If I had had only decimal fraction (1/10, 2/10, 4/10,...) I could have used the numberformat "# ?/10" but the problem is that the base number is always different.

    To this point, mdmackillop's solution is good.
    ALe
    Help indigent families: www.bancomadreteresa.org

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    AM I missing something? Why not just apply that format to the cells and leave it at that?

  16. #16
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    hi xld,

    I'll try to explain it better.

    If I put the value 4/10 in a cell the result will be:
    value=0,2
    format="# ?/?"
    displayed=2/5

    I want it displayed as "4/10" so I change its formatnumber in "# ?/10"

    If I insert in the same cell the fraction "18/20" then I get:
    value=0.9
    format="# ?/10"
    displayed="9/10" BUT I WANT IT DISPLAYED AS I ENTERED IT!

    So I want a fraction displayed as I entered it.
    I can convert it as string but I'll lose calculation.

    Enough clear?
    ALe
    Help indigent families: www.bancomadreteresa.org

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Then Malcolm's suggestion looks best, as there is no way to stop Excel reducing a formula without forcing the format, so a formula of

    [vba]

    Function Frazione(Numer As Long, Denom As Long)
    Application.Volatile (True)
    Frazione = Numer / Denom
    End Function
    [/vba]

    and worksheet event code of

    [vba]

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const FORMULA_PART As String = "=FRAZIONE("
    Dim iPos As Long
    With Target
    If UCase(Left(.Formula, 10)) = FORMULA_PART Then
    iPos = InStr(.Formula, ",")
    If iPos > 0 Then
    .NumberFormat = "# " & Application.Rept("?", Len(Mid(.Formula, 11, iPos - 11))) & _
    "/" & Mid(.Formula, iPos + 1, Len(.Formula) - iPos - 1)

    End If
    End If
    End With
    End Sub
    [/vba]

  18. #18
    VBAX Mentor ALe's Avatar
    Joined
    Aug 2005
    Location
    Milan
    Posts
    383
    Location
    Yes, I agree worksheet event is the best solution we can reach.
    ALe
    Help indigent families: www.bancomadreteresa.org

  19. #19
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Quote Originally Posted by Norie
    Patrick

    As far as I can see the OP was using the UDF in a cell, not from any other code.

    Personally, but of course I'm probably wrong/and or incomplete, but I wouldn't call a function called from other code a UDF.

    If I did why wouldn't I call a sub a UFS(ub) or a UDP(rocedure)?
    Fair enough. I was getting too nitpicky
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

Posting Permissions

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