Help finding the next similar cell using VBA

I am working on an Excel worksheet that has the word "TOUR" listed throughout the spreadsheet. How can I find the next "TOUR" and insert rows to make sure it is in the row I want it in


Answer this question

Help finding the next similar cell using VBA

  • please help me

    Hi,

    You should look to use the Selection.Find method to perform your search.... this code should do the search and find all TOURS in the list but it doesn't do anything else.


    'select the whole range to search, in this case column A
    Columns("A:A").Select

    'the tour found, stored as a range
    Dim FoundTour As Range

    'do an inital search and find the first tour in column A
    Set FoundTour = Selection.Find(What:="TOUR", After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

    'if tour was found then
    If Not FoundTour Is Nothing Then

    'store it's address which is used to determine if the search has been completed
    Dim firstAddress As String
    firstAddress = FoundTour.Address

    Do
    'find the next tour after the current found tour
    Set FoundTour = Selection.FindNext(FoundTour)

    'continue the search until tour is no longer found or until the search reaches the starting address
    'of the first found tour
    Loop While Not FoundTour Is Nothing And FoundTour.Address <> firstAddress

    End If



  • Mikael Pahmp

         
  • LasseJ

    Thanks for the information, but I need to know how to look for Tour and verify that it is in a certain cell. If it is not in a certain cell, I need to insert rows until it is in the cell that I want.
  • Hoodwinked

    Hello

    Derek has shown you how you can find each occurrence of "TOUR". Once you have found the adress of the cell that contains "TOUR" (which the above code will do) the compare it to the adress you would like it to be and insert or delete rows as you need.

    Chas

    Or you could post more of your code and I'll see if I can suggest anything more


  • Help finding the next similar cell using VBA