I have written a code to do the specific task:
- Go through each sheet and get the MAX of the last 365 rows in Column E.
The range is dynamic because each sheet have different number of rows. So I decided to use this codes:
Sheets(Cells(i, 1).Value).Range("E1048576").End(xlUp).Offset(-365, 0).Address Sheets(Cells(i, 1).Value).Range("E1048576").End(xlUp).Address
so it will return the (a) Last row minus 365 rows (b) Last Row resulting to Range(a to b)
I added the
WorksheetFunction.Max
function to get the max number.My code is inside a for loop which gets the sheet names in Column A.
Private Sub tester() Dim i As Integer LastRow = Sheets("Stocks Summary").Cells(Rows.Count, 1).End(xlUp).Row For i = 2 To LastRow Cells(i, 13).Value = WorksheetFunction.Max(Range(Sheets(Cells(i, 1).Value). _ Range("E1048576").End(xlUp).Offset(-365, 0).Address & ":" & _ Sheets(Cells(i, 1).Value).Range("E1048576").End(xlUp).Address)) Next i End Sub
My error:
Everytime I run the macro, it returns 0. I don't understand why and what went wrong.
If somebody could help me with the code, I will truly appreciate it.