+ Reply to Thread
Results 1 to 15 of 15

Copy 10 rows from specific files in a folder

  1. #1
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Copy 10 rows from specific files in a folder

    Hi Friends:

    I need help for the following problem. I have about 300 odd excel files in Excel 2003 format in a folder. I wish to extract 10 rows from some selected files out of these 300 odd files, based on a certain column B cell value, which is always a date string. The cell value to look for is "25/05/2012". I have a reference workbook (named reflist.xls) where I have listed, in column C, all the selective files to be searched for this particular date string. This reference file would be kept open (or the code itself can be added to run from this file). The macro should search for only for the file names mentioned in Column C and then search for the date string in the selected files (ignoring all other files in the folder), and when the search term is found, copy that particular row and subsequent 9 rows to Sheet2 of this reference workbook itself. All the files in the folder are properly sorted, have only a single worksheet, and data is upto column N. The reflist.xls lists files to look for with their full name including extension. Thanks.
    Last edited by GDM69; 07-29-2012 at 09:33 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: Copy 10 rows from specific files in a folder

    Based on that information, this should do it. It's difficult sometimes to "FIND" a date unless you get the format correct, which I tried to do, but you'll have to test/tweak that part.

    Please Login or Register  to view this content.
    _________________
    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
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    Hi JBeaucaire:

    Thank you very much for your code. I tried the code and it is working exceedingly well. Since my actual date values in the column were in "3-Apr-12" format, I did have difficulties with the code as it was not giving any output at all. I then tweaked the code as follows and it worked!

    Please Login or Register  to view this content.
    This line was changed to:
    Please Login or Register  to view this content.
    This change got it going.

    Thanks once again for your kind help.
    Last edited by JBeaucaire; 07-30-2012 at 11:53 AM.

  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: Copy 10 rows from specific files in a folder

    Just a warning about Excel's FIND method, you took out the LookIn and LookAt parameters, you should not do that. When you took them out, that had no effect on the macro at the moment because it kept using the parameters you took out, that's the problem you have to beware of. The FIND method remembers the last values used for LookIn and LookAt and will use them if you don't specifically control them in the macro.

    That means if you were to manually use the CTRL-F (Find) functionality in your daily use of Excel for something else, whatever settings you used for that search would become "set" in the FIND wizard and will be used when the macro runs, too, unless you let the macro specifically correct those values.

    As such, I neverr leave those two parameters out.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    Hi JBeaucaire:

    Thank you very much for enlightening me about the "Find" properties. Actully, I do not know much about VBA and my tweakings are more or less based on trial and error method.

    I regret to inform you that the above code line with the two necessary parameters never did work in my several trials but the code line with my changes did work on many occasions, though not all. I have observed that the code is working very very irratically, sometimes it works ok and sometimes there is no output at all. There is no consistency. Could you throw some light on these issues for me and other fellow members?

    Thanks

  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: Copy 10 rows from specific files in a folder

    Quote Originally Posted by GDM69 View Post
    I have observed that the code is working very very irratically, sometimes it works ok and sometimes there is no output at all. There is no consistency. Could you throw some light on these issues for me and other fellow members?
    Thanks
    Heh, I thought I addressed that already:
    Quote Originally Posted by JBeaucaire
    It's difficult sometimes to "FIND" a date unless you get the format correct, which I tried to do...

    Click GO ADVANCED and use the paperclip icon to post up a copy of your workbook, or enough of an excerpt that I can see the problem in action. Make sure your edited version of the macro is already in the workbook. I'll take a look. Try to include instructions on using the data/macro and it fails.

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Copy 10 rows from specific files in a folder

    Hi GDM69,

    I agree with JBeaucaire on this matter.

    Try:

    Please Login or Register  to view this content.
    and:
    Please Login or Register  to view this content.
    Hope that helps
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  8. #8
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    Hi JBeaucaire:

    I am enclosing the sample file as you have suggested. I have incorporated my modified code in the workbook.

    As I have said earlier, the code line with the 2 essential parameters didn't work in my several attempts. Please look into.

    Thanks
    Attached Files Attached Files

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

    Re: Copy 10 rows from specific files in a folder

    I'm not sure what this file is supposed to show me. The macro is designed to search file names in column C, thisworkbook does not contain that data. Did things get mixed up? Is the macro enclosed supposed to be in another workbook and this file is one of the "searched files"?


    Based on what I *DO* see in that data, I already told you you would need to tweak the FORMAT to match the date format of your data sheet, so maybe this will help clarify what I mean:

    Please Login or Register  to view this content.
    That matches the data format of your column A dates. Also, in the popup, enter the date in a way you KNOW won't be confused by Excel. In the US, the date:

    12/6/12 is December 6, 2012.

    In India, I presume that would be June 6, 2012. Enter the date in the popup in a way you know will be evaluated correctly, like June 6, 2012.
    Last edited by JBeaucaire; 08-04-2012 at 12:52 PM.

  10. #10
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    I'm not sure what this file is supposed to show me. The macro is designed to search file names in column C, thisworkbook does not contain that data. Did things get mixed up? Is the macro enclosed supposed to be in another workbook and this file is one of the "searched files"?
    I have attached this file as a data file for you to see the data, especially the date format. Take A28 value "25-Jun-12" In the formula bar it shows as "25-06-2012" (in my PC with US English setting). So, there was no need to tweak the format setting that you provided in the code. The point is, it is not working with the 2 essential parameters but it does work with my tweaking "Set dFIND = Cells.Find(dVAL)" (after dropping those 2 parameters). I would like you to try this out on my sample file.

    I do understand that the macro looks at Column C to extract the file names to look for but there was no point for me to attach this master code file as the file names would have been useless for your trial since those files don't exist with you. I can attach 3 to 4 sample files and one master code file, if you so desire, for your trial. Otherwise you can create 2-3 dummy files based on my sample file and then try out the code with different tweakings, to locate the exact problem. But please recreate the same environment as my PC (Column A values should appear as I have mentioned above) and then try out the code to arrive at the correct conclusions. Please note that for me your code does not need any other tweaking other than "Set dFIND = Cells.Find(dVAL)" to get the correct rows. The problem is it is working irratically. If I make any changes Set dFIND then reset and rerun the code, it doesnt work. I have to exit Excel, relaunch Excel again and then run the code but it always works only with "Set dFIND = Cells.Find(dVAL)" and no other way. By making any other change in this line, I do not get any output at all, even after exiting and relaunching the MS Excel.

    Please look into the matter.

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

    Re: Copy 10 rows from specific files in a folder

    I apologize that I won't be able to suss out why your Excel implementation is behving erratically. The method/macro I've given is the one I've used in 1000s of macros, the only issue that needs managing is the matching of the date format and the inclusion of the 2 parameters you indicate actually make it not work. In my 1000s of macros, taking them out leads to erratic results, not the other way.

    I'm sorry your environment seems to be behaving the opposite of normal. I couldn't comment on why at this point.

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Copy 10 rows from specific files in a folder

    the code in that workbook is not what was suggested. the line should be
    Please Login or Register  to view this content.
    so that the search format matches the format of the data as it appears in the sheet (not as it appears in the formula bar). to match the formula bar contents (since your data is literal, not the result of formulas) use
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  13. #13
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    Hi JBeaucaire and JosephP:

    I am glad to inform you that the modification suggested by JosephP (second code line) worked flawlessly. I tried the code several times and the output was perfectly fine every time. Thank you very much!

    I need some more help. Sometimes, in case of some files, I may be required to search on specific value (plain value instead of date) in SPECIFIC column (let us say value "-129.14" in Column H as appearing in the uploaded file), what kind of tweaking will be required in this case?. We have to look for first occurance of this value from the top in column H. I tried out really hard but could not succeed.

    Thanks

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

    Re: Copy 10 rows from specific files in a folder

    Heh, I apologize for not spotting that. I reiterate, it's not only important to always include those two parameters when doing a FIND, it's important they be set correctly!

    LookIn:=xlValues
    LookIn:=xlFormulas

    LookAt:=xlWhole
    LookAt:=xlPart

    You will always need to figure out which are the correct parameters for your situation, then the macro controls them explicitly every time and should work every time.

    ==============

    You can make your searches more surgical by searching just the RANGE needed instead of all CELLS.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    04-27-2012
    Location
    india
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Copy 10 rows from specific files in a folder

    Hi JBeaucaire and JosephP:

    Thank you very much. I have learnt quite a lot from this thread. I was also able to tweak the code to look for plain values instead of dates.

+ 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