Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default I want to highlight multiple cells using VB

I've looked all over and can't seem to figure out how to do this...

Cells A1:A100 have data
I want to have a button that when clicked, searches A1:A100
for the text in the current Active Cell.
Then, have it select each cell that matches.

Example: B1 has the word "blah"
B1 is selected.
A1:A100 have various words, but A5 and A8 have the word "blah"
I want to click on the button, and have A5 and A8 be selected as if
I had held CTRL and clicked on A5 and A8.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default I want to highlight multiple cells using VB

Here is one way to do it...

Sub SelectMatchingItems()
Dim R As Range
Dim SelectedItems As Range
For Each R In Range("A1:A100")
If R.Value = ActiveCell.Value And R.Value < "" Then
If SelectedItems Is Nothing Then
Set SelectedItems = R
Else
Set SelectedItems = Union(SelectedItems, R)
End If
End If
Next
If Not SelectedItems Is Nothing Then
SelectedItems.Select
End If
End Sub

Rick


"Christopher7291972" wrote in message
ps.com...
I've looked all over and can't seem to figure out how to do this...

Cells A1:A100 have data
I want to have a button that when clicked, searches A1:A100
for the text in the current Active Cell.
Then, have it select each cell that matches.

Example: B1 has the word "blah"
B1 is selected.
A1:A100 have various words, but A5 and A8 have the word "blah"
I want to click on the button, and have A5 and A8 be selected as if
I had held CTRL and clicked on A5 and A8.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 735
Default I want to highlight multiple cells using VB

Sub mySelector()
Dim myRange As Range, myCell As Range
Dim mySelection As String

Set myRange = Range("A1:A100")
mySelection = ""

For Each myCell In myRange
If ActiveCell.Value = myCell.Value Then
If Len(mySelection) < 0 Then
mySelection = mySelection & ","
End If
mySelection = mySelection & myCell.Address
End If
Next
Range(mySelection).Select
End Sub

--

Regards,
Nigel




"Christopher7291972" wrote in message
ps.com...
I've looked all over and can't seem to figure out how to do this...

Cells A1:A100 have data
I want to have a button that when clicked, searches A1:A100
for the text in the current Active Cell.
Then, have it select each cell that matches.

Example: B1 has the word "blah"
B1 is selected.
A1:A100 have various words, but A5 and A8 have the word "blah"
I want to click on the button, and have A5 and A8 be selected as if
I had held CTRL and clicked on A5 and A8.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default I want to highlight multiple cells using VB

That will fail when a match is not found because the string will be
empty, and will also fail when the string grows too long... The Union
approach is more robust.

-Basilisk96


"Nigel" wrote:
Sub mySelector()
Dim myRange As Range, myCell As Range
Dim mySelection As String

Set myRange = Range("A1:A100")
mySelection = ""

For Each myCell In myRange
If ActiveCell.Value = myCell.Value Then
If Len(mySelection) < 0 Then
mySelection = mySelection & ","
End If
mySelection = mySelection & myCell.Address
End If
Next
Range(mySelection).Select
End Sub

--

Regards,
Nigel



Reply
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
Compare multiple cells and highlight differences ez Excel Discussion (Misc queries) 5 July 10th 09 08:03 PM
multiple cells highlight instead of one Debbie60 Excel Discussion (Misc queries) 2 March 3rd 09 10:55 AM
how to highlight more related cells if cell highlight Jon Excel Discussion (Misc queries) 5 December 21st 08 01:06 PM
Highlight multiple findings in Excel Rick >. Excel Discussion (Misc queries) 4 August 15th 06 09:45 PM
Highlight cells with ctrl-click but only un-highlight one cell hagan Excel Discussion (Misc queries) 5 May 27th 05 06:45 PM


All times are GMT +1. The time now is 12:13 AM.

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

About Us

"It's about Microsoft Excel"