3

I am trying to write a Macro that goes through all of the Tables in a workbook and then adds the name of each table in the form of "TableName[#All]" to an array.

See incomplete code below

Sub NewShopPage()
'
' Macro that creates a duplicate sheet from a hidden template
' searches for every table in the workbook and then adds them to the
' PivotTableWizard.
'

Dim NewShop As Variant
NewShop = InputBox("What Shop are you creating a new estimate sheet for? (example: Shop 18)")

NewShop = Replace(NewShop, " ", "_")

Sheets("CE_Template").Copy After:=Sheets("Project Estimator")
ActiveSheet.Name = NewShop


ActiveSheet.ListObjects(1).Name = NewShop

Sheets("Project Estimator").Select
ActiveSheet.ListObjects(1).Select

Dim MyArray As Variant

    'HELP NEEDED HERE:
    'For each [Table] in [workbook]
    '    Add TableName + "[#All]" to MyArray
    'Next

ActiveSheet.PivotTableWizard SourceType:=xlConsolidation, SourceData:=MyArray


End Sub

I have only come to the conclusion that it must operate this way because if a user adds a worksheet then later deletes it, the PivotTableWizard errors out.

I am not sure if doing it this way each time will erase the formatting the PivotTable has.

2 Answers 2

1

The answer was:

Dim MyArray() As String
Dim ArraySize As Integer
Dim ws As Worksheet
Dim Tbl As ListObject

ArraySize = 0

ReDim MyArray(0 To 0)

For Each ws In ActiveWorkbook.Worksheets
    For Each Tbl In ws.ListObjects
        ReDim Preserve MyArray(ArraySize) As String
        MyArray(UBound(MyArray)) = Tbl.Name & "[#All]"
        ArraySize = ArraySize + 1
    Next Tbl
Next ws
Sign up to request clarification or add additional context in comments.

Comments

0
Dim tbl As ListObject
For Each tbl In ActiveSheet.ListObjects
    'Do stuff to tbl here
Next tbl

Also, I would recommend this site if you are going to be working with tables a lot. http://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables

1 Comment

Thanks, but the "do stuff here" part is really what I need. This doesnt solve my main question of how to add each of those tables to an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.