Declaring a Table Name after VBA creates column headers

jdlev

New Member
Joined
Jan 6, 2017
Messages
27
Hi Guys,

Been trying to figure this out using ListObjects.Add, but so far no luck. Here's the scenario...

Let's say VBA code dynamically produces an unknown number of column headers on a separate worksheet. I want to declare a table, and format it as such based on how many columns are produced.

So let's say it makes Col1, Col2, and Col3. Now, I have a function that tells me how many columns there are, and I can convert the number of columns into the actual alpha characters that represent the column. So my code will actually tell me that Col3 lies in column 'C' in excel before declaring my table name.

Either a Loop that determines the first empty cell in column A or declaring the range based on what I already know (A1:C1) should work. I just don't know how to make the range dynamic so it accepts my range inputs properly. If it's a static cell reference it's no problem. I've used this...but I'm not sure how to change the "A1:C1" in Range to a dynamic value?:

Sheet1.ListObjects.Add(xlSrcRange, Range("A1:C1"), , xlYes).Name = "myTable1"
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
If you know how many columns there are, and where the table starts, you could use this:

Code:
Sheet1.ListObjects.Add(xlSrcRange, Range("A1").Resize(, number_of_columns, , xlYes).Name = "myTable1"
 
Upvote 0
Do you need to know/use the number of columns? Could you use
Code:
Sheet1.ListObjects.Add(xlSrcRange, Sheet1.Range("A1").CurrentRegion, , xlYes).Name = "myTable1"
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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