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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

goofy78270

Well-known Member
Joined
May 16, 2007
Messages
555
I am looking to do this in a macro and find does not seem to work for the 4-12 in finding 11.
 

marka87uk

Board Regular
Joined
Mar 24, 2007
Messages
247
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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Forum statistics

Threads
1,181,647
Messages
5,931,210
Members
436,784
Latest member
amuljono

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
Top