Last Row Average VBA

NocoffeeNoWorkee

New Member
Joined
Apr 28, 2016
Messages
12
How can I put an average of the last row in the first empty column of the same row?

Here is what I tried:
[TABLE="class: outer_border, width: 800"]
<tbody>[TR]
[TD]Sub LastRowAve()
lastrow = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lColumn = ThisWorkbook.Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column


ThisWorkbook.Sheets("sheet1").Range(lColumn + 1 & lastrow) = Application.WorksheetFunction.Average(ThisWorkbook.Sheets("sheet1").Range("A" & lastrow:lColumn&lastrow))


End Sub


[/TD]
[/TR]
</tbody>[/TABLE]

Theres a problem with the list seperator in Range.
 
Not sure if this will work but maybe try this in the last line of code:

ThisWorkbook.Sheets("sheet1").Range(lColumn + 1 & lastrow) = Application.WorksheetFunction.Average(ThisWorkbook.Sheets("sheet1").Range("A" & lastrow & ":" & lColumn & lastrow))

Best,

Tyler Hilton
 
Upvote 0
Hi,

Does this do what you want...

Code:
Sub LastRowAve()
    Dim avg As Single
    Dim lastrow As Long, lColumn As Long


    lastrow = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    lColumn = ThisWorkbook.Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    
    avg = Application.WorksheetFunction.Average(Range(Cells(lastrow, 1), Cells(lastrow, lColumn)))
    ActiveSheet.Cells(lastrow, lColumn + 1).Value = avg


End Sub

HTH

igold
 
Upvote 0
Hi,

Does this do what you want...

Code:
Sub LastRowAve()
    Dim avg As Single
    Dim lastrow As Long, lColumn As Long


    lastrow = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    lColumn = ThisWorkbook.Sheets("sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
    
    avg = Application.WorksheetFunction.Average([B][COLOR="#FF0000"]Range(Cells(lastrow, 1), Cells(lastrow, lColumn))[/COLOR][/B])
    ActiveSheet.Cells(lastrow, lColumn + 1).Value = avg


End Sub
Since this is VB code and not worksheet formulas, you don't have to worry about Circular Referencing, so you can replace the red highlighted part of your code with this... Rows(lastrow)
 
Upvote 0
@Rick,

Thanks for the tip. That makes perfect sense...

igold
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top