Results 1 to 6 of 6

Optimizing Excel macro to find/replace with the list of data contained in the macro

Threaded View

  1. #1
    Registered User
    Join Date
    11-19-2006
    Posts
    5

    Optimizing Excel macro to find/replace with the list of data contained in the macro

    -Outlook 2003-

    Hi all,
    I have a macro stored in personal.xls. Someone usually sends me an excel list with a different number of users and they ask me if any of my users are in the list. The code runs the whole sheet, copies and pastes values all the data, searches for each of the names listed, and if found, replaces the value with the same value and colors the text. I have a macro that runs now but I would like to make it more efficient if possible:

    ' Sub routine, copies and pastes special to ensure formulas aren't searched.
       
        Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A1").Select
    
    '
        Cells.Select
        With Application.ReplaceFormat.Font
            .FontStyle = "Bold"
            .Subscript = False
            .ColorIndex = 4
        End With
        Selection.Replace What:="UserName1", Replacement:="UserName1", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=True
            
        Selection.Replace What:="UserName2", Replacement:="UserName2", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=True
    
    ... (repeated for each user name)
    
    Cells.Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Range("A1").Select
    
    '
        Cells.Select
        With Application.ReplaceFormat.Font
            .FontStyle = "Bold"
            .Subscript = False
            .ColorIndex = 3
        End With
        Selection.Replace What:="First Last1", Replacement:="First Last1", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=True
    
        Selection.Replace What:="First Last2", Replacement:="First Last2", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=True
    
    ... (repeated for each first and last name)
    
    End Sub

    The issues that I see that could happen:
    -Currently there are 28 people which means the above code is repeated about 50 times. More people may get added to my list which will make the code even longer and it doesnt seem very efficient
    -I also may want to change the search so if someone sends me a list with last name,first name or last name only, the cell will show up. But that would mean copying and pasting the text another 30-60 times

    Is there any way to combine the data? Please let me know if I can make this any clearer.
    This was also cross posted on the mrexcel forums:
    http://www.mrexcel.com/forum/showthread.php?t=359526
    Last edited by Iceyburnz; 12-17-2008 at 03:26 PM. Reason: Added version of Outlook

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1