Validate named range names

Deutz

Board Regular
Joined
Nov 30, 2009
Messages
191
Office Version
  1. 365
Platform
  1. Windows
Hi and thanks in advance,

I have a list of names and corresponding range addresses in a sheet that I would like to create named ranges from in VBA. I have that part working ok but I would like to validate each name before I create the named range, in case the name has reserved characters, references a cell address, too long etc which will cause my 'add name' code to crash.

Is there a simple way I could do this in VBA?

Kind regards
Deutz
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try using this function:
VBA Code:
Function valid_name(tx As String) As Boolean
     On Error Resume Next
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
        If Err.Number = 0 Then valid_name = True
     On Error GoTo 0
End Function

For example, you want to create a named range "abc_1", you use it like this:
VBA Code:
If valid_name("abc_1") Then
    'create the named range here
End If
 
Upvote 1
Solution
Try using this function:
VBA Code:
Function valid_name(tx As String) As Boolean
     On Error Resume Next
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
        If Err.Number = 0 Then valid_name = True
     On Error GoTo 0
End Function

For example, you want to create a named range "abc_1", you use it like this:
VBA Code:
If valid_name("abc_1") Then
    'create the named range here
End If
Thankyou Akuini, that works exactly as required. Much appreciated :)
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0
@Deutz
I forgot something.
The 'valid_name' function in post #2 actually creates a named range in this part:
VBA Code:
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
it would be better if it is deleted once it is created, because there's still a chance that it won't be deleted when you create a named range with the same name in this part:
VBA Code:
If valid_name("abc_1") Then
    'create the named range here
End If
the reason is that you can have 2 named ranges with the same name if they are in different scope.
So use this one instead:
VBA Code:
Function valid_name(tx As String) As Boolean
     On Error Resume Next
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
        If Err.Number = 0 Then
            valid_name = True
            ThisWorkbook.Names(tx).Delete
        End If
     On Error GoTo 0
End Function
 
Upvote 0
Our (free!) Name Manager has a built-in way to:
- Generate a list of all range names
- Use the same list (after editing or adding names) to update the document with the edited and added names
It will report any names that are "illegal"
 
Upvote 0
@Deutz
I forgot something.
The 'valid_name' function in post #2 actually creates a named range in this part:
VBA Code:
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
it would be better if it is deleted once it is created, because there's still a chance that it won't be deleted when you create a named range with the same name in this part:
VBA Code:
If valid_name("abc_1") Then
    'create the named range here
End If
the reason is that you can have 2 named ranges with the same name if they are in different scope.
So use this one instead:
VBA Code:
Function valid_name(tx As String) As Boolean
     On Error Resume Next
        ThisWorkbook.Sheets(1).Range("A1").Name = tx
        If Err.Number = 0 Then
            valid_name = True
            ThisWorkbook.Names(tx).Delete
        End If
     On Error GoTo 0
End Function
Thanks again Akuini, works well :)
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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