+ Reply to Thread
Results 1 to 6 of 6

Find/Replace Question

  1. #1
    Registered User
    Join Date
    03-07-2017
    Location
    Brazil
    MS-Off Ver
    2010
    Posts
    7

    Find/Replace Question

    Hello,

    Please help me with the following code, I canīt make it run properly.

    On column B, I need to find all the cells which have the word "SALAME" in it. Then, in all its correspondent rows, I need to replace the word "AURORA ALIM." to the word "AURORA", which will be located on the column D.


    Sub FindReplace()

    Dim sht As Worksheet
    Dim fnd3 As Variant
    Dim rplc3 As Variant
    Dim cat As String

    cat = Range(B).Value
    fnd3 = "AURORA ALIM."
    rplc3 = "AURORA"

    If cat = "SALAME" Then

    For Each Row In ActiveSheet
    sht.Cells.Replace what:=fnd3, Replacement:=rplc3, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
    Next

    End If

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,906

    Re: Find/Replace Question

    Let's start with this.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Find/Replace Question

    I would start with something small that you know works and build from there. Make sure when you post code, enclose it in code blocks b/c I'm not sure if I'm allowed to respond... depends on the forum (but they all have code blocks people prefer to use).

    Below, I didn't pay much attention to the Replace Method's arguments, but I restructured the code to loop through the entire column B.
    Please Login or Register  to view this content.
    ...no debugging has been done to the above code.

    Note: Re-Edited about 10 min after original post to execute your replacement method if the row in column B contains (rather than =) the string stored in cat.
    Last edited by joe31623; 03-20-2017 at 03:30 PM.
    <---If my answer helped, please click *

  4. #4
    Registered User
    Join Date
    03-07-2017
    Location
    Brazil
    MS-Off Ver
    2010
    Posts
    7

    Re: Find/Replace Question

    Thank you bakerman2!!!

    It worked well

    Could you please explain me the code? I couldnīt understand well the Counter function..also why didnīt you use "End if"?

  5. #5
    Registered User
    Join Date
    03-07-2017
    Location
    Brazil
    MS-Off Ver
    2010
    Posts
    7

    Re: Find/Replace Question

    Thank you joe31623! You helped a lot

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,906

    Re: Find/Replace Question

    Counter is just a variable that holds the rownumber and it increments with 1 on every loop.
    Please Login or Register  to view this content.
    at first loop counter starts at 2
    cells(counter,2) = range("B2")
    cells(counter,4) = range("D2")

    next loop counter is 3
    cells(counter,2) = range("B3")
    cells(counter,4) = range("D3")

    and so one....until all filled cells in column B are passed.

    No need to use end if if all statements are on 1 codeline.
    Could also be written as

    Please Login or Register  to view this content.
    PS: Thanks for rep+.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Find and replace Question
    By Canoga56 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 12-05-2014, 01:11 AM
  2. [SOLVED] Find, Replace question
    By JO505 in forum Excel General
    Replies: 23
    Last Post: 09-20-2014, 08:47 PM
  3. Find & Replace Question
    By johnysexcel in forum Excel General
    Replies: 5
    Last Post: 12-29-2010, 02:34 PM
  4. Find & replace question.....I believe
    By anorton in forum Excel General
    Replies: 13
    Last Post: 06-04-2006, 08:30 AM
  5. Find and replace question
    By devens8765 in forum Excel General
    Replies: 8
    Last Post: 11-10-2005, 08:34 AM
  6. Find & Replace question
    By Stuart in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-28-2005, 03:06 PM
  7. [SOLVED] Find/Replace Question
    By Littlebear in forum Excel General
    Replies: 3
    Last Post: 02-08-2005, 12:06 PM

Tags for this Thread

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