+ Reply to Thread
Results 1 to 4 of 4

Macro to insert a new row only at a certain point AND copy formulas down correctly

  1. #1
    Registered User
    Join Date
    10-21-2018
    Location
    US
    MS-Off Ver
    Version 16.18
    Posts
    6

    Macro to insert a new row only at a certain point AND copy formulas down correctly

    I need help with writing a macro that will insert a new row only one row above the main border of my template AND copy formulas down in that row .

    My template is 25 rows long, with formulas in columns Q-R. I only want to allow the user to insert rows above row 23 (where I have one of the main borders), or whatever row it becomes after a new row has been added.

    This means that when the user first opens the template, they have 22 rows to put data, if they need another row for more data, they will click a button and a new row will be added beneath it. Now, they have 23 rows of data and again, if they need another, they'll click the button. The macro will need to keep track where the main border is (initially on row 23, but now after two rows were added, it is now on row 25).

    I had the code below, initially, but this allows the user to add a new row wherever the active cell they selected is located and it uses "double-click", not a button. My code needs to be limited to above row 23 starting off and tracking after any added row. I don't want them to be able to add a new row wherever, even if it is above the main border. A new row can only be inserted ONE row above the main border.

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    ActiveSheet.Protect Password:="1234", UserInterfaceOnly:=True
    With Target
    .Offset(1).EntireRow.Insert
    .EntireRow.Copy .Offset(1).EntireRow(1)
    On Error Resume Next
    Target.Offset(1).EntireRow.SpecialCells(xlConstants).ClearContents
    End With

    End Sub

  2. #2
    Forum Contributor
    Join Date
    10-13-2015
    Location
    Culloden, West Virginia
    MS-Off Ver
    2010
    Posts
    168

    Re: Macro to insert a new row only at a certain point AND copy formulas down correctly

    more of a work around...
    can you have your users input their data one row at a time and after each row is complete click a macro button that inserts a row at the top? That way your insert point is always at the same row. Easy to set up.
    But by doing this your data is now going to be newest at the top instead of the bottom.

  3. #3
    Registered User
    Join Date
    10-21-2018
    Location
    US
    MS-Off Ver
    Version 16.18
    Posts
    6
    Quote Originally Posted by jme1013 View Post
    more of a work around...
    can you have your users input their data one row at a time and after each row is complete click a macro button that inserts a row at the top? That way your insert point is always at the same row. Easy to set up.
    But by doing this your data is now going to be newest at the top instead of the bottom.
    I don’t think so because the columns Q-R have formulas for cumulative sums and other formulas that only work from top to bottom.

    I was thinking of marking a row with a symbol, perhaps a “*” or something for the purpose of simplifying the code. I would then use the Search command and wherever that * is, write a macro to know to add a row above (or at, whichever is easier) that asterisk is.

    There’s also the option for writing a macro to find last filled row(last row with data?) and then adding a row there?

    I have knowledge of bits a pieces of vba code but struggling putting it all together.

  4. #4
    Forum Contributor
    Join Date
    10-13-2015
    Location
    Culloden, West Virginia
    MS-Off Ver
    2010
    Posts
    168

    Re: Macro to insert a new row only at a certain point AND copy formulas down correctly

    Please attach an example of your worksheet if you can.

+ 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. [SOLVED] VBA Code/Macro to insert rows into a protected sheet and copy formulas down
    By grahamlees in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-20-2017, 10:40 AM
  2. Macro to insert new row and copy formulas
    By kwood41799 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-21-2015, 12:34 PM
  3. [SOLVED] Insert table row not copying formulas correctly
    By dr01allen in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 06-24-2015, 11:38 AM
  4. Macro to insert new row and copy formulas from the above row.
    By manny kang in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-11-2014, 08:53 AM
  5. Macro to insert rows in multiple sheets and copy formulas
    By syt0x in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2013, 11:06 AM
  6. Replies: 2
    Last Post: 12-18-2012, 05:22 AM
  7. Macro to insert copy and insert formulas only to next blank row
    By bob in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2006, 07:10 AM

Tags for this Thread

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