.Name case sensitive

C

CinqueTerra

As part of a Print Macro, I am populating a list box with all range names
beginning with "prt1". In testing my macro, I noted that it was case
sensitive ("prt1 vs Prt1"). I've handled this with an OR statement. I'd
like a more robust solution which handles all the possible upper/lower case
combinations of prt1. Thanks in advance!

Here is my code:

Private Sub UserForm_Initialize()
Dim nme As Name

With lstP1
For Each nme In ActiveWorkbook.Names
If Left(nme.Name, 4) = "Prt1" Or Left(nme.Name, 4) = "prt1" Then
.AddItem nme.Name
End If
Next nme
End With
 
B

Bill Martin

CinqueTerra said:
As part of a Print Macro, I am populating a list box with all range names
beginning with "prt1". In testing my macro, I noted that it was case
sensitive ("prt1 vs Prt1"). I've handled this with an OR statement. I'd
like a more robust solution which handles all the possible upper/lower case
combinations of prt1. Thanks in advance!

Here is my code:

Private Sub UserForm_Initialize()
Dim nme As Name

With lstP1
For Each nme In ActiveWorkbook.Names
If Left(nme.Name, 4) = "Prt1" Or Left(nme.Name, 4) = "prt1" Then
.AddItem nme.Name
End If
Next nme
End With


How about:

If ucase(Left(nme.Name, 4)) = "PRT1" Then

Bill
 
D

Dave Peterson

Another option is to put:

Option Compare Text

At the top of the module.

If you have lots of code with lots of comparisons (and case really shouldn't
matter), it might be the easiest fix.
 

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