adding named ranges without a "-"

M

mark kubicki

I'm writing a sub that works thru a worksheet adding named ranges for all of
the values in a given column (the values are person's names); and I am
naming these "named ranges" effectively the same as the person's name
(ex: the person's name might be "Tom", so the range is named
"Proj_mngr_Tom")

one of the values person's name has a "-" (Ju-Li) which is causing an
error...

is there away of stripping the "-"? (I am presuming I'll need to write a
sub that cycles thru each letter, checking its value...)

thank in advance
-mark
 
R

RB Smissaert

This is a function I use to make valid range names:

Function MakeValidRangeName(ByVal strRangeName As String, _
Optional strAddIfTrailingNumerics As String =
"_") As String

Dim strInvalidChars
Dim i As Byte

If Len(strRangeName) = 0 Then
MakeValidRangeName = "No_Name_Provided"
Exit Function
End If

'delete leading numerics
'-----------------------
strRangeName = ClearLeadingNumerics(strRangeName)

'note that the characters \ and . are valid
'------------------------------------------
strInvalidChars = Array("!", "£", "$", "%", "%", "^", "&", _
"*", "(", ")", "-", "+", "=", "{", _
"}", "[", "]", ":", ";", "@", "'", _
"~", "#", "|", "<", ",", ">", "?", _
"/")

strRangeName = Replace(strRangeName, " ", "_", 1, -1, vbBinaryCompare)

For i = 0 To UBound(strInvalidChars)
strRangeName = Replace(strRangeName, _
strInvalidChars(i), _
"", _
1, _
-1, _
vbBinaryCompare)
Next i

'to avoid range names ending with numerics
'-----------------------------------------
If Asc(Right$(strRangeName, 1)) > 47 And _
Asc(Right$(strRangeName, 1)) < 58 Then
strRangeName = strRangeName & strAddIfTrailingNumerics
End If

strRangeName = Left$(strRangeName, 255)

MakeValidRangeName = strRangeName

End Function


RBS
 
G

Guest

You can evaluate the individuals name with the REPLACE function.
REPLACE(NameValue,FindString,Replacestring)

Replace(NameValue, "-","_")
 
R

RB Smissaert

Forgot to post the function to clear leading numerics:

Function ClearLeadingNumerics(strString As String) As String

Do While Asc(Left(strString, 1)) > 47 And _
Asc(Left(strString, 1)) < 58
strString = Mid(strString, 2)
Loop

ClearLeadingNumerics = strString

End Function


RBS


RB Smissaert said:
This is a function I use to make valid range names:

Function MakeValidRangeName(ByVal strRangeName As String, _
Optional strAddIfTrailingNumerics As String =
"_") As String

Dim strInvalidChars
Dim i As Byte

If Len(strRangeName) = 0 Then
MakeValidRangeName = "No_Name_Provided"
Exit Function
End If

'delete leading numerics
'-----------------------
strRangeName = ClearLeadingNumerics(strRangeName)

'note that the characters \ and . are valid
'------------------------------------------
strInvalidChars = Array("!", "£", "$", "%", "%", "^", "&", _
"*", "(", ")", "-", "+", "=", "{", _
"}", "[", "]", ":", ";", "@", "'", _
"~", "#", "|", "<", ",", ">", "?", _
"/")

strRangeName = Replace(strRangeName, " ", "_", 1, -1, vbBinaryCompare)

For i = 0 To UBound(strInvalidChars)
strRangeName = Replace(strRangeName, _
strInvalidChars(i), _
"", _
1, _
-1, _
vbBinaryCompare)
Next i

'to avoid range names ending with numerics
'-----------------------------------------
If Asc(Right$(strRangeName, 1)) > 47 And _
Asc(Right$(strRangeName, 1)) < 58 Then
strRangeName = strRangeName & strAddIfTrailingNumerics
End If

strRangeName = Left$(strRangeName, 255)

MakeValidRangeName = strRangeName

End Function


RBS


mark kubicki said:
I'm writing a sub that works thru a worksheet adding named ranges for all
of the values in a given column (the values are person's names); and I am
naming these "named ranges" effectively the same as the person's name
(ex: the person's name might be "Tom", so the range is named
"Proj_mngr_Tom")

one of the values person's name has a "-" (Ju-Li) which is causing an
error...

is there away of stripping the "-"? (I am presuming I'll need to write a
sub that cycles thru each letter, checking its value...)

thank in advance
-mark
 
D

Dave Peterson

Dim myStr As String
myStr = "Ju-Li"
'xl2k or higher
myStr = Replace(myStr, "-", "_")
'or before xl2k
'myStr = Application.Substitute(myStr, "-", "_")
MsgBox myStr


For just that one problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top