VBA Help

brinks19

New Member
Joined
Sep 21, 2017
Messages
10
I have two tables named Starting & Ending on two different sheets. I would like to have a macro that clears the starting table and then copies whatever is in the ending table over to the starting table. There are formulas in each table; they're the same format. I need to be able to cycle continuously, but with what I have I'm only able to do it once before I run into errors since the table name changes. I'm not sure if what I have is currently the best way to do it. Currently have this:

Private Sub CommandButton1_Click()
Sub ResetTable()


Dim tbl As ListObject


Set tbl = ActiveSheet.ListObjects("Starting")


'Delete all table rows except first row
With tbl.DataBodyRange
If .Rows.Count > 1 Then
.Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count).Rows.Delete
End If
End With


'Clear out data from first table row (retaining formulas)
tbl.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents


End Sub


Sub CopyTables()


Worksheets(1).ListObjects("Ending").Range.Copy _
Destination:=Worksheets(2).Range("B1")




End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You can refer to your table by index number. For example, assuming that the active sheet contains only one table, you can refer to it as follows...

Code:
[COLOR=#333333]Set tbl = ActiveSheet.ListObjects(1)[/COLOR]

Hope this helps!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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