+ Reply to Thread
Results 1 to 6 of 6

How to find last 10 cells in a row and paste the average into new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2007
    Posts
    7

    How to find last 10 cells in a row and paste the average into new sheet

    Hi guys

    Trying to sort some data on seperate sheets. I have a table of data on each sheet and need to find the average value from the last 10 cells in column G on a sheet called "0.3lpm" and paste into a sheet called "comp" (which could be the active sheet).

    Thanks!!

  2. #2
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    banana97,

    This would be one way of doing it. Probably the hard way, but at least it will get you started.

    Sub aaa()
    
    Dim lLastRow As Long, lrow As Long, lSum As Long
    Dim wsComp As Worksheet, wsLPM As Worksheet
    
    Set wsComp = Sheets("Comp")
    Set wsLPM = Sheets("0.3lpm")
    
    lLastRow = wsLPM.Cells(Rows.Count, "G").End(xlUp).Row
    lSum = 0
    
    For lrow = lLastRow To lLastRow - 9 Step -1
        
        lSum = lSum + wsLPM.Cells(lrow, "G").Value
        
    Next lrow
    
    wsComp.Cells(1, 1).Value = lSum / 10
    
    End Sub
    Sincerely,
    Jeff

  3. #3
    Registered User
    Join Date
    07-26-2007
    Posts
    7
    I couldn't get that to work.

    If i just record a macro I get this:

    
    Sub try()
        ActiveCell.FormulaR1C1 = "=SUM('0.3lpm'!R[645]C[6]:R[654]C[6])/10"
        Range("A5").Select
    End Sub
    So basically while in the active sheet (which i've called comp) I'm going to another sheet called 0.3lpm and selecting the last 10 rows of column G then doing the average.

    How can I modify the macro to automatically pick the last 10 rows in column G on sheet 0.3lpm?

    Thanks for the help!

  4. #4
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    banana97,

    Try this. It is setting up a formula to be placed in cell A1 on sheet Comp. It creates a formula that sums the last 10 cells of Column G and divides by it by 10.

    Sub aaa()
    
    Dim lLastRow As Long
    Dim wsComp As Worksheet, wsLPM As Worksheet
    
    'This assigns the worksheets to a variable to make coding easier
    Set wsComp = Sheets("Comp")
    Set wsLPM = Sheets("0.3lpm")
    
    'This finds the last row in Column G on sheet 0.3lpm that has a value in it.
    lLastRow = wsLPM.Cells(Rows.Count, "G").End(xlUp).Row
    
    'Creates the formula
    wsComp.Range("A1").Formula = "=sum('0.3lpm'!G" & CStr(lLastRow - 9) & ":G" & CStr(lLastRow) & ")/10"
    
    End Sub
    If you want this to calculate everytime a change is made to 0.3lpm you will need to put something like this Sheet 0.3lpm object in the VB editor.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Call aaa
    End Sub
    Hope this works for you

  5. #5
    Registered User
    Join Date
    07-26-2007
    Posts
    7
    Worked like a dream!

    Thank you very much!

  6. #6
    Forum Contributor boylejob's Avatar
    Join Date
    02-22-2007
    Location
    Forest City, NC
    MS-Off Ver
    2003
    Posts
    562
    Banana97,

    Great! I'm glad it worked. Creating a formula just took a little more thought on my end so answering your question ended up being a benefit to me.

    Glad I was able to help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1