VBA to Format Current Selection as a Table

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
503
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

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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
Try this.
Code:
Set rngData = Range(Range("A1"), Range("A1").End(xlToRight).End(xlDown))

ActiveSheet.ListObjects.Add(xlSrcRange, rngData, , xlYes).Name = _ 
"Table2"
 
Upvote 0
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,214,632
Messages
6,120,649
Members
448,975
Latest member
sweeberry

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