+ Reply to Thread
Results 1 to 5 of 5

Thread: Custom average function

  1. #1
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Custom average function

    can this be done with Worksheet functions:
    Function MyAvg(Target As Range) As String
    
    Dim Ccell As Range, NumVals As Long, MySum As Double
    
      'From string to values  
      For Each Ccell In Target
          Select Case Ccell.Value
            Case ""
              'Do nothing
            Case "O"
              MySum = MySum + 1: NumVals = NumVals + 1
            Case "M"
              MySum = MySum + 2: NumVals = NumVals + 1
            Case "V"
              MySum = MySum + 3: NumVals = NumVals + 1
            Case "RV"
              MySum = MySum + 4: NumVals = NumVals + 1
            Case "G"
              MySum = MySum + 5: NumVals = NumVals + 1
            Case Else
              MySum = 0
              Exit Function
          End Select
        Next
        
        MySum = MySum / NumVals
    
      'From values back to Strings
      Select Case MySum
        Case 1 To 1.5
          MyAvg = "O "
        Case 1.5 To 2.5
          MyAvg = "M "
        Case 2.5 To 3.5
          MyAvg = "V "
        Case 3.5 To 4.5
          MyAvg = "RV "
        Case 4.5 To 5
          MyAvg = "G "
      End Select
      
    End Function
    It works like a normal AVERAGE only the 1,2,3,4,5 are now replaced by O,M,V,RV,G
    Last edited by rwgrietveld; 12-11-2009 at 08:08 AM.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  2. #2
    Forum Guru sweep's Avatar
    Join Date
    04-03-2007
    Location
    Location: Location:
    MS-Off Ver
    3.0
    Posts
    2,472

    Re: Custom average function

    Didn't work when tested - sorry
    Last edited by sweep; 12-11-2009 at 08:10 AM. Reason: Didn't work when tested

  3. #3
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Custom average function

    Spot on Sweep. Thx
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  4. #4
    Forum Guru sweep's Avatar
    Join Date
    04-03-2007
    Location
    Location: Location:
    MS-Off Ver
    3.0
    Posts
    2,472

    Re: Custom average function

    Did that work for you? I just tried it out and I'm not getting what I expected.

  5. #5
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Custom average function

    Though I suspect you need to add a ROUND to the Average output if you wish to replicate your UDF Ricardo ?
    that being said your Select Case arguments overlap so it's not quite clear if your boundaries are round related or not

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0