+ Reply to Thread
Results 1 to 14 of 14

Thread: Using loops to find averages

  1. #1
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Using loops to find averages

    I'm a beginner when it comes to macros to bear with me if this is really simple.

    I have a bunch of data taken from a force measurement machine and i need to write a macro that organizes it and makes some average calculations. The machine has to take force measurements from several spots on each unit, then when it is done it goes to the next unit. Sometimes we take measurements of 5 units, sometimes 10, sometimes 20 etc. and all the data is exported in one excel file. I need to find the average force measurement of all the units at each specific spot. So the average force on spot a, b, c etc.

    I attached an example workbook to help explain what I mean better, and to help you understand how the data is spit out by the machine.

    I need this to work no matter how many units i will be testing, so i need either a way to tell excel how many units there are or it just somehow knows.

    Thanks!
    Attached Files Attached Files

  2. #2
    Forum Guru mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2007/2010
    Posts
    3,004

    Re: Using loops to find averages

    Paste

    =AVERAGEIF(A:A,D2,C:C)

    into E2 and copy down.
    Martin

    Eighty Twenty Spreadsheet Automation http://homepage.ntlworld.com/martin.rice1/ for all your Excel customisation and consulting needs.

    If my solution has saved you time and/or money, please consider donating to Cancer Research UK.

  3. #3
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    Hi Mdiver, click the button Calculate Averages to see if this meets your requirements.
    Attached Files Attached Files
    Please leave a message after the beep!

  4. #4
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    Quote Originally Posted by Mordred View Post
    Hi Mdiver, click the button Calculate Averages to see if this meets your requirements.
    Yes, that is what I had in mind. That was a simplified version of what I will end up having to do. I will have 54 spots and I will not only be finding the average of force, but the average of several other factors. How can i change your code to fit my needs? You don't have to write out the code yourself, but maybe a brief explanation in words can help me figure it out?

    If you could break down what this section of the code means I think it would greatly help my understanding:

    Case "a"
    Force(1) = Force(1) + cl.Offset(0, 2).Value
    a1 = a1 + 1
    Range("E2").Value = Force(1) / a1


    Also, will this work no matter the amount of units I use? Say one time I test 10 units, and a week later I need to test 20. Will the same macro work or will I have to change the code each time?


    Thanks A TON for your help.

  5. #5
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    Hi Mdiver, I am not sure without specifics how you could use the code I provided to fit your needs but I'll try to explain what's going on. for
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''If the cl value = Select Case "a" or "a" from your list in Column A'''
    '''then go into Case "a"'''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Case "a"
         '''Force() is an array that is used to add all the case values
         '''Force(1) is the first array count and is designated to case "a", Force(2) is 
         '''designated to case "b" and so on.
         Force(1) = Force(1) + cl.Offset(0, 2).Value
         '''a1 is the counter for case "a" so that we know what the count
         '''is regarding the amount of times Case "a" is selected.
         a1 = a1 + 1
          '''Set Range E2 value to equal the average'''
          Range("E2").Value = Force(1) / a1
    Please leave a message after the beep!

  6. #6
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    What does the For Next loop do that you put in the macro? Does this allow me to put any units I want or just 1-10?

    For x = 1 To 10
    Force(x) = 0
    Next x

  7. #7
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    Quote Originally Posted by Mdiver View Post
    What does the For Next loop do that you put in the macro? Does this allow me to put any units I want or just 1-10?

    For x = 1 To 10
    Force(x) = 0
    Next x
    That represents the count of letters ie: a to j, that you have on the sheet. Force(x [where x = 1]) represents "a" while Force(x [where x = 10]) represents "j"
    Please leave a message after the beep!

  8. #8
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    If you elaborate more from your post #4 I can help you figure it all out (hopefully)
    Please leave a message after the beep!

  9. #9
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    I commented this out some more for you, let me know if something isn't clear enough for you.
    Sub Mdiver()
        Dim SpotRng As Range, Force(10) As Double, AvgForce As Double, rCnt As Long
        Dim a1 As Long, b1 As Long, c1 As Long, d1 As Long, e1 As Long, f1 As Long, g1 As Long
        Dim h1 As Long, i1 As Long, j1 As Long
        
        '''setting a1 to j1 to 0 values so the are countable (Excel does this by default but
        '''I like to make sure'''
        a1 = 0: b1 = 0: c1 = 0: d1 = 0: e1 = 0: f1 = 0: g1 = 0: h1 = 0: i1 = 0: j1 = 0
        
        '''Setting the Force Array to 0's to be addable'''
        For x = 1 To 10
            Force(x) = 0
        Next x
        '''Find the last row count'''
        rCnt = Range("A1000").End(xlUp).Row
        
        '''Set SpotRng is necessary for the loop to know what range of cells
        '''to loop through'''
        Set SpotRng = Sheets(1).Range("A1", Range("A1000").End(xlUp))
        
        '''Loop through each Cell in SpotRng'''
        For Each cl In SpotRng
            Select Case cl
            
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                '''If the cl value = Select Case "a" or "a" from your list in Column A'''
                '''then go into Case "a"'''''''''''''''''''''''''''''''''''''''''''''''''
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                Case "a"
                    '''Force() is an array that is used to add all the case values
                    Force(1) = Force(1) + cl.Offset(0, 2).Value
                    '''a1 is the counter for case "a" so that we know what the count
                    '''is regarding the amount of times Case "a" is selected.
                    a1 = a1 + 1
                    '''Set Range E2 value to equal the average'''
                    Range("E2").Value = Force(1) / a1
                Case "b"
                    '''Same reasoning as Case "a"'''
                    Force(2) = Force(2) + cl.Offset(0, 2).Value
                    b1 = b1 + 1
                    Range("E3").Value = Force(2) / b1
                Case "c"
                    '''Same reasoning as Case "a"'''
                    Force(3) = Force(3) + cl.Offset(0, 2).Value
                    c1 = c1 + 1
                    Range("E4").Value = Force(3) / c1
                Case "d"
                    '''Same reasoning as Case "a"'''
                    Force(4) = Force(4) + cl.Offset(0, 2).Value
                    d1 = d1 + 1
                    Range("E5").Value = Force(4) / d1
                Case "e"
                    '''Same reasoning as Case "a"'''
                    Force(5) = Force(5) + cl.Offset(0, 2).Value
                    e1 = e1 + 1
                    Range("E6").Value = Force(5) / e1
                Case "f"
                    '''Same reasoning as Case "a"'''
                    Force(6) = Force(6) + cl.Offset(0, 2).Value
                    f1 = f1 + 1
                    Range("E7").Value = Force(6) / f1
                Case "g"
                    '''Same reasoning as Case "a"'''
                    Force(7) = Force(7) + cl.Offset(0, 2).Value
                    g1 = g1 + 1
                    Range("E8").Value = Force(7) / g1
                Case "h"
                    '''Same reasoning as Case "a"'''
                    Force(8) = Force(8) + cl.Offset(0, 2).Value
                    h1 = h1 + 1
                    Range("E9").Value = Force(8) / h1
                Case "i"
                    '''Same reasoning as Case "a"'''
                    Force(9) = Force(9) + cl.Offset(0, 2).Value
                    i1 = i1 + 1
                    Range("E10").Value = Force(9) / i1
                Case "j"
                    '''Same reasoning as Case "a"'''
                    Force(10) = Force(10) + cl.Offset(0, 2).Value
                    j1 = j1 + 1
                    Range("E11").Value = Force(10) / j1
            End Select
        Next cl
    End Sub
    Also, I'm sure that a VBA guru could make this code more efficient but it does work on any number of entries.
    Please leave a message after the beep!

  10. #10
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    Quote Originally Posted by Mordred View Post
    If you elaborate more from your post #4 I can help you figure it all out (hopefully)
    Ok, you asked for it! What I eventually need to produce is as follows:

    The data comes out of the machine with tons of junk in it (the attached excel file 'dataexample' is how it looks fresh out of the machine), then I would ideally be able to copy and paste all that data into the Raw Data tab in the second attached excel file 'workingexample', press one button that runs a macro that produces a final copy that looks like the third attached excel file 'finalexample'. Maybe two buttons would be required, one to sort the data and one to Transfer it from the raw data tab to the Summary tab.

    To help you understand where some of the numbers are coming for, I put up a 4th excel file that shows which column in the Raw Data tab of the 'finalexample' file matches up with which column in the 'dataexample' file.

    Obviously this is easier said than done, but at the same time i think a lot of it is deleting garbage columns and making things neat with a few algorithms that find max, min and avg. I would think that the hardest part would be what you did in the mock workbook earlier, but I could be wrong as I am pretty illiterate when it comes to excel.

    ALSO, the most important part of this file would be it's ability to work no matter how many units are tested, whether it be 5, 10, 20, etc.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    And just to clarify, the FinalExample only has one unit tested, that is why the maximum and minimum are the same and why it is always Unit 1

  12. #12
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Using loops to find averages

    Wowzers I am having a hard time understanding this now . I am going to try and walk through this with you but it may not happen in one day as it looks like there are a few steps that need to be done here and I don't know if I have the coding abilities to do everything you want but I will try (and get help if I need it).
    Please leave a message after the beep!

  13. #13
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    Quote Originally Posted by Mordred View Post
    Wowzers I am having a hard time understanding this now . I am going to try and walk through this with you but it may not happen in one day as it looks like there are a few steps that need to be done here and I don't know if I have the coding abilities to do everything you want but I will try (and get help if I need it).
    I completely understand, I don't know why I was assigned this; I have no experience with VBA code and the only macro experience I have is simple recordings.

    Thanks a ton for any help you can give me and let me know if you need something clarified.

  14. #14
    Registered User
    Join Date
    06-28-2011
    Location
    United States
    MS-Off Ver
    Excel 2007
    Posts
    7

    Re: Using loops to find averages

    (bump bump)

+ 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