LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Want to select cells in B1:B19 which have same value as in cell A1

Hi AL007,

Why macro below is not working - can anybody provide an alternative


Two reasons:

(1) Select is a reserved word and should not be used as a procedure name
(2) Errors my code

Try, therefore, this revision:

'=============
Public Sub SelectThem()
Dim rng As Range
Dim rng2 As Range
Dim rngFound As Range
Dim rngOut As Range
Dim sStr As String
Dim firstAdd As String

Set rng = Range("A1")
Set rng2 = Range("B1:B19")
sStr = rng.Value

Set rngFound = rng2.Find _
(What:=sStr, _
After:=rng2(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext)

If Not rngFound Is Nothing Then
firstAdd = rngFound.Address
Set rngOut = rngFound

Do
Set rngFound = Range("B2:B19").FindNext(rngFound)
If Not rngFound Is Nothing Then
Set rngOut = Union(rngOut, rngFound)
End If
Loop While Not rngFound Is Nothing _
And rngFound.Address < firstAdd
End If

If Not rngOut Is Nothing Then rngOut.Select

End Sub
'<<=============

BTW, it is more efficient, polite, and in accordance with netiquette, to
remain within the original thread.

---
Regards,
Norman



"al007" wrote in message
oups.com...
Want to select cells in B1:B19 which have same value as in cell A1
Why macro below is not working - can anybody provide an alternative



Public Sub Select()
Dim rng As Range
Dim rngFound As Range
Dim rngOut As Range
Dim sStr As String
Dim firstAdd As String

Set rng = Range("A1")
sStr = rng.Value

Set rngFound = Range("B1:B19").Find _
(What:=sStr, _
After:=rng(1), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext)

If Not rngFound Is Nothing Then
firstAdd = rngFound.Address
Set rngOut = rngFound
End If

Do
Set rngFound = Range("B2:B19").FindNext(rngFound)
If Not rngFound Is Nothing Then
Set rngOut = Union(rngOut, rngFound)
End If
Loop While Not rngFound Is Nothing _
And rngFound.Address < firstAdd

If Not rngOut Is Nothing Then rngOut.Select

End Sub



 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
when I select one cell, why are 5 more cells selected below Mrnetwurm Excel Discussion (Misc queries) 3 August 19th 09 11:15 PM
Up down arrow keys do not select cells if select locked cells unch roandrob Excel Discussion (Misc queries) 3 May 18th 09 12:48 AM
Select max value from 9 cells, copy cell col heading to other cell Struggling in Sheffield[_2_] New Users to Excel 6 March 11th 09 11:47 AM
Using formulas to select cells (Ex: Select every nth cell in a col Lakeview Photographic Services Excel Discussion (Misc queries) 2 March 15th 07 02:17 PM
if a cell is 0, how do I select a range of cells? Angela0212 Excel Programming 3 July 24th 04 12:44 AM


All times are GMT +1. The time now is 04:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"