+ Reply to Thread
Results 1 to 11 of 11

Cycling through files in a folder

  1. #1
    Registered User
    Join Date
    09-21-2009
    Location
    Nottingham, UK
    MS-Off Ver
    Excel 2003
    Posts
    5

    Cycling through files in a folder

    Hi all,

    I'm a novice with VB, so go easy on me. I'm trying to write a macro to copy the same couple of cells from hundreds of excel files and paste them into one summary file. What I've written so far is:

    Please Login or Register  to view this content.
    Forgive the clumsy break down of the files; they range from 001 to 063, and since I don't know how to set x as a three digit number I decided to have a section for 1-9 and a second section (with the same code) for 10-63.

    The problem I think I'm having is in referencing the workbook (the debugger always stops on the worksheets(wb).Activate line). Ideally I'd like the macro to open the files itself, but given that I can't even make it work when they're open I think I should start small. The issue, I believe, is that I'm not referencing the file correctly, since I'm trying to use the 'name + x' format to cycle through the files, and either I'm doing it wrong or this sort of variable referencing isn't suitable for the function. I've tried all manner of different tactics to get the activate workbook function liking my workbook name, but have had no luck.

    Any help would be hugely appreciated, and once again, forgive my unsubtle coding.

    Kate
    Last edited by KateMolloy; 09-28-2009 at 08:55 AM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Cycling through files in a folder

    Hi,

    I think your error may be here

    Please Login or Register  to view this content.
    this will set wb to namex.xls

    try

    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    09-21-2009
    Location
    Nottingham, UK
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Cycling through files in a folder

    Hi Dave,

    Thanks for your help. You're right, I did need to delete the "s around the x and y (I'd thought of that and decided not to for some reason!), but unfortunately I'm still getting the same error when running. In case it helps, the error is:

    Runtime error 9. Subscript out of range.

    Kate

  4. #4
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Cycling through files in a folder

    There's a couple of places that you have variables in quotes, which will result in the code not working.

    There's a ready made macro here which you may find useful

  5. #5
    Registered User
    Join Date
    09-21-2009
    Location
    Nottingham, UK
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Cycling through files in a folder

    Hi again,

    Thanks for the link, Dave, I've used the Macro you suggested and this has solved the problem I was having before. Unfortunately I've come across a new problem!

    I'm trying to do a basic copy and paste from one workbook to another. At the moment I have the sheets worked out (I think) but there's a problem with the pasting. The code I have in place at the moment (with a bit of extra around the edges) is:

    Please Login or Register  to view this content.
    The problem is that Excel doesn't like the ActiveSheet.Paste command for some reason! I've done the copy and paste manually while recording a Macro, and this is what it came up with, but when it's run on its own I get the error 'Object doesn't support this property or method'.

    I've tried loads of other paste options, including Selection.Paste and changing the Cell line above to .paste instead of .select (but I don't think the cells command works with paste), and changing the cells command to a range command, but this throws up yet more problems.

    I've simplified the macro to the real basics: I select the cells in the other workbook, then select the book I want to paste into and have the macro:

    Please Login or Register  to view this content.
    And while this is basically exactly what's recorded when I do it manually and record, when I run it as a macro I get the same problems.

    I've tried other methods of selecting the cell I need to paste into, such as columncount etc, but have had no luck either.

    I can't believe I'm having this much trouble with such a simple thing, so any help would be really appreciated!

    Thanks again,

    Kate

  6. #6
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,454

    Re: Cycling through files in a folder

    Try activating a worksheet in wbNew by replacing this:

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

    Please Login or Register  to view this content.
    I've used sheet 1 for an example.

  7. #7
    Registered User
    Join Date
    09-21-2009
    Location
    Nottingham, UK
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Cycling through files in a folder

    Hi again Dave,

    Unfortunately that's one of the things I've tried. In fact I've still got it in in the mini macro I'm using to tease the problem out, but it hasn't helped. Should have posted it in the code, really, sorry about that.

    Just to check the line I have works, it's:

    Workbooks("data.xls").Worksheets("Sheet1").Activate

    Kate

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Cycling through files in a folder

    This may help (or not); tryu replacing this code
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.
    Hope this helps.
    J
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  9. #9
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Cycling through files in a folder

    hi Kate,

    This may need some fine tuning but hopefully it will do what is needed once the changes are made - my idea of not using copy & paste removes the need to flick between files, removes the risk of something else being mistakenly put on the clipboard or the clipboard being cleared while the macro is running (it may be faster too but I'm not sure). I've included the LastCell Function, rather than coding it into the same macro, as you may find it useful for other code development too.

    Kate/Dave, some of your code in posts 5 & 6 respectively seems to be missing the dot prefixes for the range or sheet within the With statements.

    - fingers crossed...
    Please Login or Register  to view this content.
    hth
    Rob
    Last edited by broro183; 09-26-2009 at 06:50 PM. Reason: fixed code tags
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  10. #10
    Registered User
    Join Date
    09-21-2009
    Location
    Nottingham, UK
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Cycling through files in a folder

    Thanks to you all! I copied the text I had in my macro into a new file this morning and for some reason it's now working! Must have been a bug somewhere, I have no idea what, but luckily it's ok now.

    Particular thanks to Rob for that last post; it does seem like a nicer way of doing things, and I may experiment with it once I've got the job done, just out of interest.

    Thanks again, I really appreciate people taking time out of their days to help novices like me.

    Kate

  11. #11
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Cycling through files in a folder

    Thanks for the feedback & marking the thread as solved - I'm pleased we could help :-)

    Rob

+ 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