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

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,540
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

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.
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,215,799
Messages
6,126,976
Members
449,351
Latest member
Sylvine

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