Archive for VBA Code Library

Consolidating Data from >20 workbooks, each workbook Containing >70 worksheets

– Selva V Pasupathy, HSBC Global Resourcing, Hyderabad

Option Explicit
‘___________________________________________________________________________
‘***************************************************************************
‘*
‘* MODULE NAME:     Copy Data from all Excel Files in a Folder
‘* AUTHOR:          Selva V Pasupathy, HSBC Global Resourcing, Hyderabad
‘*
‘* CONTACT:         socko@rediffmail.com
‘* WEB SITE:        https://socko.wordpress.com
‘*
‘* DESCRIPTION:     The following visual basic code was written when
‘*                  I wanted to consolidate the data from 26 workbooks
‘*                  and in each workbook, there were are 77 sheets. It
‘*                  would have been a very difficult task, if I had to
‘*                  do it manually. I believe this procedure will be
‘*                  useful to somebody else
‘* NOTES:
‘*  Before using the codes, you can change all the variables like,
‘*  Folder, File , SearchString, SearchSubfolders (true / false),
‘*  and other variables.
‘*————————————————————————
‘* Other Information
‘*
‘* UPDATES:
‘*  DATE            COMMENTS
‘*  16 Nov 2008     You are free to use , change, and modify this code.
‘___________________________________________________________________________
‘***************************************************************************

Function ListFiles(sCount As Integer, ByVal sFldr As String, _
                bFldr As Boolean, sFileName As String) As String
Dim fCnt As Integer

    With Application.FileSearch
        .NewSearch
        .LookIn = sFldr
        .SearchSubFolders = bFldr
        .Filename = sFileName             ‘”*consolidated*.xls”
        .FileType = msoFileTypeAllFiles
        
        If .Execute() > 0 Then
            
            If .FoundFiles.Count > sCount Then
              
              For fCnt = 1 To .FoundFiles.Count
                  
                  If fCnt = sCount Then
                    
                    ListFiles = .FoundFiles(fCnt)
                  
                  End If
              
              Next i
            
            Else
            
            ListFiles = “”
            
            End If
        
        Else
            
            MsgBox “There were no files found.”
        
        End If
    
    End With

End Function

Sub Consolidate_Data()
Dim cFile As Integer
Dim sht, rw, cl, avLastRow As Long
Dim myFileName, myFolder, sFileName As String
Dim myFile As Workbook
Dim sFldr As Boolean

myFolder = “J:\GB Project\CONSOLIDATED”
sFldr = False
sFileName = “*consolidated*.xls”

With ThisWorkbook.Sheets(“data”)
  For cFile = 13 To 100
    myFileName = ListFiles(cFile, myFolder, True, sFileName)
    If myFileName = “” Then Exit Sub
      Set myFile = Workbooks.Open(myFileName)
      ThisWorkbook.Activate
      ThisWorkbook.Sheets(“data”).Activate
        For sht = 2 To myFile.Sheets.Count
          For rw = 1 To 100
            avLastRow = .Cells(65536, 1).End(xlUp).Row + 1
            ThisWorkbook.Sheets(“data”).Cells(avLastRow, 1).Activate
            .Cells(avLastRow, 1) = myFile.Sheets(sht).Name
            For cl = 1 To 18
            Application.StatusBar = “Writing File# ” & cFile & _
                    ”  Sheet# ” & sht
            .Cells(avLastRow, cl + 1) = myFile.Sheets(sht).Cells(rw, cl)
            Next cl
          Next rw
        Next sht
  myFile.Close False
  ThisWorkbook.Save
  Next cFile
End With
End Sub

Advertisements

Leave a Comment

Archives

  1. ActiveX Programming
  2. Add an Image to Cell Comments
  3. Add Sheet Navigation CommandBar to an Excel file or Application
  4. Auto-Generate a File Name
  5. Automate File Download from Internet
  6. Automate WebLogin…
  7. Convert Decimal to RGB Color Value
  8. Copy Ranges from Different Sheets into one Sheet
  9. Create HyperlinkObject on Sheet
  10. Create LogFile for Workbook Open
  11. Create Sheet “Table of Contents”
  12. Create ShortCut Link at the desired location
  13. Create UserForm at Runtime using Code
  14. Display Userform with no Title Bar (Splash Screen)
  15. Downloads
  16. Downloads from www.jkp-ads.com
  17. Export a Text File with Comma and Quote Delimiters
  18. Get elements from HTML form
  19. Get FileName with Path from User
  20. Get FileNames with Path from User
  21. Hyperlink Elements in HTML
  22. Ken’s Knowledge Base submissions
  23. Login to Rediffmail
  24. Move a File
  25. Multiple Cell Concatenation
  26. Other Site Links
  27. Read CSV Using VBA techniques
  28. Remove Unprintable Character & Spaces from cells
  29. Rename a File
  30. Rename Files using Codes (VBA)
  31. Resources
  32. Resources
  33. Search Folders and Subfolders for files
  34. Show Status on Userform While executing Code
  35. Site Contents
  36. socko.wordpress.com — Technorati Search
  37. Sort WorkSheets by Name
  38. Sorting Text with Worksheet Formula
  39. test
  40. Use of vbScript in Database Applications
  41. Userform Resizer – VBA
  42. Userforms
  43. VBA Code Library
  44. vbaX KB Links
  45. Verify that a File Exists
  46. Web Forms manipulated by Visual Basic
  47. Write to & Read from Sequential Text Files

Leave a Comment

Tips on VBA

Some basic to intermediate VBA tips from http://www.automateexcel.com
– Selva V Pasupathy, HSBC Global Resourcing, Hyderabad, India

Comments (1)