+ Reply to Thread
Results 1 to 11 of 11

Macro to search a string, then search above for another one and copy the rows between them

  1. #1
    Registered User
    Join Date
    11-14-2012
    Location
    Lima, Peru
    MS-Off Ver
    Excel 2007
    Posts
    5

    Macro to search a string, then search above for another one and copy the rows between them

    Good night,
    Im having a really hard time trying to figure out how to make a Macro to Search for a Substring then search the rows above for a different one and finally select the cells until a third criteria is met, after the selection is made copy this to a different sheet based on another string that must be searched.

    Lets say this is the info from the Data Sheet all the information for every row is on the same cell in column A, the number of rows between the "----" and the number of total rows are not always the same, sometimes there are blank rows in the data. For the Example we'll use 3 aditional sheets "P", "O" and "I".

    ----
    P11
    111
    11x
    111
    ----
    O11
    111
    11z
    ----
    I11

    11x
    ----

    What I'm looking for is a way to search for "X", then search the cells above for "----" and made a selection til the next "----" cell, when this is made, search inside the selection for "P", "O" or "I", and copy the selection to the sheet with the same name, then repeat the process until all the rows are done. Using the info above the macro should encounter the first "X" on the 4th row then search above and select from the 1st to 6th row and copy this selection to Sheet "P".

    Thanks for your time.
    Attached Files Attached Files

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Macro to search a string, then search above for another one and copy the rows between

    Hi DarKDjinni, try it
    Please Login or Register  to view this content.
    Attached Files Attached Files

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro to search a string, then search above for another one and copy the rows between

    Hi

    Try this

    Please Login or Register  to view this content.
    The example data doesn't seem to have anthing that will go into sheet O. Is this correct?

    rylo

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to search a string, then search above for another one and copy the rows between

    Yet one more...

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-14-2012
    Location
    Lima, Peru
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to search a string, then search above for another one and copy the rows between

    Hello everyone, thank you so much for your time (And my apologies for taking this long before give you my words of gratitude)

    I had tested all the macros, they do the job and are really fast, you guys rock !! Actually Rylo's code doesn't copy the "----" and I wanted those as delimiters, but I'm pretty sure that was cause my explanation wasn't clear enough and that you can fix it in a blink = )

    Rylo's it was a mistake, the "z" on the sample sheet was a typo

    At this point I think that this thread should be flagged as Solved, because I can use your Codes to do exactly what I asked for, the problem is that I couldn't modify any of them to do what I really "need".

    So my question is should I flag this as Solved and start a new thread or post my new questions here with a new sample sheet ?

    Thank you
    Last edited by DarKDjinni; 11-17-2012 at 04:49 PM.

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro to search a string, then search above for another one and copy the rows between

    Quote Originally Posted by DarKDjinni View Post

    So my question is should I flag this as Solved and start a new thread or post my new questions here with a new sample sheet ?
    What do you really need?

  7. #7
    Registered User
    Join Date
    11-14-2012
    Location
    Lima, Peru
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to search a string, then search above for another one and copy the rows between

    Well a Code to do what I asked for in the beginning, copy a range of rows based on multiple criterias and you provide me with that but I need it to work with different data, I thought that I will be able to do a few tweaks to your codes to work with my real data but I couldn't...

    If anybody stills wants to give me a hand, this time the sample sheet will have the data as much closer to the real that I could possibly upload, on the sample sheet I replaced every letter with an "X" and the numbers with "1", and put the criterias on bold and a green Font.

    Then the code should search for this substrings:
    "Message Type: R" or "Status: R"
    Then search above for the line "-----" , select until the next line "-----", when this is done search inside the selection for this substrings:
    "Number:999xx" (the "xx" could be any number from 05 to 95) or "Number: 999xx" (With 2 spaces between the ":" and the numbers) and copy the selection to the sheet with the same name as the number (999xx) it found, then repeat the process unitl all the rows are done.

    On the sample sheet are 4 selections that met this criteria for every sheet from 99905 to 99930
    and sheets 99940, 99950, 99960, 99970, 99980 and 99990

    Thank you so much guys
    Attached Files Attached Files

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro to search a string, then search above for another one and copy the rows between

    Hi

    You say you want to search for message type r or status r, but you have highlighted mesage type a and status a in green font in your example file to signify data to transfer. Exactly what data should be copied across?

    rylo

  9. #9
    Registered User
    Join Date
    11-14-2012
    Location
    Lima, Peru
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to search a string, then search above for another one and copy the rows between

    Just use the ones with an "R" as criteria, the other are there just to show that there are different Status.

    The "Status A" and "Message Type A" shouldn't be copied and Im not really sure why I highlighted those.

    Thanks

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Macro to search a string, then search above for another one and copy the rows between

    Hi

    OK, see how this goes.

    Please Login or Register  to view this content.
    rylo

  11. #11
    Registered User
    Join Date
    11-14-2012
    Location
    Lima, Peru
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Macro to search a string, then search above for another one and copy the rows between

    I tested your code with my raw data and it works great, its amazing.

    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