+ Reply to Thread
Results 1 to 2 of 2

How to create a loop of the Find & Replace Function

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Puerto Rico
    MS-Off Ver
    Excel 2007
    Posts
    2

    How to create a loop of the Find & Replace Function

    Hi,

    I already have a code for the loop but is no working properly.

    I attached a test Worksheet for you to test the code.

    Test.xlsx

    The idea of the code is not to do a Find & Replace for the 369 fields.

    So the code must read the Row B on Sheet 2 and look it up in the Row A on Sheet 1, then when its finds the number in the row it must replace it with the number in row A on Sheet 2.

    Example: First step will be get the NUmber from Row B on Sheet 2 (115000059001000) and look for it in the Row A on SHeet 1. In this case the number does not repeats but is very problable that many others will. So when it finds the number then it should replace it with (100-100). Whty with this spesific number? Because is the number next to him in the Field 2.


    Here is the code that I have so far.

    Option Explicit
    Sub ReplaceOld()
    Dim lastOld, lastOld2, nxtRow
    Dim oldNum As String
    Dim c
    'Determine Last Row of Old Number List
    lastOld = Sheets(2).Range("B" & Rows.Count).End(xlUp).Row

    'Determine Last Row of "Random and Old Number" List
    lastOld2 = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row

    'Loop through Old Numbers in Column B (2)
    For nxtRow = 2 To lastOld
    oldNum = Sheets(2).Range("B" & nxtRow)

    'Find each Old number in "Random and Old Number" list
    With Sheets(2).Range("B2:B" & lastOld2)
    Set c = .Find(oldNum, lookat:=xlWhole)
    If Not c Is Nothing Then

    'When found, replace it with New number from Column A (1)
    Sheets(1).Range(c.Address) = Sheets(2).Range("A" & nxtRow)

    End If
    End With
    Next
    End Sub

    I got this code form some forum post and I edit it. But It is not working as I intend
    Hope you understand.

    Anticipated thanks

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: How to create a loop of the Find & Replace Function

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

+ 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