Searching for a number

goofy78270

Well-known Member
Joined
May 16, 2007
Messages
555
I am looking to search quickly for a number within a cell.

Using a macro, I am looking to search something like:

1,2,3,4-12,45,67,200

to see if the number 11 is contained.

I have recently wrote a macro that splits out the field into an array based on the "," and then again on the "-" while filling in the missing numbers but this is pretty time consuming. Is there an easier way to simply search for the number in question?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
How about INSTR? It seemed to work when I tried it using your example...

Code:
If InStr(Range("A1"), "11") = 0 Then
MsgBox "No match."
Else
MsgBox "Found."
End If
 
Upvote 0
I think the op is trying to find the 11 in 4-12, as this number range contains 4, 5, 6, 7, 8, 9, 10, 11, and 12.
 
Upvote 0
Perhaps:
Code:
Public Function FindInRange(r As String, srchNum As Long) As Boolean
Dim t, u, v
t = Split(r, ",")
For Each v In t
    u = Split(v, "-")
        If UBound(u) = 0 Then
                If u(0) = srchNum Then
                    FindInRange = True
                    Exit For
                End If
            Else
                If srchNum >= Val(u(0)) And Val(u(1)) >= srchNum Then
                    FindInRange = True
                    Exit For
                End If
        End If
Next
End Function


Usage is =FindInRange(cell, number to look for)
Book1
ABCD
11,2,3,4-12,45,67,200TRUE
21, 3-10,47FALSE
Sheet1
 
Upvote 0

Forum statistics

Threads
1,217,328
Messages
6,135,917
Members
449,972
Latest member
Natejack

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