Consulting

Results 1 to 4 of 4

Thread: Find / Replace formatting?

  1. #1
    VBAX Regular
    Joined
    Mar 2008
    Posts
    16
    Location

    Find / Replace formatting?

    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:

    [vba] 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
    [/vba]


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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    [VBA]
    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[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Fixing your own code:
    [VBA]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
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Sep 2008
    Posts
    36
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •