How to check that a label is a valid table name?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,550
Office Version
  1. 365
Platform
  1. Windows
Is there VBA code that I can use to check whether a text string, such as "Table12", is the name of a valid table in the calling sheet?

This is what I am currently doing. Is there a better way?

VBA Code:
Dim loTable As ListObject
  On Error GoTo BadTableName
  Set loTable = Range(rnTable).ListObject
  GoTo TableNameOK
BadTableName:
  Call ErrMsg("Invalid table name (" & rnTable & ")", MyName)
  Exit Sub
TableNameOK:

Thanks
 
Last edited:

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
You could simply loop through the listobjects on the sheet and check each one's name. There's nothing particularly wrong with your current approach , though I'd probably make it its own function rather than using Goto statements.
 
Upvote 0
You could simply loop through the listobjects on the sheet and check each one's name. There's nothing particularly wrong with your current approach , though I'd probably make it its own function rather than using Goto statements.
Yeah, I don't like Go To statements, either.

If I use a function, can I pass the listobject (loTable) as a By Ref parameter and load it in the function?

Thanks
 
Upvote 0
Normally, you'd return the listobject (or nothing) as the result of the function and assign that to your loTable variable:

VBA Code:
Set loTable = GetTableByName("table name", ws)
If loTable is nothing then exit sub

for example.
 
Upvote 0
Solution
Normally, you'd return the listobject (or nothing) as the result of the function and assign that to your loTable variable:

VBA Code:
Set loTable = GetTableByName("table name", ws)
If loTable is nothing then exit sub

for example.
Ok, thanks
 
Upvote 0

Forum statistics

Threads
1,216,375
Messages
6,130,245
Members
449,568
Latest member
mwl_y

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