+ Reply to Thread
Results 1 to 7 of 7

Run a search and replace based on what is in another Cell?

Hybrid View

  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    waco, texas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Run a search and replace based on what is in another Cell?

    Basically I have two sheets.

    Sheet 1 changes monthly. It has a list of names in column A and their associated emails in column B. New Names and Emails are added and taken off and existing emails change.
    Sheet 2 comes in daily and has just the list of names.

    I need to figure out a macro that will look at sheet 1, look at a cell in column A, take the name that is in that cell.
    Search for that name in Sheet 2, then replace that name in sheet 2 with the email(column B) from sheet 1 associated with that name.


    It's quite all right if I need to merge the two sheets to accomplish this, but it would be preferable to keep them separate.
    Last edited by cekenney; 09-28-2012 at 12:20 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: Run a search and replace based on what is in another Cell?

    Hello there,

    When do you want this action to take place? On change of a cell in worksheet 1, on add new worksheet?, on button press?

    Thanks!

  3. #3
    Registered User
    Join Date
    09-28-2012
    Location
    waco, texas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Run a search and replace based on what is in another Cell?

    Really just when the macro is run. So on a shortcut key press or a button would work as well.

  4. #4
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Run a search and replace based on what is in another Cell?

    very bad code, but it works ^^

    Sub copyto()
    
    On Error Resume Next
    
    Dim Cell As Range, Ncell As String, dest As Range
    
    Workbooks("From").Activate
    
    For Each Cell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
    
        Cell.Activate
        
            Cell.Offset(0, 1).Copy
        
            Ncell = Cell.Value
            
            Workbooks("pastto").Activate
        
            Set dest = Cells.Find(What:=Ncell, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
             
            dest.PasteSpecial xlPasteValues
            
            Workbooks("From").Activate
            
                
        Next Cell
    
    End Sub
    Last edited by Bishonen; 09-28-2012 at 02:54 PM.

  5. #5
    Registered User
    Join Date
    09-28-2012
    Location
    waco, texas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Run a search and replace based on what is in another Cell?

    Quote Originally Posted by Bishonen View Post
    very bad code, but it works ^^

    Sub copyto()
    
    On Error Resume Next
    
    Dim Cell As Range, Ncell As String, dest As Range
    
    Workbooks("From").Activate
    
    For Each Cell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
    
        Cell.Activate
        
            Cell.Offset(0, 1).Copy
        
            Ncell = Cell.Value
            
            Workbooks("pastto").Activate
        
            Set dest = Cells.Find(What:=Ncell, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
             
            dest.PasteSpecial xlPasteValues
            
            Workbooks("From").Activate
            
                
        Next Cell
    
    End Sub
    How would I change this if I simply wanted to do it from 2 different sheets within the same workbook?

  6. #6
    Valued Forum Contributor
    Join Date
    03-17-2012
    Location
    Warsaw, Poland
    MS-Off Ver
    2007/2010
    Posts
    555

    Re: Run a search and replace based on what is in another Cell?

    Quote Originally Posted by cekenney View Post
    How would I change this if I simply wanted to do it from 2 different sheets within the same workbook?
    Sub copyto()
    
    On Error Resume Next
    
    Dim Cell As Range, Ncell As String, dest As Range
    
    Worksheets(1).Activate 'chose your "from" worksheet
    
    For Each Cell In Range("A1", Cells(Rows.Count, 1).End(xlUp))
    
        Cell.Activate
        
            Cell.Offset(0, 1).Copy
        
            Ncell = Cell.Value
            
            Worksheets(2).Activate 'chose your destination worksheet
        
            Set dest = Cells.Find(What:=Ncell, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False)
             
            dest.PasteSpecial xlPasteValues
            
            Worksheets(1).Activate 'chose your "from" worksheet again
            
                
        Next Cell
    
    End Sub

  7. #7
    Registered User
    Join Date
    09-28-2012
    Location
    waco, texas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Run a search and replace based on what is in another Cell?

    wow! worked like a charm thanks so much!

+ Reply to Thread

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