View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Range - selecting multiple cells

This will fail if the string gets too big. Only a concern if there are a
lot of discontiguous cells.

--
Regards,
Tom Ogilvy


"BrianB" wrote in message
om...
This code should do what you want:-
'---------------------------------------------------------
Sub findtheperson()
Dim WS As Worksheet
Dim sname As String
Dim Cnt As Long
Dim RangeList As String
'--------------------------------------
Set WS = Worksheets("Sheet1")
Cnt = 0
RangeList = ""
sname = LCase(InputBox("Enter the name to Count: "))
'-
With WS.Cells
Set c = .Find(sname, LookIn:=xlValues)
If Not c Is Nothing Then
firstaddress = c.Address
Do
Cnt = Cnt + 1
RangeList = RangeList & c.Address & ","
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address < firstaddress
End If
RangeList = Left(RangeList, Len(RangeList) - 1)
ActiveSheet.Range(RangeList).Select
MsgBox "The Name selected: " & sname & ", Name Count: " & Cnt
End With
End Sub
'-------------------------------------------------------------------





"gr8guy" wrote in message

...
Hi,

does anybody know through VBA how to select mutliple cells in different
columns & rows together. the manual method is to press cntrl key & click

the
cells.

the code i have written asks for a text input to search similar

text(names)
spread in different columns & rows in a worksheet & gives the count for

the
no. of names found.

sub findtheperson()
dim WS as worksheet
set WS=Worksheets("Sheet1")
dim sname as string
dim Cnt as Long
Cnt=0

With WS
sname=LCase(Inputbox("Enter the name to Count: "))
With WS.Range("a1:iv65536")
set c = .Find(sname,Lookin:=xlValues)
If Not c Is Nothing Then
firstaddress=c.address
Do
Cnt=Cnt+1
' Cells.SpecialCells(xlCellTypeConstants).Select
Set c= .FindNext(c)
Loop While Not c Is Nothing And c.address <

firstaddress
End If
MsgBox "The Name selected: "& sname &", Name Count: " & Cnt
End With
End With
End sub

This just finds the count of the no of occurences of the similar names

in
the worksheet, but does not do a multi-select. Can use ColorIndex

property
to color code the found names, but donot want to do that, instead do a
multi-select. Cant use SpecialCells property as it selects all names on

the
sheet & also cannot use Union Method as it requires the more than 1

ranges
to be defined. like mymultiRange=Union(Range1,Range2,....)

Any ideas?????????

Rgds,

Eijaz