Consulting

Results 1 to 13 of 13

Thread: Function average using VBA

  1. #1
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location

    Function average using VBA

    Hello

    I would like to calculate automatically and using VBA the average
    of my results. Please have a look at my file to see what I really
    would like to create.


    Thanks a lot!

    Fran?ois

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Here is a macro to do it.


    Sub CalcAverage()
    Dim sDays As String
    Dim sDilutions As String
    Dim sResults As String
    Dim iLastRow As Long
    Dim i As Long
    Dim sFormula As String
    iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    sDays = "B2:B" & iLastRow
    sDilutions = "C2:C" & iLastRow
    sResults = "E2:E" & iLastRow
    For i = 2 To iLastRow
    sFormula = "AVERAGE(IF((" & sDays & "=$B" & i & ")*(" & _
    sDilutions & "=$C" & i & ")," & sResults & "))"
    Cells(i, "G").Value = Evaluate(sFormula)
    Next i
    End Sub
    ____________________________________________
    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

  3. #3
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location
    Ok, thanks a lot for your help!! The macro works without problem

    But what can I do to create the macro for the other column (E)"?


    Fran?ois

  4. #4
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location
    column F by the way

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by frade
    But what can I do to create the macro for the other column (E)"?
    Sorry Fran?ois,

    Didn't realise you wanted both.

    Here is an amended version.


    Sub CalcAverage()
    Dim sDays As String
    Dim sDilutions As String
    Dim sResults As String
    Dim iLastRow As Long
    Dim i As Long
    Dim sFormula As String
    iLastRow = Cells(Rows.Count, "A").End(xlUp).Row
    sDays = "B2:B" & iLastRow
    sDilutions = "C2:C" & iLastRow
    sResults = "E2:E" & iLastRow
    For i = 2 To iLastRow
    sFormula = "AVERAGE(IF((" & sDays & "=$B" & i & ")," & sResults & "))"
    Cells(i, "F").Value = Evaluate(sFormula)
    sFormula = "AVERAGE(IF((" & sDays & "=$B" & i & ")*(" & _
    sDilutions & "=$C" & i & ")," & sResults & "))"
    Cells(i, "G").Value = Evaluate(sFormula)
    Next i
    End Sub
    ____________________________________________
    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

  6. #6
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location
    Ok, Thanks a lot. It works without problem.

    But what can I do to replace Average by SUM/value of a number in a cell
    or calculate SQRT of SUM/Value of a number in a cell.

    Something like this
    sFormula = "SQRT(SUM/number)(IF((" & sDays & "=$B" & i & ")*(" & _ 
            sDilutions & "=$C" & i & ")," & sResults & "))"
    I would like to calcule the square root of the sum divide by a number..


    Best regards,

    Fran?ois

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by frade
    But what can I do to replace Average by SUM/value of a number in a cell or calculate SQRT of SUM/Value of a number in a cell.

    I would like to calcule the square root of the sum divide by a number..

    Fran?ois,

    All I have done is to plug a worksheet formula into VBA, so just adjust that VBA as you would a worksheet formula.

    I also created a formula string, which helps, so change

    sFormula = "AVERAGE(IF((" & sDays & "=$B" & i & ")," & sResults & "))"
    to say

    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))"
    Just ensure that you maintain correct syntax.
    ____________________________________________
    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

  8. #8
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location
    Ok, but I would like to obtain the SUM divided by a value


    Something like this?

    sFormula = "SUM/value(IF((" & sDays & "=$B" & i & ")," & sResults & "))"


    in place of just SUM

    Thanks

  9. #9
    you can just write the formula as you would in excel only everything that is not a VBA variable just be between " ". You combine 2 parts with the & symbol (see the example you gave). Tricky part is when you are using an Excel that is in a different language then english. VBA only handles english formulas (so stick to these) and uses commas instead of ; . I am using a dutch Excel and took me a while to figure it all out.

    HTH

    Frank

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Regouin
    Tricky part is when you are using an Excel that is in a different language then english. VBA only handles english formulas (so stick to these) and uses commas instead of ; . I am using a dutch Excel and took me a while to figure it all out.
    Frank,

    I believe that if you put the local language formula ina worksheet cell, and then use
    ?Activecell.Formula (or maybe (?Activecell.FormulaLocal)
    in the immediate window in the VB IDE, then you will see the English formula.

    Can't test it myself, I have other language versions of Excel, but never installed them.
    ____________________________________________
    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

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by frade
    Ok, but I would like to obtain the SUM divided by a value


    Something like this?



    sFormula = "SUM/value(IF((" & sDays & "=$B" & i & ")," & sResults & "))"
    in place of just SUM
    No, but it depends upon where the value is. If it is a VBA variable, then use
    something like


    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))" 
    Cells(i, "F").Value = Evaluate(sFormula)/myVal

    If you want to divide by another cell, then something like


    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))/$H$10" 
    Cells(i, "F").Value = Evaluate(sFormula
    ____________________________________________
    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

  12. #12
    VBAX Regular
    Joined
    May 2005
    Posts
    75
    Location
    Thanks a lot xld!

    If my value is located on the page "sheet1", what can I do?

    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))/Sheet1.$H$10" 
    Cells(i, "F").Value = Evaluate(sFormula )
    or something like that?

    Fran?ois

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by frade
    If my value is located on the page "sheet1", what can I do?
    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))/Sheet1.$H$10" Cells(i, "F").Value = Evaluate(sFormula )


    Almost.


    sFormula = "SUM(IF((" & sDays & "=$B" & i & ")," & sResults & "))/Sheet1!$H$10"
    ____________________________________________
    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

Posting Permissions

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