Consulting

Results 1 to 5 of 5

Thread: average calculation

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

    average calculation

    hello
    the aim of the following function is to calculate the average of a specific
    number of columns containing only a required number format.
    why is it not working
    [VBA]
    Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, amt As Double, i As Integer
    For Each cell In Rng
    i = Rng.Columns.count
    If cell.numberformat = fmt Then
    If IsNumeric(cell.Value) Then
    SumByFormat10 = (SumByFormat10 + cell.Value) / i
    End If
    End If
    Next cell
    End Function
    [/VBA]
    moshe

  2. #2
    VBAX Mentor Marcster's Avatar
    Joined
    Jun 2005
    Posts
    434
    Location
    [VBA]Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, amt As Double, i As Integer
    For Each cell In Rng
    i = Rng.Columns.count
    If cell.numberformat = fmt Then
    If IsNumeric(cell.Value) Then
    SumByFormat10 = (SumByFormat10 + cell.Value) / i
    End If
    End If
    Next cell
    End Function[/VBA]

    Surrounded the code in VBA tags for easier reading.

  3. #3
    it worked for me.
    make sure you put the format of the range of cells as the same format that you put into the fmt parameter.

    btw, me'eifoh ata ba'aretz?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lior03
    hello
    the aim of the following function is to calculate the average of a specific
    number of columns containing only a required number format.
    why is it not working
    Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, amt As Double, i As Integer
    For Each cell In Rng
    i = Rng.Columns.count
    If cell.numberformat = fmt Then
    If IsNumeric(cell.Value) Then
    SumByFormat10 = (SumByFormat10 + cell.Value) / i
    End If
    End If
    Next cell
    End Function
    I think that you want

    [vba]
    Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, amt As Double, i As Integer

    For Each cell In Rng
    If cell.NumberFormat = fmt Then
    If IsNumeric(cell.Value) Then
    SumByFormat10 = (SumByFormat10 + cell.Value)
    End If
    End If
    Next cell
    SumByFormat10 = SumByFormat10 / Rng.Cells.Count
    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by xld
    I think that you want

    [vba]
    Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, amt As Double, i As Integer

    For Each cell In Rng
    If cell.NumberFormat = fmt Then
    If IsNumeric(cell.Value) Then
    SumByFormat10 = (SumByFormat10 + cell.Value)
    End If
    End If
    Next cell
    SumByFormat10 = SumByFormat10 / Rng.Cells.Count
    End Function
    [/vba]
    I think Rng.Cells.Count will give an erroneous result, try[vba]Function SumByFormat10(Rng As Range, Optional fmt As String = "0.00%")
    Dim cell As Range, N As Long
    N = 0
    For Each cell In Rng
    If cell.NumberFormat = fmt Then
    If IsNumeric(cell) Then
    SumByFormat10 = (SumByFormat10 + cell)
    N = N + 1
    End If
    End If
    Next cell
    SumByFormat10 = SumByFormat10 / N
    End Function[/vba]
    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.

Posting Permissions

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