Search for number and degree symbol

0 votes

I have a worksheet with a lot of other information as well as a section where elbows need to be categorized according to their degrees. I employ VBA. I can't just search for 45, 90, etc. using a Like or InStr function because it returns other content. To obtain the degree symbol and the number, I used the formula below, although it only returns values if both appear in the cell, not necessarily next to one another. Therefore, it causes problems if I enter data in the cell that reads "Qty 90 of 45° elbows" and "Qty 45 of 90° elbows."

For Each cell In Range("A1:A5000")
If InStr(cell.Value, ChrW(176)) > 0 And cell Like "*45*" Then
    cell.Offset(0,5).Value = "45 Degree"
End If
Next cell

I figure I can do a workaround and try to extract info from the cell, enter it into another one, and then search on that. But that's a pain. Any ideas?

Dec 28, 2022 in Others by Kithuzzz
• 38,000 points
450 views

1 answer to this question.

0 votes

The Instr and Like operators are not mutually excluding which means you are getting "false positives". Why not just a single Like a statement:

Sub Test()

Dim arr As Variant: arr = Array("Qty 90 of 45° elbows", "Qty 45 of 90° elbows", "Qty 90 of 145° elbows")

'Using Like expressions:
For Each el In arr
    Debug.Print el Like "*45°*"
Next

'Using Regular expressions:
With CreateObject("vbscript.regexp")
    .Pattern = "(?:^|\D)45°(?:$|\D)"
    For Each el In arr
        Debug.Print .Test(el)
    Next
End With

End Sub
answered Dec 28, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer
0 votes
0 answers

Replacing H1 text with a logo image: best method for SEO and accessibility?

i want to link my logo to ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
2,955 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,211 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,662 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
906 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,053 views
0 votes
1 answer

AngularJS SEO - Once and for all

java script cant be or wont be ...READ MORE

answered Feb 14, 2022 in Others by narikkadan
• 63,600 points
639 views
0 votes
1 answer

AngularJS SEO - Once and for all

javascript is not compiled by google bots ...READ MORE

answered Feb 20, 2022 in Others by narikkadan
• 63,600 points
584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP