Hi,
I am running a macro to put the sum function in selected sheets only and I have come across a problem.
In the attached file P12.xls I have four sites which I want to run the macro on (there are many other sheets in the original).
The macro runs through each sheet fine but as the sheets are of various lengths I hit upon the problem.
Site 1 inserts the formula down to row 50 fine
When I get to Site 4 it inserts the formula down to row 64 which is also fine, but then the formula is also inserted down to row 64 in all the sheets. I assume that this is because all the sheets are selected?

My code is:
Sub LiqMargins()
    Dim aCurSheet As Worksheet, sht As Worksheet
    
    Set aCurSheet = ActiveSheet
    For Each asheet In ActiveWindow.SelectedSheets
        asheet.Activate
        With asheet
        
        Range("r4").Select
            Do While ActiveCell.Offset(0, -16).Formula <> "total"
            ActiveCell.FormulaR1C1 = "=RC[-11]-sum(RC[-10]:RC[-1])"
            ActiveCell.Offset(1, 0).Select
            Loop
        ActiveCell.FormulaR1C1 = "=RC[-11]-sum(RC[-10]:RC[-1])"
        End With
        
    Next asheet
    aCurSheet.Activate
    ActiveSheet.Select


End Sub
How do I get the formula just on the rows that I need it and not on the same amount of rows on each sheet?