Share via

Macro to Format Table

Anonymous
2011-04-06T16:35:27+00:00

Hi,

I've recorded a macro and the result as as below:

    Range("A1").Select

    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$AO$472"), , xlNo).Name = _

        "Table1"

    Range("Table1[#All]").Select

    ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight13"

But instead of fixing the Range as ("$A$1:$AO$472"), how can I make it to select different range?

Thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2011-04-07T11:57:56+00:00

If row 1 already contains column headers (field names), change xlNo to xlYes.

Was this answer helpful?

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2011-04-06T19:26:52+00:00

If you want to turn the current selection into a range, change

    Range("A1").Select

ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$AO$472"), , xlNo).Name = _

"Table1"

to

ActiveSheet.ListObjects.Add(xlSrcRange, Selection, , xlNo).Name = _

"Table1"

If you want to turn the contiguous range containing A1 into a table, use

ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlNo).Name = _

"Table1"

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2011-04-07T08:02:23+00:00

    The second one work but not sure why it added another row on top as a header? This is the code I used

    Range("A1").Select

        ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlNo).Name = _

        "Table1"

        Range("Table1[#All]").Select

        ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight13"

    Was this answer helpful?

    0 comments No comments