+ Reply to Thread
Results 1 to 6 of 6

Dynamic Find and Replace

Hybrid View

  1. #1
    Forum Contributor TheRetroChief's Avatar
    Join Date
    09-30-2008
    Location
    UK
    MS-Off Ver
    2019
    Posts
    136

    Dynamic Find and Replace

    Greetings all,

    Is there a way in which you can use two cells (a2 & b2 for example) with a button to trigger a macro which will search for a2 and replace with b2 over a certain range of cells? unless there is a function to do it of course, which i would prefer!!

    Ta muchly,
    Ali Mac
    Last edited by TheRetroChief; 12-15-2008 at 07:29 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Sure is; you have two simple options -

    1. take a look at the Find function; this is directly out of the online help:
    With Worksheets(1).Range("a1:a500")
        Set c = .Find(2, lookin:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                c.Value = 5
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
    End With
    Obviously, you'd just make the "2" the value of A2 and "5" the value of B2 in your example.

    2. use the Replace function:
        Cells.Replace What:="asdf", Replacement:="qwerty", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    Hope that helps.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Forum Contributor TheRetroChief's Avatar
    Join Date
    09-30-2008
    Location
    UK
    MS-Off Ver
    2019
    Posts
    136
    Ah, but i want A2 and B2 to change without having to change the macro. Will this accept that? i.e. 1 is "A2" and 2 is "B2"???

  4. #4
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717
    Try this code

    Sub find_and_replace()
    
        Selection.Replace What:=Range("A2"), Replacement:=Range("B2"), LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False
    End Sub
    Note... First select the range where u want to find and replace and then run the macros..

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Thanks Shijesh So Retro - the answer to your question is "yes"; just replace my "asdf" and "qwerty" with the range values as Shijesh has shown below.

  6. #6
    Forum Contributor TheRetroChief's Avatar
    Join Date
    09-30-2008
    Location
    UK
    MS-Off Ver
    2019
    Posts
    136
    Cheers guys.

+ 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