• Update formula for inserted row (97, SR-2)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Update formula for inserted row (97, SR-2)

    Author
    Topic
    #374905

    I’m using this formula in Column E of a worksheet to keep a running total which is increased by an entry in C and decreased by an entry in D: =IF(AND(C2=””,D2=””),””,E1+C2-D2). I’ve filled the formula down in Column E far enough to give me plenty of space for entries in any given accounting period. Occasionally I need to insert a row somewhere among the completed entries. How can I alter this formula to get Excel to insert it in Column E of the new row and update all the following values in Column E?
    Clark

    Viewing 0 reply threads
    Author
    Replies
    • #608018

      Hi Clark,

      If you just want to be able to copy your formula into the new row, without having to update the row below, you could use:
      =IF(AND(C2=””,D2=””),””,OFFSET(E2,-1,0)+C2-D2)

      However, if you want Excel to automatically insert the formula into the new row as well, you’ll need an event-driven macro that fires every time you insert a new row into a defined range.

      Cheers

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

      • #609261

        Thanks very much. Would you or someone else be willing to write such a macro for me?
        Clark

        • #609332

          Something like this should do it. It will insert a row above the row which contains the selected cell and then fix up the formula in column E.

          Sub InsertRow()
          Dim lRow As Long
              lRow = Selection.Row - 1
              With ActiveSheet
                  Selection.EntireRow.Insert
                  Range("E1").Offset(lRow - 1, 0).AutoFill _
                    Destination:=Range(.Range("E1").Offset(lRow - 1, 0), .Range("E1").Offset(lRow + 1, 0)), _
                    Type:=xlFillDefault
              End With
          End Sub
          
          • #609965

            Works great! Thanks!

          • #649763

            This macro works very well for me. However, sometimes I would like to delete an entire row because it’s a duplicate entry or for some other reason. This messes up the balance in every subsequent row. My only recourse is to leave the incorrect entry and change its value to 0. Do I need a “delete row” macro now?
            Clark

            • #649808

              The VBA routine below should delete the row containing the active cell and then fix up the formulas in column E.

              Public Sub DelCurRow()
              Dim oCell As Range, lLastRow As Long
                  lLastRow = ActiveSheet.Range("E1").Offset(ActiveSheet.UsedRange.Row + _
                    ActiveSheet.UsedRange.Rows.Count, 0).End(xlUp).Row
                  If ActiveCell.Row <= 2 Then Exit Sub
                  Set oCell = ActiveSheet.Range("E1").Offset(ActiveCell.Row - 2, 0)
                  ActiveCell.EntireRow.Delete
                  oCell.AutoFill Destination:=Range(oCell, _
                    ActiveSheet.Range("E1").Offset(lLastRow - 1, 0)), _
                    Type:=xlFillDefault
              End Sub
              
            • #649818

              Thanks again!

    Viewing 0 reply threads
    Reply To: Update formula for inserted row (97, SR-2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: