dee

  • Thread starter Thread starter dee2417
  • Start date Start date
D

dee2417

hi
I want to find some data in excel sheet and want to change the selecte
data into a different color how to do it with a macro

Thanx in advance
de
 
Here is the help example for the FIND method:

This example finds all cells in the range A1:A500 that contain the value 2
and makes those cells gray.

With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
 
Tom.
This code finds and highlights all cells that include "2" anywhere within
the cell, like "1234". But what if you wanted only cells with the value
"2".
Tks,


Tom Ogilvy said:
Here is the help example for the FIND method:

This example finds all cells in the range A1:A500 that contain the value 2
and makes those cells gray.

With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
 
..Find is one of those methods that remember the settings the last time it was
used--either via code or via the userinterface.

If you look at the help for .find, you'll see all the options that can be
specified. (This is from xl2003.)


expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection,
MatchCase, MatchByte, SearchFormat)

It may be better to specify all the options that are available than to rely on
having the settings the way you want.

(Tom copied the example from the help -- which is less than robust.)

Jim said:
Tom.
This code finds and highlights all cells that include "2" anywhere within
the cell, like "1234". But what if you wanted only cells with the value
"2".
Tks,
 
Just to add:
It is robust enough, but less than comprehensive; however it is always
readily available as a consult for a generalized algorithm. It does have
some warts, for example the loop termination conditions, not setting args.
As Dave said, it is always best to include the settings you want FIND to
use.
 
Also, notices that if my range is formatted numbers (comma, 2) the Macro
yields nothing;
If I change formatting the General (Control+Shift+~) macro paints my four
2's. Find method must be sensitive to formatting,,, hummmm

My Code:
Sub Foo()
With Worksheets(1).Range("a1:a15")
Set c = .Find(2, Lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 6
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub




Tom Ogilvy said:
Just to add:
It is robust enough, but less than comprehensive; however it is always
readily available as a consult for a generalized algorithm. It does have
some warts, for example the loop termination conditions, not setting args.
As Dave said, it is always best to include the settings you want FIND to
use.

--
Regards,
Tom Ogilvy


Dave Peterson said:
.Find is one of those methods that remember the settings the last time
it
was
used--either via code or via the userinterface.

If you look at the help for .find, you'll see all the options that can be
specified. (This is from xl2003.)


expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection,
MatchCase, MatchByte, SearchFormat)

It may be better to specify all the options that are available than to rely on
having the settings the way you want.

(Tom copied the example from the help -- which is less than robust.)

Jim said:
Tom.
This code finds and highlights all cells that include "2" anywhere within
the cell, like "1234". But what if you wanted only cells with the value
"2".
Tks,

Here is the help example for the FIND method:

This example finds all cells in the range A1:A500 that contain the value 2
and makes those cells gray.

With Worksheets(1).Range("a1:a500")
Set c = .Find(2, lookin:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.Pattern = xlPatternGray50
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
 
May not be as fast but try this.

Sub find2()
For Each c In Sheets("sheet11").Range("a1:a15")
If c = 2 Then c.Interior.ColorIndex = 6
Next c
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)
Jim May said:
Also, notices that if my range is formatted numbers (comma, 2) the Macro
yields nothing;
If I change formatting the General (Control+Shift+~) macro paints my four
2's. Find method must be sensitive to formatting,,, hummmm

My Code:
Sub Foo()
With Worksheets(1).Range("a1:a15")
Set c = .Find(2, Lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 6
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub




Tom Ogilvy said:
Just to add:
It is robust enough, but less than comprehensive; however it is always
readily available as a consult for a generalized algorithm. It does have
some warts, for example the loop termination conditions, not setting args.
As Dave said, it is always best to include the settings you want FIND to
use.

--
Regards,
Tom Ogilvy


Dave Peterson said:
.Find is one of those methods that remember the settings the last time
it
 
OR
Sub Fooo()
With Sheet11.Range("a1:a15")
.NumberFormat = "General"
Set c = .Find(2, lookat:=xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 6
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
.Style = "Comma"
End With
End Sub

--
Don Guillett
SalesAid Software
(e-mail address removed)
Jim May said:
Also, notices that if my range is formatted numbers (comma, 2) the Macro
yields nothing;
If I change formatting the General (Control+Shift+~) macro paints my four
2's. Find method must be sensitive to formatting,,, hummmm

My Code:
Sub Foo()
With Worksheets(1).Range("a1:a15")
Set c = .Find(2, Lookat:=xlWhole, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 6
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub




Tom Ogilvy said:
Just to add:
It is robust enough, but less than comprehensive; however it is always
readily available as a consult for a generalized algorithm. It does have
some warts, for example the loop termination conditions, not setting args.
As Dave said, it is always best to include the settings you want FIND to
use.

--
Regards,
Tom Ogilvy


Dave Peterson said:
.Find is one of those methods that remember the settings the last time
it
 
tks Don;

Don Guillett said:
OR
Sub Fooo()
With Sheet11.Range("a1:a15")
.NumberFormat = "General"
Set c = .Find(2, lookat:=xlWhole)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Interior.ColorIndex = 6
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
.Style = "Comma"
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top