+ Reply to Thread
Results 1 to 5 of 5

Copying values from a folder full of closed workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2015
    Location
    Geraldton Australia
    MS-Off Ver
    2010
    Posts
    21

    Copying values from a folder full of closed workbooks

    Hi Guys
    I have been trying to experiment with copying files from a closed workbook. I am particularly interested in copying and pasting using the destination method where I don't need to copy and paste as values. I want to understand how the command works but I don't really know. My knowledge of VBA is cobbled together from copying other code and figuring out what each line of code does in relation to the spreadsheets. That is all well and good until I want to understand how a code works properly then I have to try and research and find out and when I fail to understand I am going to come here and ask in the hope someone can enlighten me. So far I have the code working in some capacity.

    I have attached 2 files one named original with my original code which works well and the new extraction with the new code which doesn't work so well as it leaves me with #N/A in all of the rows below the first lot of data that it manages to extract.
    It won't let me post the code in here which is the second time I have not been able to post code in the forums due to the security features of this site.
    ORIGINAL DATA EXTRACTION.xlsm

    NEW DATA EXTRACTION.xlsm

    My lack of understanding of where to put lngrow means I am unsure how to incorporate it into this. I want the first 80 rows of data to fill up then follow on with the next 80 rows of data below it from the next closed workbook. I am unsure of how to do this with this particular command. I am really just trying to learn how to use VBA as efficiently as possible. I know there is many ways to doing things but I want to have code that executes quickly and effectively and whilst copy and paste values works well I understand that it is slow and cumbersome and have noticed how much faster the destination method is. Over time and experimenting with different lines of code I hope to pick up VBA enough to the point where I can write code off the top of my head without having to reference any materials ensuring what I write is the fastest possible way of performing the tasks. You don't notice how long some code takes until it has to work through many workbooks and I am learning that a few simple tweaks can make a world of difference. I would appreciate any help anyone can offer me on this

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

    Re: Copying values from a folder full of closed workbooks

    Original code:
            lngrow = lngrow + 80
            ws1.Range("A2:A81" & lngrow).Value = ws.Range("A16:A95").Value
    In this instance you are using lngrow as an 80 row offset for each file you copy values from. So try something like this.

            ws1.Range("A2:A81").Offset(lngrow, 0).Value = ws.Range("A16:A95").Value
            lngrow = lngrow + 80

    So on the first file there is 0 offset, the second file there is an 80 row offset from A2:A81, 3rd file offset 160 rows from A2:A81...
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-12-2015
    Location
    Geraldton Australia
    MS-Off Ver
    2010
    Posts
    21

    Re: Copying values from a folder full of closed workbooks

    Quote Originally Posted by AlphaFrog View Post
    Original code:
            lngrow = lngrow + 80
            ws1.Range("A2:A81" & lngrow).Value = ws.Range("A16:A95").Value
    In this instance you are using lngrow as an 80 row offset for each file you copy values from. So try something like this.

            ws1.Range("A2:A81").Offset(lngrow, 0).Value = ws.Range("A16:A95").Value
            lngrow = lngrow + 80

    So on the first file there is 0 offset, the second file there is an 80 row offset from A2:A81, 3rd file offset 160 rows from A2:A81...
    Thanks for that. This worked just as I was aiming for. Thanks for the explanation it has helped me understand how this works now too.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,027

    Re: Copying values from a folder full of closed workbooks

    Try this approach.
    Sub CopyRange()
        Application.ScreenUpdating = False
        Dim wkbDest As Workbook
        Dim wkbSource As Workbook
        Set wkbDest = ThisWorkbook
        Const strPath As String = "C:\Test\"
        ChDir strPath
        strExtension = Dir("*.xls*")
        Do While strExtension <> ""
            Set wkbSource = Workbooks.Open(strPath & strExtension)
            With wkbSource
                .Sheets("Sheet1").Range("A16:B95").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                .Sheets("Sheet1").Range("F16:F95").Copy wkbDest.Sheets("Sheet1").Cells(Rows.Count, "D").End(xlUp).Offset(1, 0)
                .Close savechanges:=False
            End With
            strExtension = Dir
        Loop
        Application.ScreenUpdating = True
    End Sub
    Change the path in this line :
     Const strPath As String = "C:\Test\"
    to match the path of the folder containing your source files. Also change the sheet names to match yours if necessary.
    Last edited by Mumps1; 08-31-2015 at 12:41 PM.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

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

    Re: Copying values from a folder full of closed workbooks

    You're welcome. Thanks for the feedback.

+ 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. Copying data from a master worksheet to a folder full of closed workbooks
    By Simsam_g in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2015, 04:20 AM
  2. [SOLVED] Macro to list worksheet names of all closed workbooks in a specified folder
    By Chrispelletier in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-15-2014, 12:52 PM
  3. [SOLVED] Macro to loop through workbooks in folder and attach and email using address in closed WB
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-03-2013, 07:23 PM
  4. Print Full Folder of Workbooks
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-17-2009, 06:41 AM
  5. VBA: Copying data from closed workbooks
    By staffordalex in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2008, 01:20 PM
  6. Copying data from closed Workbooks
    By danman1043 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2008, 09:11 AM
  7. Possible ? Search all closed workbooks in a folder
    By Corey in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2006, 11:35 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