+ Reply to Thread
Results 1 to 4 of 4

Macro - Insert new row in table but copy selected cells from previous row

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    97

    Macro - Insert new row in table but copy selected cells from previous row

    Here is my code:
    Sub CopyRow()
    
    Dim ws As Worksheet
    Set ws = Sheets("ERROR REPORT")
    Const strTableName As String = "Table2"
    
    Dim Tbl As ListObject
    Dim NewRow As ListRow
    
    With ws
        .Unprotect ("mdqc")
        ActiveWorkbook.Unprotect Password:=("mdqc")
        Set Tbl = .Range(strTableName).ListObject
        With Tbl
            Set NewRow = .ListRows.Add(AlwaysInsert:=True)
            .DataBodyRange.Locked = True
            NewRow.Range.Locked = False
            NewRow.Range(1, 1) = Date
        End With
        .Protect ("mdqc"), AllowFiltering:=True
        ActiveWorkbook.Protect Structure:=True, Windows:=False, Password:=("mdqc")
        
    End With
    End Sub
    Everything works great, it currently inserts a new blank row each time. But now looking to copy the previous cell from column B,D and E with the option to overwrite the data if necessary. All this will do is save some input time for the end user.

    Side note, when we insert a new row, we have it configured that it locks all the previous rows of data to prevent data changes. ( .DataBodyRange.Locked = True)

    Is this possible?

    Much appreciated,

    Jon

  2. #2
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Macro - Insert new row in table but copy selected cells from previous row

    Does this do what you are looking for?

    Sub CopyRow()
    
    Dim ws As Worksheet
    Set ws = Sheets("ERROR REPORT")
    Const strTableName As String = "Table2"
    
    Dim Tbl As ListObject
    Dim NewRow As ListRow
    
    With ws
        .Unprotect ("mdqc")
        ActiveWorkbook.Unprotect Password:=("mdqc")
        Set Tbl = .Range(strTableName).ListObject
        With Tbl
            Set NewRow = .ListRows.Add(AlwaysInsert:=True)
            NewRow.Range(1, 1) = Date
            NewRow.Range(1, 2) = NewRow.Range(-1, 2)
            NewRow.Range(1, 4) = NewRow.Range(-1, 4)
            NewRow.Range(1, 5) = NewRow.Range(-1, 5)
            .DataBodyRange.Locked = True
            NewRow.Range.Locked = False
            
        End With
        .Protect ("mdqc"), AllowFiltering:=True
        ActiveWorkbook.Protect Structure:=True, Windows:=False, Password:=("mdqc")
        
    End With
    End Sub
    Some people volunteer in soup kitchens or hospitals. I choose to make the world better by trying to help you with Excel. We're all learning.

    <---Click * Add Reputation for all helpful comments. It's like giving a smile.
    Forum Rules: How to mark your post [Solved] and have a happier, Excel enriched life.

  3. #3
    Registered User
    Join Date
    04-02-2013
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Macro - Insert new row in table but copy selected cells from previous row

    You addressed my issue, just altered the logic slightly, instead of (-1, 2) etc. I changed it to (0, 2) as every second row inserted would copy the alternate row behind the last one. Thank you so much. I also added Reputation!

  4. #4
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Macro - Insert new row in table but copy selected cells from previous row

    Thank you for the rep. I am glad it helped.

+ 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] Macro to insert a rage of cells deletes previous content if range is blank
    By 54ed in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-24-2014, 12:01 AM
  2. [SOLVED] Macro to insert a rage of cells deletes previous content if range is blank
    By 54ed in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-23-2014, 05:59 PM
  3. [SOLVED] Macro to copy and paste selected cell to a table.
    By JohnDoh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-24-2013, 03:13 AM
  4. Macro to insert formulas to certain cells respective of cell selected
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-21-2011, 10:51 PM
  5. copy data from selected cells , in selected row and paste it to ms word table ?
    By aaashaaa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2010, 03:25 PM
  6. I want to copy a row- with a macro and insert on any selected row
    By 2newguy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-27-2007, 03:39 PM
  7. How do I copy selected cells in excel and paste into word table
    By REVA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2006, 05:45 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