+ Reply to Thread
Results 1 to 7 of 7

VBA; loop; search for string, cut/paste then search second time

  1. #1
    Registered User
    Join Date
    06-01-2010
    Location
    Pacific, Ocean
    MS-Off Ver
    Excel 2002
    Posts
    6

    Thumbs up VBA; loop; search for string, cut/paste then search second time

    Relatively new to macros and I'm starting to venture past the recorder. I've been unable to write code for this loop...

    I would like to search a short range in one column (B1:B50) for two of 5 names.

    Two, and only two of the five names will always appear in the range. And, each name will only appear once. However, the location of the two names within the range will vary from one day to the next.

    When the first name is found, I would like to cut/paste it at A15.

    Resuming the search in column B, when the second name is found, I would like to cut/paste it at A25.

    Example. Names: John, Joe, Jack, Jill, Jane. 'Jill' is located in cell B12. 'John' is located in cell B33.

    What VBA code would, 1) search for the names in range B1:B50; 2) when the first name is found, identify the row (msg box) and cut/paste it at A15; 3) resume the search one cell below where the first name was found; 4) when the second name is found, cut/paste it at A25?

    Any help would be greatly appreciated.
    Last edited by tw9901; 06-06-2010 at 10:02 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA; loop; search for string, cut/paste then search second time

    Edit the sheet name and then try this:
    Please Login or Register  to view this content.

    I know I added one to the search range adding B51, but that makes it work consistently.
    Last edited by JBeaucaire; 06-05-2010 at 02:08 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    06-01-2010
    Location
    Pacific, Ocean
    MS-Off Ver
    Excel 2002
    Posts
    6

    Re: VBA; loop; search for string, cut/paste then search second time

    Thank you for the reply, Jerry.

    The code finds the names and does the cut/paste fine. However, the names are being placed alphabetically whereas I need them to be placed sequentially. That is, searching from B1 to B51, the first name found should go to A15. Then resume the search at the cell below where the first name was located and continue the search for the second name.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA; loop; search for string, cut/paste then search second time

    Can you post a sample workbook clearly showing how you are using it and how it doesn't do what is needed?

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.

  5. #5
    Registered User
    Join Date
    06-01-2010
    Location
    Pacific, Ocean
    MS-Off Ver
    Excel 2002
    Posts
    6

    Re: VBA; loop; search for string, cut/paste then search second time

    Here is a sample file; I've tried to show the desired outcome on Sheet1
    Attached Files Attached Files

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA; loop; search for string, cut/paste then search second time

    Use this instead:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-01-2010
    Location
    Pacific, Ocean
    MS-Off Ver
    Excel 2002
    Posts
    6

    Re: VBA; loop; search for string, cut/paste then search second time

    Ya, Baby!!

    Works perfect!

    Thank you!!

+ 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