I need aa VBA code that will look for the last empty row then calculate the 10 rows…
- User
I need aa VBA code that will look for the last empty row then calculate the 10 rows above from column A to M
- Assistant
Here's a VBA code that will look for the last empty row and calculate the 10 rows above from column A to M:
Sub LastEmptyRow()
Dim lastRow As Long Dim startRow As Long Dim endRow As Long Dim totalSum As Double
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'find last row with data in column A startRow = lastRow - 10 'calculate row to start the sum endRow = lastRow - 1 'calculate row to end the sum
If startRow < 1 Then startRow = 1 'ensure start row is not less than 1
'loop through all rows from startRow to endRow and sum up the values in columns A to M For i = startRow To endRow For j = 1 To 13 totalSum = totalSum + Cells(i, j) Next j Next i
'place the total sum in the last empty row in column A Cells(lastRow, 1).Value = totalSum
End Sub
To use this code, simply open your Excel worksheet and press ALT+F11 to open the VBA editor. Insert a new module and copy and paste the code above into the module. Then, go back to your worksheet and run the macro using ALT+F8. The macro will automatically find the last empty row and calculate the sum of the 10 rows above it from columns A to M, and place the total sum in the last empty row in column A.
- User
What I mean by calculate is not a mathematical calculation but to force all formulas in the rows to calculate
- Assistant
Here's a VBA code that will force all formulas in the last 10 rows of column A to M to calculate:
Sub CalculateLastTenRows()
Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row 'find last row with data in column A startRow = lastRow - 9 'calculate row to start the calculation
If startRow < 1 Then startRow = 1 'ensure start row is not less than 1
'loop through all rows from startRow to lastRow and force a calculate on columns A to M For i = startRow To lastRow Range("A" & i & ":M" & i).Calculate Next i
End Sub
To use this code, simply open your Excel worksheet and press ALT+F11 to open the VBA editor. Insert a new module and copy and paste the code above into the module. Then, go back to your worksheet and run the macro using ALT+F8. The macro will automatically find the last 10 rows in column A to M and force all formulas to calculate.