Convert Entire Sheet to a Named Table for a Pivot Table

default_name

Board Regular
Joined
May 16, 2018
Messages
170
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
Hi guys,

I have looked online to try and find some solutions but I keep getting an error.
Perhaps I am messing up with the syntax.

I want the vba to go to the worksheet titled "Index", and then to convert all of the data on that sheet into a table called "Source"
(similar to going to the "Index" sheet, hitting control+A, and setting the range as a table called "Source")

Here is what I have right now:
VBA Code:
    Sheets("Index").Select
    Range("A1").Select
    Dim tbl As ListObject
    Dim rng As Range
    Set rng = Range(Range("A1"), Range("A1").SpecialCells(xlLastCell))
    Set tbl = ActiveSheet.ListObjects.Add(xlSrcRange, rng, , xlYes).Name _
        = "Source"

What am I doing wrong here?

The reason I am doing this is that I want to build a pivot table that references all of the data on the Index worksheet.
Previously, the pivot table was based off of a large range on the Index worksheet, but since the amount of data on the Index sheet varies, the pivot table occasionally ends up with (blank) rows.
I figure that if I convert all of the data on the Index sheet to being a table, that I can simply reference that table name ("Source") (instead of a set range) for the pivot table (this worked when I did it manually).
If there is a more simple method that wouldn't require me to convert to a table, I would be open to ideas.

Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
How about
VBA Code:
Sub defaultname()
   With Sheets("Index").Range("A1").CurrentRegion
      .Parent.ListObjects.Add(xlSrcRange, Range(.Address(, , , True)), , xlYes).Name = "Source"
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub defaultname()
   With Sheets("Index").Range("A1").CurrentRegion
      .Parent.ListObjects.Add(xlSrcRange, Range(.Address(, , , True)), , xlYes).Name = "Source"
   End With
End Sub
Works perfectly! Thanks Fluff!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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