+ Reply to Thread
Results 1 to 13 of 13

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.

    Please Login or Register  to view this content.
    Last edited by rigray; 07-14-2011 at 11:04 AM.

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

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

  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
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: For Loop and search

    Try:
    Please Login or Register  to view this content.

    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 Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: For Loop and search

    Hi rigray, let me know if this works:
    Please Login or Register  to view this content.

  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 Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: For Loop and search

    To find "Name name" first, use:
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    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:

    Please Login or Register  to view this content.

  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.

    Please Login or Register  to view this content.

  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
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    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
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: For Loop and search

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

    Please Login or Register  to view this content.
    To this:

    Please Login or Register  to view this content.
    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.6.0 RC 1