Copy & Paste a Table Row that contains Excel Formulas using VBA

winsbury

New Member
Joined
Feb 10, 2022
Messages
12
Office Version
  1. 2007
Platform
  1. Windows
I have a large and complex table in excel, some of the cells in the first row contains formulas including some array formulas.

Users can input data into rows 2 and below via forms to avoid overwriting the formulas, the formulas fill a number of the cells automatically based on lookups etc from the users data input.

Periodically I want to delete all the user data and only retain the first row.

I have tried a number of different methods but the only one which works is extremely slow:

VBA Code:
    Dim oSheetName As Worksheet
    Dim sTableName As String
    sTableName = "MyTable"
    Set oSheetName = Sheets("MySheetName")

    FirstRecord = 2
    LastRecord = loTable.DataBodyRange.Rows.Count
    
    For i = LastRecord To FirstRecord Step -1
        loTable.ListRows(i).Delete
    Next


A much faster method of deleting the entire table contents is

VBA Code:
loTable.DataBodyRange.Rows.Delete

In order to use this method I must copy the content of row 1 so that it can be pasted back again after the deletion.

I have tried a variety of different solutions, none of which work well, the nearest I have doesn't preserve the formulas.

VBA Code:
    Dim MyRow As Variant
    MyRow = loTable.DataBodyRange.Rows(1)
    loTable.DataBodyRange.Rows.Delete
    loTable.ListRows.Add
    loTable.ListRows(loTable.ListRows.Count).Range.Resize(UBound(MyRow, 1)).Value = MyRow        'Only pastes values, formulas are lost !!!

Any other ideas I could try ?

Thanks in advance.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Is the data input via a form or directly into the sheet?
Everything is via forms to keep users away from entering data directly into tables. Admin is also done via forms hence the need to be able to delete the rows except row 1 using VBA, its very easy manually within excel but just as easy to make an error so really want this done within the scripting.
 
Upvote 0
Define a named range, write a macro to clear the named range

[namedRange].clearcontents

Do you need to delete the rows or just clear them?
 
Upvote 0
Sorry, I never assume. Have your tried to record the macro and see what code is generated?
 
Upvote 0
Sorry, I never assume. Have your tried to record the macro and see what code is generated?
no problem. Yes , recording a macro while selecting and deleting multiple rows in a table simply generates multiple deletes, exactly equivalent to my first code example so just as slow when run in the main code.
 
Upvote 0
Are there any formulas that will refer to the cells that contain the data that you are looking to delete?
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top