+ Reply to Thread
Results 1 to 13 of 13

Thread: For Loop and search

  1. #1
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    For Loop and search

    I'm new to vba and I have a simple problem but cannot get the code to work. I have a worksheet with headers. Column D's header is "Name name". I want to search thru this column and find the text "text". When found move to the left adjacent cell (of the found "text" cell) and enter "text2". Then move on to the next cell in "Name name". Here is what I have so far. Thanks for any suggestions and help.

        Dim i As Long
        colLen = Range("Name Name" & Rows.Count).End(xlUp).Row
    
        For i = 1 To ColLen
            Cells.find(What:="text", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
                :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                False, SearchFormat:=False).Activate
            ActiveCell.Offset(0, -1).Select
            ActiveCell.Value = "text2"
        Next i
    Last edited by rigray; 07-14-2011 at 11:04 AM.

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

    Re: For Loop and search

    Hi rigray and welcome to the forum. Please take a moment and read the forum rules located here and wrap code tags around your code as per Rule #3. After you do that someone will be able to help you.
    Please leave a message after the beep!

  3. #3
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: For Loop and search

    Thanks. It should be corrected now. My apologies.

  4. #4
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: For Loop and search

    Try:
    Sub MainMacro()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim lastrow As Long
    Dim c As Range, iFind As Range
    Dim startaddress As String
    
    lastrow = ws1.Range("D" & Rows.Count).End(xlUp).Row
    
    Set iFind = ws1.Range("D2:D" & lastrow)
    With iFind
        Set c = .Find(what:="text", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext)
        If Not c Is Nothing Then
            c.Offset(0, -1).Value = "text2"
            startaddress = c.Address
            Do
                c.Offset(0, -1).Value = "text2"
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> startaddress
        End If
    End With
    End Sub

    If you need help with explanations on why stuff does what it does let me know. The .FindNext stuff can be a little confusing.

    Note: This is just showing you a way using Find (no loop). You could also use a loop without Find to accomplish the same thing.
    Last edited by stnkynts; 07-14-2011 at 11:13 AM.

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

    Re: For Loop and search

    Hi rigray, let me know if this works:
    Sub rigrayLoops()
        Dim i As Long, rCnt As Long, flRng As Range, ColLen As Long, cl As Variant
        rCnt = Sheets(1).Cells(Rows.Count, 4).End(xlUp).Row
        For i = 1 To rCnt
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            '''Setting the flRng starting at .Cells(2,4) represents the range "Name name".'''
            '''Where ever that cell is located, you should change the .Cells() address    '''
            '''accordingly.                                                               '''
            Set flRng = Sheets(1).Range(Sheets(1).Cells(2, 4), Sheets(1).Cells(rCnt, 4))
            For Each cl In flRng
                If cl.Value = "text" Then cl.Offset(0, -1).Value = "text2"
            Next cl
        Next i
    End Sub
    Please leave a message after the beep!

  6. #6
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: For Loop and search

    One correction, I should have not been explicit with the column number in the original post. I need the code to search for a specific column named "Name name". I am having additional problems finding a column with a space in the name. I will try to implement the other aspect as you've described. Thanks.

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

    Re: For Loop and search

    To find "Name name" first, use:
    Sub rigrayLoops()
        Dim i As Long, rCnt As Long, flRng As Range, ColLen As Long, cl As Variant
        Dim Namename As Range
        Set Namename = Sheets(1).Cells.Find("Name name")
        If Not Namename Is Nothing Then
        rCnt = Sheets(1).Cells(Rows.Count, Namename.Column).End(xlUp).Row
        For i = 1 To rCnt
            Set flRng = Sheets(1).Range(Namename, Sheets(1).Cells(rCnt, 4))
            For Each cl In flRng
                If cl.Value = "text" Then cl.Offset(0, -1).Value = "text2"
            Next cl
        Next i
        End If
    End Sub
    Please leave a message after the beep!

  8. #8
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: For Loop and search

    bah Mordred beat me to it. I used a loop on the column search just to show a different way. Here is mine:

    Sub MainMacro()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1")
    Dim lastrow As Long, lastcol As Long, icol As Long, mycol As Long
    Dim c As Range, iFind As Range
    Dim startaddress As String
    
    lastcol = ws1.Cells(1, Columns.Count).Column
    
    For icol = 1 To lastcol
        If ws1.Cells(1, icol).Value = "Name name" Then
            mycol = icol
            Exit For
        End If
    Next icol
    
    lastrow = ws1.Cells(Rows.Count, mycol).End(xlUp).Row
    
    Set iFind = ws1.Range(Cells(2, mycol), Cells(lastrow, mycol))
    With iFind
        Set c = .Find(what:="text", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext)
        If Not c Is Nothing Then
            c.Offset(0, -1).Value = "text2"
            startaddress = c.Address
            Do
                c.Offset(0, -1).Value = "text2"
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> startaddress
        End If
    End With
    End Sub

  9. #9
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: For Loop and search

    I was thinking more along the lines of the column search as well during my attempted implementation so I used your approach stnkynts, thanks for the help though Mordred. Anyway, I am getting and error at the definition of "lastrow". Any ideas?

  10. #10
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: For Loop and search

    I think it has something to do with mycol not getting defined properly. Here is the code now.

    Dim wsA As Worksheet:   Set wsA = Sheets("Status")
    Dim lastrow As Long, lastcol As Long, icol As Long, mycol As Long
    Dim c As Range, iFind As Range
    Dim startaddress As String
    
    lastcol = wsA.Cells(1, Columns.Count).Column
    
    For icol = 1 To lastcol
        If wsA.Cells(3, icol).Value = "EMS Composite Name" Then
            mycol = icol
            Exit For
        End If
    Next icol
    
    'lastrow = wsA.Cells(Rows.Count, mycol).End(xlUp).Row
    lastrow = 120
    
    Set iFind = wsA.Range(Cells(2, mycol), Cells(lastrow, mycol))
    With iFind
        Set c = .find(what:="spare", LookAt:=xlWhole, LookIn:=xlValues, SearchDirection:=xlNext)
        If Not c Is Nothing Then
            c.Offset(0, -1).Value = "spare"
            startaddress = c.Address
            Do
                c.Offset(0, -1).Value = "spare"
                Set c = .FindNext(c)
            Loop While Not c Is Nothing And c.Address <> startaddress
        End If
    End With

  11. #11
    Registered User
    Join Date
    07-14-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: For Loop and search

    ok. I found out why it was causing problems, it never found the header name I was using since I forgot i changed it on the test case. The code is running now, but it never ends and doesn't appear to be doing anything. I changed it to look at the third row for the column header name. Did I do this correctly?

  12. #12
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: For Loop and search

    Hmmm. Is your header name "EMS Composite Name" in row 3? If you could attach a copy of your workbook I could take a look at it to see where the gum up is.

  13. #13
    Valued Forum Contributor
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    244

    Re: For Loop and search

    I just noticed an error that i made which might be slowing things down.
    Change this:

    lastcol = wsA.Cells(1, Columns.Count).Column
    To this:

    lastcol = wsA.Cells(1, Columns.Count).End(xlToLeft).Column
    I apologize for this mistake.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0