Funny behaviour when trying to check for a range's name

bernatbosch

Board Regular
Joined
Dec 12, 2015
Messages
66
Hi everybody!


Let's see if someone can help me understand this:


I have a table object in my worksheet (or many, but let's assume I have one, to make it easy).
The user will be able to add one or two elements above the table, which are meant to work together with the table's data, in a pair of ranges that have to be called 'table statistics' and 'table fields', respectively. Therefore, placed above the table, the user can choose to have one of these ranges, both ranges, or have just the table alone.
When the range called 'table statistics' is created (by a call of the user) the same procedure incorporates a cell at the top in which a title is added: "STATS". This cell is given a name that may help users to know what are the sheet and the table to which the range actually refers to (something like: 'Sheet1Table1StatsTitle').
A similar thing happens when the user creates the range called 'table fields': also a cell is being named (something like: 'Sheet1Table1FieldsTitle').
In case the 'statistics' range for this particular table does exist, the cell corresponding to StatsTitle name is always meant to be placed 20 rows above the table object.
In case the 'fields' range for this particular table does exist, the cell corresponding to FieldsTitle name is always meant to be placed 9 rows above the table object.


In order to apply certain procedure, one a table object is selected, I need to check whether the selected table has one of these ranges, both ranges, or the table is alone.
To do so, I try to check whether the cell placed 9 rows above the table object (first table column) has a name or not. If it has, I may consider the table has the fields range. If not, I may consider the table has not the field's range.
Also, I try to check whether the cell placed 20 rows above the table object (first table column) has a name or not. If it has, I may consider the table has the stats range. If not, I may consider the table has not the stats range.
Simple!


To check for the existance of these two names (named ranges's names) I use the following syntax:


If FieldsTitleRng.Name.Name <> vbNullString Then


and


If StatsTitleRng.Name.Name <> vbNullString Then


If the table being checked has not its fields range (fields range does not exist) the code returns an error, so I tell the procedure to go to certain line when this error occurs and there to note that the table has no fields range.


Untill this moment everyting works just fine.


BUT, once I've checked whether the selected table has fields range or not, when I try to check whether the selected table has stats range or not (by using the same procedure) in the case that in my first checking I got an error (so that fields range did not exist for the selected table) I get a new error that I'm not able to catch.


The result is that, under these circumstances (when the selected table has no names associated with it) in the checking for the existance of the second named range, I get the following message:


Run-time error '1004':
Method 'name' of object 'range' failed.


CAN ANYONE TELL ME WHY THE FIRST TIME I CHECK FOR A NAMED RANGE EXISTANCE USING THIS SYNTAX I CAN CATCH THE ERROR PRODUCED BY THE NON EXISTANCE OF THE NAME, BUT THE SAME SYNTAX DOES NOT ALLOW ME TO CATCH THE SAME ERROR FOR THE SECOND TIME?


Here is my code (neither FieldsTitleRng and StatsTitleRng have a name):


'check fields


On Error GoTo SelectedTableHasNoFields


Set FieldsTitleRng = TableObject.Range.Cells(1, 1).Offset(-9, 0).Resize(1, 1)

If FieldsTitleRng.Name.Name <> vbNullString Then 'Line that works fine

SelectedTableHasFields:
TableHasFields = True
Else
SelectedTableHasNoFields:
Err.Clear
TableHasFields = False
End If



'check stats


On Error GoTo SelectedTableHasNoStats


Set StatsTitleRng = TableObject.Range.Cells(1, 1).Offset(-20, 0).Resize(1, 1)


If StatsTitleRng.Name.Name <> vbNullString Then 'Line I get a Run-time error '1004': Method 'name' of object 'range' failed.
SelectedTableHasStats:
TableHasStats = True
Else
SelectedTableHasNoStats:
Err.Clear
TableHasStats = False
End If
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Because you don't have a Resume statement or other method to clear the current exception. (see here: On Error WTF? | Excel Matters)

I would suggest you simply use a local error handler:

Code:
Dim sName as string
On Error Resume Next
sName = FieldsTitleRng.Name.Name
On Error Goto 0
If sName <> vbNullString Then

for example.
 
Upvote 0
I expect that the first error hander is still active when the second error occurs, so the second error is not trapped by the On Error statement. Here's a good write up on error handling:

Error Handling In VBA
 
Upvote 0
Grate! Your suggestion works perfectly! Thank you so much. :biggrin:

I really need to improve my knowledge on error handling…

This is how it looks now:

'check fields

Set FieldsTitleRng = TableObject.Range.Cells(1, 1).Offset(-9, 0).Resize(1, 1)

On Error Resume Next
sName = FieldsTitleRng.Name.Name
On Error GoTo 0

If sName <> vbNullString Then
TableHasFields = True
Else
TableHasFields = False
End If


'check stats

Set StatsTitleRng = TableObject.Range.Cells(1, 1).Offset(-20, 0).Resize(1, 1)

On Error Resume Next
sName = StatsTitleRng.Name.Name
On Error GoTo 0

If sName <> vbNullString Then
TableHasStats = True
Else
TableHasStats = False
End If
 
Upvote 0
Thank you very much for your answer!
Everything seems fairly simple once you got to know it! :rolleyes:
But I've spent one hour trying to find out for my own without understanding it. :p

Thanks.
 
Upvote 0
You should use a separate variable for each test, or reset it after the first test. Otherwise if the first test passes, the variable will still have that name in it after the second error occurs.
 
Upvote 0
Either of those will work.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,375
Members
448,888
Latest member
Arle8907

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