How can I update Excel table values & headings with an array?

JTannas

New Member
Joined
Mar 8, 2010
Messages
29
Hello Everyone,

Can anyone suggest a clean way of updating an excel table from an array that includes new column headings?

I've got a macro that outputs an array of column headings and values to a spreadsheet, and then formats the output as a table.
When updating the information using the macro, I'd like to preserve the same table (so that my spreadsheet formulas stay intact).

The problem is that when I dump the array values into the table range, the table gets deleted.
Any solutions I've found are pretty ugly.

The code goes like this:
Code:
[COLOR=#333333]
[/COLOR]'Grab the table if it already exists
For each objTable in MySheet.ListObjects
      If objTable.Name = "MyTable" then
               Set MyTable = MyRange
               Exit For
      End if
Next

'If the table does not exist, create it
If MyTable is Nothing then
       Set MyTable = MySheet.ListObjects.Add(xlSrcRange, MyRange, , xlYes)
       MyTable.Name = "MyTable"
End If

' Perform the data dump
[COLOR=#333333]MyRange.Value2 = Application.Transpose(arrValues)
[/COLOR]
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
if the table already exists do not bring across table headings, just data pasted in the first empty row, then format cells as needed
 
Upvote 0
I would prefer to update the table columns as well, in case any columns are added to the output array.
 
Upvote 0
say you have 3 columns and 10 rows of data, and you wish to import 5 columns (ie 2 new columns) with 20 rows of data - this is tricky. Do you have any idea of the likely columns you may add - if so set them up on your table right from the start.

I think you need a spare cell in your main sheet that tells you the number of the first empty row, and to run a macro that looks at existing headings to determine if new headings need to be created.

It is unusual, to say the least, to import data that does not readily fit into your existing table - are you certain it has to be this way?
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,387
Members
449,080
Latest member
Armadillos

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