+ Reply to Thread
Results 1 to 13 of 13

Find & Replace macro

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Find & Replace macro

    Hi All. This is my first post here, but I have been browsing your forum for a while now and have found it a very useful resource, so thanks for your (unknowing) help so far.

    I have a problem where I want to search column K in a spreadsheet, for a value in cell B2 on a second spreadsheet, and replace it with the value in C2 of the second spreadsheet. I then want the macro to move down to B3 in the second spreadsheet, but replace it with C3, and so on until all values in the second spreadsheet have been found/replaced.

    So far I have the code to find and replace C2.s value with C3's, but I'm not sure how to make this move down to run therough the other values.

    Any help would be greatly appreciated.

    Code so far (only for working on one spreadsheet so far):

    
    Sub MassReplace()
    
    Range("K:K").Select
       Selection.Replace what:=Range("B2").Value, _
           replacement:=Range("C2").Value, _
           LookAt:=xlPart, _
           SearchOrder:=xlByRows, _
           MatchCase:=False
           
    End Sub
    Thanks

    Sun 13
    Last edited by Sun13Banjo; 07-19-2012 at 09:46 AM. Reason: spelling!

  2. #2
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    A simple loop or with function would help you.

    do until activecell.value = ""
    
    replace code
    
    activecell.offset(1,0).select
    
    loop

  3. #3
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    Thanks for your reply. The loop doesn't work as the B2/C2 references are specific, and I need the Find and Replace to move down to B3/C3 and so on until the active cell is blank

    Sun 13

  4. #4
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    send a sample sheet and ill set it up

  5. #5
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    Thanks GaidenFocus. I can't send any files from work

    I will upload from home later.

    Thanksfor trying tho - there's probably a really simple solution that I'm missing

  6. #6
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    oh i got it haha

    are you familiar with range variables?

    dim tmpbox as range
    
    
    Sub MassReplace()
    set tmpbox as range("b2")
    
    do until tmpbox.value = ""
    Range("K:K").Select
       Selection.Replace what:=tmpbox, _
           replacement:=tmpbox.offset(0,1), _
           LookAt:=xlPart, _
           SearchOrder:=xlByRows, _
           MatchCase:=False
    
    set tmpbox = tmpbox.offset(1,0)
    loop     
     
    End Sub

  7. #7
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    The quick answer would be no

    I have tried your code and am getting a compile error:syntax error on the line 'set tmpbox as range("b2")'

  8. #8
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    sorry change as to =

  9. #9
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    dim tmpbox as range
    
    
    Sub MassReplace()
    set tmpbox = range("b2")
    
    do until tmpbox.value = ""
    Range("K:K").Select
       Selection.Replace what:=tmpbox, _
           replacement:=tmpbox.offset(0,1), _
           LookAt:=xlPart, _
           SearchOrder:=xlByRows, _
           MatchCase:=False
    
    set tmpbox = tmpbox.offset(1,0)
    loop     
     
    End Sub

  10. #10
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    On second thoughts it works perfectly!

    Many, many thanks for this. I may well be carried shoulder high by my colleagues in honour of my having saved them hours of work.

    I will, of course, take all the credit, but deep down we'll all know it was you!

    Thanks again
    Last edited by Sun13Banjo; 07-19-2012 at 11:02 AM. Reason: Can't spell 'thanks' properly

  11. #11
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    Thanks again GaidenFocus.

    The code is not actually replacing any text, altho for the life of me I can't figure out why

  12. #12
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Find & Replace macro

    Haha, no worries, I'm sure everyone here has done that at some point. I've actually gotten a raise from some of the things these guys helped me with. Just give me some rep! I'm negative for some reason. Take care.

  13. #13
    Registered User
    Join Date
    06-08-2012
    Location
    Liverpool
    MS-Off Ver
    Office 365
    Posts
    84

    Re: Find & Replace macro

    Rep given, and well deserved!

+ 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