Dynamically refer to a table name

freelensia

New Member
Joined
Jul 2, 2017
Messages
18
Hello everybody, I am joining the forum for the first time. Hope to learn a lot from everybody and exchange useful information.

With Excel 2013 we can use Tables just as named ranges.

I am trying to refer to the Table "Extract". However, this table name may change in the future. As such, I would like to register it once as a public variable then refer to the public variable name for the rest of the Module. An example is the following:

Code:
Public ExtractTable As Range

Sub ResizeExtract()
Set ExtractTable = Range("Extract")

Range(ExtractTable.Name&"[[#Headers],[username]]").ColumnWidth = 10.75

End Sub

However I get syntax error.

Could somebody help me? Thanks!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi and welcome to the MrExcel Message Board.

If it were me, I would take the time to learn about ListObjects. This is what Tables used to be called in previous versions of Excel.

There is a whole object model based on ListObjects and it allows tou to manipulate Tables easily.

OK, the syntax gets a bit of getting used to but it is what I would do. Your question becomes:
Code:
Sub ResizeExtract()
    Dim ExtractTable As ListObject
    
    With ThisWorkbook.Worksheets("Sheet1")
        Set ExtractTable = .ListObjects("Extract")
        ExtractTable.ListColumns("username").Range.ColumnWidth = 10.75
    End With
    
End Sub

For an introduction to what is possible you could do worse than look here: https://www.thespreadsheetguru.com/blog/2014/6/20/the-vba-guide-to-listobject-excel-tables



Rergards,
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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