Consulting

Results 1 to 5 of 5

Thread: advanced find and replace loop

  1. #1
    VBAX Regular
    Joined
    Apr 2008
    Posts
    6
    Location

    advanced find and replace loop

    I have an excel sheet with a code name in column B and a generic name in column C. I need to search for "vdk" within column B, and if present, change column C to "vodka". Here's what I have so far:

    [VBA]Sub FindAndReplace()


    Dim VdkCondition As String
    Dim CommonCodeCell As String
    Dim ClassCellRow As String
    Dim ClassCell As String

    Columns("B:B").Select
    VdkCondition = Selection.FindNext(What:="vdk", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False).Activate


    CommonCodeCell = ActiveCell.Address
    ClassCellRow = Mid(CommonCodeCell, 4, 10)
    ClassCell = "$C$" & ClassCellRow

    If VdkCondition = True Then

    Range(ClassCell).Select
    ActiveCell.FormulaR1C1 = "Vodka"

    End If


    End Sub
    [/VBA]

    I am having trouble with the looping. This code will find the first instance, but not continue after that. There are almost 30,000 entries. I've tried to use findnext, but it's not working too well. Any help would be most appreciated. The file is huge, but I can load it up if that helps at all. Thanks!

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Eh, there is no looping in that code, unless I'm missing something.

    Why don't you actually use the Replace method?

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You have to do a Find, and then if you find something,m runa a lopp[ with FindNext.

    There is a very clear example in VBA help.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Regular
    Joined
    Apr 2008
    Posts
    6
    Location
    found it, thanks a lot!

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Eddie,
    If your question is solved, please mark it so using the thread tools drop down.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •