Why is this macro changing the headers when converting a range to a table?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
I'm trying to run this code to turn a range with proper headers into a table. When I run this code my headers become "Column1, Column 2, Column 3, etc.). How can I prevent this from happening?

VBA Code:
Sub Macro3()

    Range("A1").Activate
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlYes).Name = _
        "Table1"
    ActiveCell.Range("Table1[#All]").Select
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try replacing the code line as below.
VBA Code:
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Selection.Address), , xlYes).Name = "Table1"

Or just:
VBA Code:
Sub Macro4()
 ActiveSheet.ListObjects.Add(xlSrcRange, Range(Range("A1").CurrentRegion.Address), , xlYes).Name = "Table1"
End Sub
 
Upvote 0
Solution
Try replacing the code line as below.
VBA Code:
ActiveSheet.ListObjects.Add(xlSrcRange, Range(Selection.Address), , xlYes).Name = "Table1"

Or just:
VBA Code:
Sub Macro4()
 ActiveSheet.ListObjects.Add(xlSrcRange, Range(Range("A1").CurrentRegion.Address), , xlYes).Name = "Table1"
End Sub
You guys are gods amongst men. This works and it is perfect

I'm truly trying to learn VBA but I have no idea where to start. I always end up coming back here for help.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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