Results 1 to 11 of 11

Get data from all workbooks in folder

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-23-2007
    Location
    Texas
    Posts
    417

    Get data from all workbooks in folder

    A while back someone helped me out with a code to pull data out of all workbooks in a folder. This is the code
    Sub RefreshRates()
    Dim fn As String, myPath As String, e, n As Long
    Dim dic As Object, w
    Set dic = CreateObject("Scripting.Dictionary")
    dic.CompareMode = vbTextCompare
    myPath = ThisWorkbook.Path & "\"
    fn = Dir(myPath & "*.xls")
    Do While fn <> ""
        With CreateObject("VBScript.RegExp")
            .Pattern = ".*1(\D+)9_(\d{8}).xls"
            If .test(fn) Then
                If Not dic.exists(.Replace(fn, "$1$2")) Then
                   dic.Add .Replace(fn, "$1$2"), Array(fn, "")
                Else
                   w = dic(.Replace(fn, "$1$2")): w(0) = fn
                   dic(.Replace(fn, "$1$2")) = w
                End If
            End If
            .Pattern = ".*2(\D+)1_(\d{8}).xls"
            If .test(fn) Then
                If Not dic.exists(.Replace(fn, "$1$2")) Then
                    dic.Add .Replace(fn, "$1$2"), Array("", fn)
                Else
                    w = dic(.Replace(fn, "$1$2")): w(1) = fn
                    dic(.Replace(fn, "$1$2")) = w
                End If
            End If
        End With
        fn = Dir
    Loop
    'With ThisWorkbook.Sheets(1).Range("a" & Rows.Count).End(xlUp)(2)
    '    For Each e In dic.Items
    '        If e(1) <> "" Then
    With ThisWorkbook.Sheets(1).Range("a2")
        With .CurrentRegion
     '    .Resize(.Rows.Count - 1).Offset(1).ClearContents
        Range("a2:e65500").Select
        Selection.ClearContents
        End With
        For Each e In dic.Items
            If e(1) <> "" Then
     ' This If looks at ESP2
                .Offset(n).Formula = "='" & myPath & "[" & e(1) & "]summary'!EH3"
                .Offset(n, 2).Formula = "='" & myPath & "[" & e(1) & "]summary'!EH4"
                .Offset(n, 3).Formula = "='" & myPath & "[" & e(1) & "]summary'!EH5"
            End If
     ' This If looks at ESP1
            If e(0) <> "" Then
                .Offset(n, 1).Formula = "='" & myPath & "[" & e(0) & "]summary'!G17"
                .Offset(n, 4).Formula = "='" & myPath & "[" & e(0) & "]summary'!H4"
            End If
            n = n + 1
        Next
    End With
    End Sub
    This code works flawless!

    What I thought I could do with this code is create another file with this code, change a few things and have it work in another folder with a bunch of files pulling infromation from cells...(Clear as mud?) Well, it's not that easy I guess... for a nub anyway

    What can I do to make this code work for different files? The files I am working with in a folder now are named like this 01-02-08_All.xls. Only the date changes in the nameing. Sorry I don't remember exactly who wrote this code but I think it was that really smart guy "Ross" but I could be wrong. Found the old post.. it was that elite Richard Buttrey that first helped me with this code!
    Last edited by Tortus; 12-08-2008 at 04:59 PM.

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