+ Reply to Thread
Results 1 to 4 of 4

Code to continue formula in column when adding rows

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Code to continue formula in column when adding rows

    I am currently using the following code to add new rows to my sheet:
    Private Sub btAdd_Click()
    If tbRowAdd < 1 Or tbRowAdd > 1500 Then
    tbRowAdd.Text = "": MsgBox "You must enter a valid number from 1 to 1500."
    Else
      Dim r%, c    As Range
      Dim i As Long
      If Not IsNumeric(tbRowAdd.Text) Then
        tbRowAdd.Text = "": MsgBox "You must enter a number."
      Else
        r = CInt(tbRowAdd.Text)
        Set c = ActiveCell.Offset(, 1 - ActiveCell.Column).Resize(, 6) 'cells A:F of the active row
        If WorksheetFunction.Median(1, 5000, r) = r And c.Parent.Name = "ReturnData" Then 'JAMES NOTE - This 5000 is the total limiting number of rows it will handle.
          Application.EnableEvents = False
          Application.ScreenUpdating = False
          c.Offset(1).EntireRow.Resize(r).Insert xlDown
          If Not c.Cells(1, 6).Formula Like "=IFERROR*" Then
            i = c.Row
            Do While i > 1
                i = i - 1
                If c.Offset(-(c.Row - i)).Cells(1, 6).Formula Like "=IFERROR*" Then
                    c.Offset(-(c.Row - i)).Copy c.Offset(1).Resize(r)
                    Exit Do
                End If
            Loop
          Else
            c.Copy c.Offset(1).Resize(r)
          End If
          c.Offset(1, 4).Resize(r, 1).ClearContents
          c.Offset(1, 3).Resize(r, 1).ClearContents
          vColf = Cells(Rows.Count, "F").End(xlUp).Row ' ID For keeping count of number of rows remaining. See ReturnData Worksheet code
          vColG = Cells(Rows.Count, "G").End(xlUp).Row ' ID For keeping count of number of rows remaining. See ReturnData Worksheet code
          
            Dim Zlast As Long
            Dim Zcolm As Range
            Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
            With Sheets("ReturnData")
                Zlast = .Cells(.Rows.Count, "Z").End(xlUp).Row
                If Zlast < 6 Then Zlast = 6
                Set Zcolm = .Range("Z6:Z" & Zlast)
                If awf.CountA(Zcolm) Then
                    Zcolm.Copy
                    Range("G" & vColG).Offset(1).PasteSpecial Paste:=xlPasteValues
                    Zcolm.Value = ""
                    Application.CutCopyMode = False
                    RowStart = Cells(Rows.Count, "D").End(xlUp).Offset(1).Row
                    RowEnd = Cells(Rows.Count, "G").End(xlUp).Row
                    If RowEnd - RowStart >= "1" Then
                        Cells.Range("D" & RowStart, "D" & RowEnd) = Date
                    End If
                End If
            End With
            
          Application.EnableEvents = True
          Application.ScreenUpdating = True
          Unload Me
        Else
          MsgBox "An error has occured. You have likely exceeded the maximum amount of records the system is allowed to handle at one time (5000). We recommend breaking it down into seperate actions. If the error continues please contact the Administrator at [email protected]"
        End If
      End If
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub
    The code continues the formulas in columns A, B C & F in the newly added rows. I have just recently added formulas in the K column. How can I get it to continue the code in that column also?

    Thanks,
    James

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,052

    Re: Code to continue formula in column when adding rows

    Take a look at using Tables, instead of your code. Tables automatically copy formulas down when you start typing data into a new row
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to continue formula in column when adding rows

    The sheet is very complicated and has a lot going on. I tried using tables once and it messed everything up. This has been working great for me, I just need to extend it to work on the K Column also.

  4. #4
    Forum Contributor
    Join Date
    09-16-2013
    Location
    Los Angeles, USA
    MS-Off Ver
    Excel 2011
    Posts
    620

    Re: Code to continue formula in column when adding rows

    I just figure out how to get it to copy down the formula in the F column by changing the 6 to 11 on lines 11, 16 and 20 (see bold).

    However it now for some reason is also copying the data in column G to all the new rows it adds. How can I keep column G clear in all the newly added rows?

    Private Sub btAdd_Click()
    If tbRowAdd < 1 Or tbRowAdd > 1500 Then
    tbRowAdd.Text = "": MsgBox "You must enter a valid number from 1 to 1500."
    Else
      Dim r%, c    As Range
      Dim i As Long
      If Not IsNumeric(tbRowAdd.Text) Then
        tbRowAdd.Text = "": MsgBox "You must enter a number."
      Else
        r = CInt(tbRowAdd.Text)
        Set c = ActiveCell.Offset(, 1 - ActiveCell.Column).Resize(, 11) 'cells A:F of the active row
        If WorksheetFunction.Median(1, 5000, r) = r And c.Parent.Name = "ReturnData" Then
          Application.EnableEvents = False
          Application.ScreenUpdating = False
          c.Offset(1).EntireRow.Resize(r).Insert xlDown
          If Not c.Cells(1, 11).Formula Like "=IFERROR*" Then
            i = c.Row
            Do While i > 1
                i = i - 1
                If c.Offset(-(c.Row - i)).Cells(1, 11).Formula Like "=IFERROR*" Then
                    c.Offset(-(c.Row - i)).Copy c.Offset(1).Resize(r)
                    Exit Do
                End If
            Loop
          Else
            c.Copy c.Offset(1).Resize(r)
          End If
          c.Offset(1, 4).Resize(r, 1).ClearContents
          c.Offset(1, 3).Resize(r, 1).ClearContents
          vColf = Cells(Rows.Count, "F").End(xlUp).Row ' ID For keeping count of number of rows remaining. See ReturnData Worksheet code
          vColG = Cells(Rows.Count, "G").End(xlUp).Row ' ID For keeping count of number of rows remaining. See ReturnData Worksheet code
          
            Dim Zlast As Long
            Dim Zcolm As Range
            Dim awf As WorksheetFunction: Set awf = WorksheetFunction
    
            With Sheets("ReturnData")
                Zlast = .Cells(.Rows.Count, "Z").End(xlUp).Row
                If Zlast < 6 Then Zlast = 6
                Set Zcolm = .Range("Z6:Z" & Zlast)
                If awf.CountA(Zcolm) Then
                    Zcolm.Copy
                    Range("G" & vColG).Offset(1).PasteSpecial Paste:=xlPasteValues
                    Zcolm.Value = ""
                    Application.CutCopyMode = False
                    RowStart = Cells(Rows.Count, "D").End(xlUp).Offset(1).Row
                    RowEnd = Cells(Rows.Count, "G").End(xlUp).Row
                    If RowEnd - RowStart >= "1" Then
                        Cells.Range("D" & RowStart, "D" & RowEnd) = Date
                    End If
                End If
            End With
            
          Application.EnableEvents = True
          Application.ScreenUpdating = True
          Unload Me
        Else
          MsgBox "An error has occured. You have likely exceeded the maximum amount of records the system is allowed to handle at one time (5000). We recommend breaking it down into seperate actions. If the error continues please contact the Administrator at [email protected]"
        End If
      End If
    End If
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Every Two Rows - Insert Formula -> Continue for as many rows as there is data.
    By athyeh in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-10-2014, 04:15 PM
  2. [SOLVED] Code to add rows and continue formulas
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-26-2014, 10:03 PM
  3. Code to add rows and continue formulas needs tweaking
    By Nitefox in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-26-2014, 09:25 PM
  4. [SOLVED] help on vba code to make list continue column and change date format
    By johnodys in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-09-2013, 12:06 PM
  5. The chain of formula doesn't continue at the next rows
    By actonpres in forum Excel General
    Replies: 2
    Last Post: 04-21-2012, 02:54 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1