+ Reply to Thread
Results 1 to 13 of 13

VBA to copy specific cells from all files in a folder

  1. #1
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    VBA to copy specific cells from all files in a folder

    Hi guys,

    I am trying to copy values from specific cells in all the files stored in a folder and paste it onto a new file. I found a code which does almost what I am after. The only problem here is that it can not access the file names with an apostrophe like "Simon's Boilers", "Baker's extra", "Sauer's bakehouse" etc.

    Is there a way to fix this thing? Other than that it is all good.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Hi Mehul,

    The problem is that in your code (by design) a single apostrophe denotes a the start or end of a field. To get around this you to write code to replace any single apostrophe in the fn filename variable with two apostrophes, which though I can't test without being on your server, I would use something like this...

    Please Login or Register  to view this content.
    ...just below this line:

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks a lot Trebor76. I just had to change the code a bit as below and it worked out perfectly.

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I have one more query if you could please help me.

    As you see the code is operating in the worksheet named "Hours". Is it possible to program the code to look in Sheet2 if "Hours" worksheet is not present?

    Thanks once again.

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Is it possible to program the code to look in Sheet2 if "Hours" worksheet is not present?
    Though it's much easier to do this while the file is open, try these lines of code...

    Please Login or Register  to view this content.
    ...immediately under this existing line:

    Please Login or Register  to view this content.
    Regards,

    Robert

  6. #6
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks Robert. It still asks me to select the sheet every time it can't find Hours worksheet.

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Without being on your server and testing it's very hard to recommend a solution, but see how this goes:

    Please Login or Register  to view this content.
    Robert

  8. #8
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I absolutely agree it is difficult to recommend a solution this way. It still continues to do the same thing.

    Not sure if this will help, here is a screenshot of the point where excel asks me to select a sheet as it couldn't find Hours sheet. Hope it helps.

    Untitled.jpg

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    Not sure if this will help, here is a screenshot of the point where excel asks me to select a sheet as it couldn't find Hours sheet. Hope it helps.
    No it doesn't as the attachment doesn't open

  10. #10
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I'm sorry for that.
    I have attached it once again or you can use this link to access it.

    http://postimg.org/image/7jbyuzx6v/

    Untitled.jpg

  11. #11
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: VBA to copy specific cells from all files in a folder

    OK, that really doesn't help either but try this where I use an ADO connection to the workbook to determine if the sheet exists or not:

    Please Login or Register  to view this content.
    Again this is untested so it may need some tweaking on your side. Also note the comment regarding making a reference to the Microsoft ActiveX Data Objects library which is required before you try and run the macro.

    Regards,

    Robert
    Last edited by Trebor76; 11-03-2014 at 01:04 AM.

  12. #12
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    Thanks for your help Robert. Unfortunately this did not work either. So to make it less complicated I manually sorted out the files from the folder which did not have HOURS spreadsheet on it.

  13. #13
    Registered User
    Join Date
    10-17-2014
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    29

    Re: VBA to copy specific cells from all files in a folder

    I have just one last issue with this code. The code always gives the output in the first sheet of the workbook. For example, even if I have the code in Sheet3 and run it there the output goes to Sheet1. How can I fix that?
    Here is code that I am using currently.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 01-28-2019, 06:55 PM
  2. [SOLVED] Need to copy values from all files in folder from specific worksheet
    By jh51745 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-03-2013, 01:05 PM
  3. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  4. [SOLVED] Copy 10 rows from specific files in a folder
    By GDM69 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 08-07-2012, 10:13 AM
  5. Replies: 1
    Last Post: 01-24-2006, 11:00 AM

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