String comparison

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,835
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Having a problem dealing with sameness including casing with strings

Album is "LIVE!"
ThisRow!Title is "Live"
My first check fails
VBA Code:
If StrComp(Album, UCase(ThisRow!Title), vbBinaryCompare) = 0 Then
so then tried
VBA Code:
? Album <> UCase(ThisRow!Title)& "!"
But this failed too. Just wondering how others would handle this.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I am not sure of your variables, so you may have to reverse the middle two arguments. I am also not sure what is a variable and what is text, so I guessed. With that said, maybe...

If InStr(1, Album, "ThisRow!Title", vbTextCompare) = 0 Then
 
Upvote 0
Hi Rick, can't explain why this is confusing me so much!
Both are strings.
Album should be an upper cased ThisRowITitle.
So
VBA Code:
Album= Ucase(ThisRow!title)&"!"
returns True which is correct.
But if Album = "Live!" it also returns True, which is incorrect.
Instr or StrComp muddy things more as all of the text isn't compared.
 
Upvote 0
Text strings must be encased in quote marks, but if both of what you are testing are strings, then your code line makes no sense. Explain to us exactly what text you have (the complete text) and what text you want to search for within it.
 
Upvote 0
Dim Album As String
Dim ThisRow As DAO.Recordset
Title is a string Field in recordset

They contain:
ThisRow!Title = "Live"
Album = "LIVE!"

The test is to see if Album and ThisRow!Title are the same but additionally Album is Uppercased.
Maybe two clauses are needed, or there could be some way with one I haven't thoight of ?

thisrow!title = Album needs to be true. Then Album must be uppercase. That's what I'm having trouble with.
Is there a way to confirm some text is all uppercased, or not ? I tried binarycompare and textcompare but both are unreliable, or does it only compare the first character?
For these tests the exclamantion mark is removed.
? strcomp("LIVE","Live",vbBinaryCompare)= True
True
? strcomp("LIve","Live",vbBinaryCompare)= True
True
? strcomp("LIVE","Live",vbTextCompare)= True
False
? strcomp("LIve","Live",vbTextCompare)= True
False
 
Upvote 0
If Album is a String variable which does not contain spaces...
VBA Code:
If Not Album Like "*[!A-Z]*" Then
  ' Album contains all upper case letters
Else
  ' It doesn't
End IF

If Album's text could contain spaces, then use this instead...
VBA Code:
If Not Album Like "*[!A-Z ]*" Then
  ' Album contains all upper case letters
Else
  ' It doesn't
End IF
 
Upvote 0
I found this on Google

'To check if a value is all uppercase, you can do so easily by checking if the value is equal to the same uppercase value
(by using the UCase function). In this example the "HelLO" value is not all caps, so it fails the my_value = UCase(my_value) test.


? "HelLO" = Ucase("HelLO")
True
If that should fail, wouldn't it return False?
I found the "Not Album Like" routine ran into trouble if numbers or other chars were used.
I ended up with this, even though it seems overkill somehow.
If ThisRow!Title = Album And ConfirmUcase(album) = True Then
Else
VBA Code:
Private Function ConfirmUcase(Dat As String) As Boolean
    Dim i As Integer
    For i = 1 To Len(Dat)
        Select Case Asc(Mid(Dat, i, 1))
            Case 97 To 122
                Exit Function
        End Select
    Next
    If Len(Dat) > 0 Then ConfirmUcase = True
End Function
 
Upvote 0
I found the "Not Album Like" routine ran into trouble if numbers or other chars were used.
Your original message was not entirely clear on what text you are testing. Is your only concern that there not be a lower case letter in the text (all other characters permitted)? If so...
VBA Code:
If Album Like "*[a-z]*" Then
   ' There is at least one lower case letter in the text
Else
  ' All letters in the text are upper case
End If

As to your last question, yes, something like this should also work...
VBA Code:
If Album = UCase(Album) Then
  ' All letters in the text are upper case
Else
  ' They are not
End If
 
Upvote 0
Gotcha! Album Like "*[a-z]*" = False would do it. Thanks.

But can you tell me if album = UCase(album) = true (and Album *is* upper case
how can
"HelLO" = Ucase("HelLO")
also be true when a lower case e and l exist ?
 
Upvote 0

Forum statistics

Threads
1,216,102
Messages
6,128,852
Members
449,471
Latest member
lachbee

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