Macro hard coding cell reference when creating a table

Noddy13

New Member
Joined
Jan 25, 2010
Messages
45
Hello, I am trying to set up a macro that will select a varying amount of data and then format it into a table. I'm selecting the data using the ctrl shift keys to get to the end cells however when I create the table it locks in the cells from the original selection so the table size is static even though the selected area varies. What am I missing?

Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$E$10"), , xlNo).Name = _
"Table5"
Range("Table5[#All]").Select
ActiveSheet.ListObjects("Table5").TableStyle = "TableStyleLight2"

All advise appreciated.

Thanks
Jamie
 

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.
Assuming you want to select a range and then have that range now be a table.
Try this:

Be sure your Table name is not repeated:

Code:
Sub My_New_Table()
'Modified  4/16/2019  2:25:03 AM  EDT
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlNo).Name = _
"Table5"
Range("Table5[#All]").Select
ActiveSheet.ListObjects("Table5").TableStyle = "TableStyleLight2"
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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