PDA

View Full Version : [SOLVED:] Selecting numeric data in range if they start with 2 specified numbers



Palon
11-16-2014, 12:01 PM
Hi all,

I want to be able to select all the cells in a single column which contain string of numbers - if the first two numbers correspond to criteria provided eg. in the column below I would like to have cells with values beginning with 81 selected.

8144751
5202847
5401927
8100898
8922131
2700299

I have tried recording a macro using the filter option but that works only if I am only dealing with the same set of data. For me it's different every day so I need a versatile and simple piece of code.

Using excel 2010.

Help is much appreciated. Thank you :-)

GarysStudent
11-16-2014, 12:35 PM
Give this a try:


Sub Selecting81()
Dim r As Range, rr As Range
For Each r In Intersect(ActiveSheet.UsedRange, Range("A:A"))
v = r.Text
If Left(v, 2) = "81" Then
If rr Is Nothing Then
Set rr = r
Else
Set rr = Union(rr, r)
End If
End If
Next r
If rr Is Nothing Then
Else
rr.Select
End If
End Sub

Palon
11-16-2014, 12:51 PM
This works just great.

You Sir are awesome!

Thank you - that just made my life a lot easier :)