Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21

Thread: Sleeper: Summing based on numberformat

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    Sleeper: Summing based on numberformat

    hello
    i have a table in which rows contains cell with different numberformats - #,##0.00 or 0.00% i built this vba function called server.
    i want the function to sum only cells with numberformat-#,##0.00
    whats wrong?

    Function server(r As Range) 
    Dim cell As Range
    Dim i As Integer
    For Each cell In Selection
    If cell.NumberFormat = "#,##0.00" Then
    i = cell.Value + 1
    Else: Exit Function
    i = server
    End If
    Next
    End Function
    thanks
    Last edited by johnske; 06-25-2005 at 02:59 PM. Reason: to add VBA tags
    moshe

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

    What do you mean 'what's wrong'?

    Is the function returning an incorrect answer or not working at all.

    By the way it looks to me as though you are counting the no of times that number format is being used rather than summing.

  3. #3
    I really can't answer your Q, but I do suggest that you remove the colon following the Else .

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

    That colon is acceptable syntax. Though I do agree it should be removed, if only to clarify the flow of the code.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    hello
    i have a table in which rows contains cell with different numberformats - #,##0.00 or 0.00% i built this vba function called server.
    i want the function to sum only cells with numberformat-#,##0.00
    whats wrong?
    Function server(r As Range)
    Dim cell As Range
    Dim i As Integer
    For Each cell In Selection
    If cell.NumberFormat = "#,##0.00" Then
    i = cell.Value + 1
    Else: Exit Function
    i = server
    End If
    Next
    End Function
    thanks
    A couple of things.

    First you are counting not summing, and secondly you have an argument of r as a Range, but then ignore it and process the selected cells.

    How do you call it, and what is wrong for you?

  6. #6
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Moshe,

    Try this

    Option Explicit
    
    Sub Test()
          'first format some cells in #,##0.00 format
          'then make a selection & run this sub
          server Selection
    End Sub
    
    Function server(r As Range)
          'I've added message boxes so you
          'can see what's happening here
          Dim cell As Range
          Dim i As Integer
          For Each cell In Selection
                If cell.NumberFormat = "#,##0.00" Then
                      i = cell.Value + 1
                      MsgBox i
                Else
                      i = server  '< maybe you mean this?
                      MsgBox i
                      Exit Function
                      'i = server  '< does nothing here
                End If
          Next
    End Function
    HTH,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm not sure if this is quite what you're after, but the following as a UDF (user defined formula) should return the total as you have specified. It's entered as =server(A1:A10)


    Function server(r As Range)
        Dim cell As Range
        Dim i As Double
        For Each cell In r
            If cell.NumberFormat = "#,##0.00" Then
                i = i + cell
            End If
        Next
        server = i
    End Function
    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'

  8. #8
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by mdmackillop
    I'm not sure if this is quite what you're after, but the following as a UDF (user defined formula) should return the total as you have specified. It's entered as =server(A1:A10)


    Function server(r As Range)
        Dim cell As Range
        Dim i As Double
        For Each cell In r
            If cell.NumberFormat = "#,##0.00" Then
                i = i + cell
            End If
        Next
        server = i
    End Function
    I believe that the main problems all along were:
    1. the incorrect summing statement (originally i = cell.value + 1)
    2. no assignment of a summed value to the function name
    both of which you corrected in your revised UDF.
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  9. #9
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    a question

    hello
    is it possible to add to the following function
    a an optional argument which will enable the user to select the numberformat he desire?

    Function server(r As Range,optional fmt as string) 
    Dim cell As Range
    Dim i As Double
    For Each cell In r
    If cell.NumberFormat = fmt Then
    i = i + cell
    End If
    Next
    server = i
    server = Format(server, "##,###.00")
    End Function
    thanks
    moshe

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi,
    You can do that

    Function server(r As Range, Optional fmt As String)
        Dim cell As Range
        Dim i As Double
        For Each cell In r
            If cell.NumberFormat = fmt Then
                i = i + cell
            End If
        Next
        server = Format(i, fmt)
    End Function
    As an alternative, you could exclude only items of a certain format

    Function xpcsum(r As Range, Optional fmt As String)
        Dim cell As Range
        Dim i As Double
        For Each cell In r
            If Not cell.NumberFormat = fmt Then
                i = i + cell
            End If
        Next
        xpcsum = i
    End Function
    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'

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    hello
    is it possible to add to the following function
    a an optional argument which will enable the user to select the numberformat he desire?
    You seem to have done that already for the range being checked. so I assume that you mean the output format.

    Also, your summing statement

    i = i + cell

    is incorrect, it should be

    i = i + 1

    How about this?


    Function server(r As Range, Optional inFormat As String = "#,##0.00", _
                    Optional outFormat As String = "#,##0")
        Dim cell As Range
        Dim i As Double
        For Each cell In r
            If cell.NumberFormat = inFormat Then
                i = i + 1
            End If
        Next
        server = Format(i, outFormat)
    End Function

  12. #12
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    a question

    Function server1(r As Range, Optional fmt As String) 
    Dim cell As Range, i As Double
    If IsMissing(fmt) Then
    fmt = "#,##0.00"
    For Each cell In r
    If cell.NumberFormat = fmt Then
    i = i + cell
    server1 = Format(i, fmt)
    End If
    Next
    End If
    End Function
    what's wrong with
    thanks
    moshe

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try
    [VBA] If fmt = "" Then
    fmt = "#,##0.00"[/VBA]
    This code will only work where an actual font is set, not on General font which may "look like" the fmt setting.
    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'

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    Function server1(r As Range, Optional fmt As String)
    Dim cell As Range, i As Double
    If IsMissing(fmt) Then
    fmt = "#,##0.00"
    For Each cell In r
    If cell.NumberFormat = fmt Then
    i = i + cell
    server1 = Format(i, fmt)
    End If
    Next
    End If
    End Function
    what's wrong with
    thanks
    Moshe,

    Where are you going with this? There have been numerous attempts to help you, but we are getting no feedback as to whether we are succeeding, getting close, or are miles off, and then you come up with this effort which is similar, but oddly different.

    Help us to help you, please

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Moshe,
    I agree with XLD, we're not getting anywhere fast. Can you post a sample of your spreadsheet with the cells you're trying to "sum", so we can see if there is an underlying problem. To post a workbook, zip it and use Manage Attachments which you can access if you use the Go Advanced button
    Regards
    MD
    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'

  16. #16
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    i'm sorry

    hello
    i did not mean to upset anyone.
    attached is a workbook with the problem.
    a database of economic data year by year .each column of data is ccompanied by a column of growth ratio to the year befor formated in "0.00%".
    i am trying to build a UDF to deal with average growth rate ,as well as summig the data formated in "##,###.00"
    for instance - in sheet gdpf how can i get the average of data formated in "0.00%".
    thanks
    moshe

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

    There is no attachment.

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Moshe,
    I don't believe you have upset anyone, so no apologies needed;
    but your workbook is not attached.
    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'

  19. #19
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    i did not mean to upset anyone.
    attached is a workbook with the problem.
    a database of economic data year by year .each column of data is ccompanied by a column of growth ratio to the year befor formated in "0.00%".
    i am trying to build a UDF to deal with average growth rate ,as well as summig the data formated in "##,###.00"
    for instance - in sheet gdpf how can i get the average of data formated in "0.00%".
    thanks
    moshe

  20. #20
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Question continued in new post at http://www.vbaexpress.com/forum/showthread.php?t=3793
    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'

Posting Permissions

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