+ Reply to Thread
Results 1 to 2 of 2

Warehouse Data From Collection of Excel Files in Different Folders (VBA)

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2016
    Location
    California
    MS-Off Ver
    2013
    Posts
    3

    Warehouse Data From Collection of Excel Files in Different Folders (VBA)

    Hi all! This forum has been a great help in the past and I've lurked here soaking up as much as I can. Alas, I've come to a problem that requires me to become active and thus, my first post!

    What I'm trying to accomplish:

    I've been asked to collect up data points, we'll call them "values" from a collection of Excel files scattered throughout a maze of folders on our network. The naming convention for the files do not follow any specific pattern, but, and here hopefully is the saving grace, the Worksheet within each file does follow a standard naming convention.

    Location: G:\Data\Notes
    Worksheet name: Data Values
    Data Location within Worksheet: Column B (text), Column C (value).
    Output: Two Columns (Column 1 (text), Column 2 (value))

    A few challenges/questions: Is there a way to ignore any text that is BOLD? The text elements in Column B that I would like to ignore are bold. I would prefer to only return text and their cooresponding values that are not bold. If not, I can work around this by simply removing any rows from Column 1 where Column 2 returns no value (null).

    Please let me know what other information you need from me. And most of all THANK YOU for providing such a wonderful resource for those of us still learning.

    Bests,

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: Warehouse Data From Collection of Excel Files in Different Folders (VBA)

    This could take a LONG time depending on how many files your talking about.

    The code consists of three sections. One Sub and two Functions.

    Copy to Module code page.

    Sub ImportNonBold()
    Dim WS As Worksheet
    Dim WBSrc As Workbook
    Dim WSSrc As Worksheet
    Dim Temp As Variant
    Dim A As Long
    Dim LastRow As Long
    Dim LRSrc As Long
    Dim FN As Variant
    Dim AllFiles As New Collection
    
    Application.ScreenUpdating = False
    
    'Creates a collection of all the path/filenames
    RecursiveDir AllFiles, "G:\Data\Notes\", "*.xls?", True
    
    'Define current sheet
    Set WS = ActiveSheet
    
    With WS
        'Determine lastrow of WS
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    End With
    
    'Loop through all collection of filenames
    For Each FN In AllFiles
        'Open source workbook
        Set WBSrc = Workbooks.Open(FN, , True)
        'Loop through all worksheets.
        For Each WSSrc In WBSrc.Worksheets
            'Looking for match.
            If WSSrc.Name = "Data Values" Then
                With WSSrc
                    'Determine lastrow of source worksheet
                    LRSrc = .Cells(.Rows.Count, "B").End(xlUp).Row
                    'Loop through all the rows of data
                    For A = 2 To LRSrc
                        'If cell is NOT bold.
                        If Not .Range("B" & A).Font.Bold Then
                            'If cell is not empty.
                            If .Range("B" & A) <> "" Then
                                'Copy Col B&C
                                .Range("B" & A & ":C" & A).Copy
                                WS.Range("A" & LastRow).PasteSpecial xlPasteAll
                                'File where data was extracted.
                                WS.Range("C" & LastRow) = FN
                                'Increment lastrow of WS.
                                LastRow = LastRow + 1
                            End If
                        End If
                    Next
                End With
            End If
        Next
        'Close source workbook and do not save changes.
        WBSrc.Close False
    Next
    
    'Bring cursor to top of worksheet.
    WS.Range("A1").Select
    Application.ScreenUpdating = True
    End Sub
    Public Function RecursiveDir(colFiles As Collection, _
                                 strFolder As String, _
                                 strFileSpec As String, _
                                 bIncludeSubfolders As Boolean)
    'http://www.ammara.com/access_image_faq/recursive_folder_search.html
        Dim strTemp As String
        Dim colFolders As New Collection
        Dim vFolderName As Variant
    
        'Add files in strFolder matching strFileSpec to colFiles
        strFolder = TrailingSlash(strFolder)
        strTemp = Dir(strFolder & strFileSpec)
        Do While strTemp <> vbNullString
            colFiles.Add strFolder & strTemp
            strTemp = Dir
        Loop
    
        If bIncludeSubfolders Then
            'Fill colFolders with list of subdirectories of strFolder
            strTemp = Dir(strFolder, vbDirectory)
            Do While strTemp <> vbNullString
                If (strTemp <> ".") And (strTemp <> "..") Then
                    If (GetAttr(strFolder & strTemp) And vbDirectory) <> 0 Then
                        colFolders.Add strTemp
                    End If
                End If
                strTemp = Dir
            Loop
    
            'Call RecursiveDir for each subfolder in colFolders
            For Each vFolderName In colFolders
                Call RecursiveDir(colFiles, strFolder & vFolderName, strFileSpec, True)
            Next vFolderName
        End If
    
    End Function
    
    
    Public Function TrailingSlash(strFolder As String) As String
        If Len(strFolder) > 0 Then
            If Right(strFolder, 1) = "\" Then
                TrailingSlash = strFolder
            Else
                TrailingSlash = strFolder & "\"
            End If
        End If
    End Function
    David
    (*) Reputation points appreciated.

+ 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. Create folders and save text files to these folders
    By bloomingcarrot in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-03-2014, 08:14 PM
  2. Use Excel VBA to Copy multiple files from different source folders to different folders
    By mm1234mail in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2014, 01:17 PM
  3. Replies: 0
    Last Post: 05-26-2014, 07:47 PM
  4. Replies: 1
    Last Post: 09-12-2013, 09:23 PM
  5. Replies: 0
    Last Post: 01-11-2013, 12:05 AM
  6. Replies: 1
    Last Post: 02-29-2012, 01:15 PM
  7. Create folders and move and copy files into that folders
    By vijaybharthi in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-31-2010, 04:01 AM

Tags for this Thread

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