PDA

View Full Version : Find / Replace formatting?



lijon
09-10-2008, 10:03 AM
Hi folks... I'm trying to write a sub to do the following:
For each name in this B-column, try and find it in the G-column. If it's there, reformat the cell in G-column to be like B-column.

...but I'm stuck. Can anyone tell me where I'm coming up short?
Here's what I've got so far:

Dim names As Integer
Dim namey As String

Range("b7").Select
Selection.End(xlDown).Select
erow = ActiveCell.Row
srow = 7
For names = srow To erow

Range("B" & names).Select
namey = Range("b" & names).Value

Range("G7:G16").Select
Selection.Find(What:=(namey), After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next names



Mucho gracias for all feedback and replies. The time is appreciated.
Lijon.

mdmackillop
09-10-2008, 10:21 AM
Hi lijon,
Welcome to VBAX.
When you post code, use the VBA button to format it as shown,

Re your question, try to avoid Selection.

Option Explicit
Sub Test()
Dim Rng As Range, cel As Range, c As Range

Set Rng = Range(Cells(7, 2), Cells(7, 2).End(xlDown))

For Each cel In Rng
Set c = Range("G7:G16").Find(What:=cel, LookAt:=xlWhole)
If Not c Is Nothing Then
cel.Copy
c.PasteSpecial Paste:=xlPasteFormats
End If
Next
End Sub

mdmackillop
09-10-2008, 10:25 AM
Fixing your own code:
Sub Test2()
Dim names As Integer
Dim namey As String

Range("b7").Select
Selection.End(xlDown).Select
erow = ActiveCell.Row
srow = 7
For names = srow To erow

Range("B" & names).Select
namey = Range("b" & names).Value

Range("G7:G16").Select
Selection.Find(What:=(namey), After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Select

Range("B" & names).Copy

Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next names
End Sub

Slyboots
09-10-2008, 01:40 PM
There's actually a much easier non-macro way to do this, using Conditional Formatting.

Select column G, and then Format, Conditional Formatting from the menu. Select "Formula" from the first dropdown and enter this as the formula:

=ISNA(MATCH(G1,B:B,0))=FALSE

Each entry in column G that is ALSO contained in column B can be formatted any way you like.

S