VBA to Format Current Selection as a Table

nirvehex

Active Member
Joined
Jul 27, 2011
Messages
493
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to format the current selection as Table2.

My code:

Code:
Range("A1").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Range(Selection, Selection.End(xlDown)).Select 
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$CP$52"), , xlYes).Name = _ 
"Table2"

How do I get rid of that A1:CP52 range and just do current selection?

Thanks.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Code:
Range("A1").SelectRange(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlYes).Name = _
"Table2"
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
Set rngData = Range(Range("A1"), Range("A1").End(xlToRight).End(xlDown))

ActiveSheet.ListObjects.Add(xlSrcRange, rngData, , xlYes).Name = _ 
"Table2"
 
Upvote 0

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
Try this

Code:
Dim rng As Range
Dim loc As String
loc = Selection.Address

ActiveSheet.ListObjects.Add(xlSrcRange, Range(loc), , xlYes).Name = _
"Table2"
 
Upvote 0

Forum statistics

Threads
1,191,607
Messages
5,987,637
Members
440,104
Latest member
thigarette

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
Top