+ Reply to Thread
Results 1 to 2 of 2

Collating data from varying number of sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    11-22-2009
    Location
    Newcastle,England
    MS-Off Ver
    Excel 2003
    Posts
    78

    Collating data from varying number of sheets

    Hi ppl,

    Right I am loading data in to seperate sheets in to excel, with the sheet names being the file names where the data came from (each file has a sheet within called "dump" which collates all data needing to be reviewed and is then transferred over to my "analysis" workbook). The number of sheets will vary as will the names of the sheets.

    The sheets follow a general format in that their names are made up of the team they come from and also the week commencing date the data was recorded for e.g.

    FR1-23.11.2009
    RA12-23.11.2009
    Etc for a possible 24 teams.


    I want to carry out analysis on the data listed in these files or, to at least collate the data in to more meaningful tables in a new sheet. I wish to have all of this automated in some way so when sheets are loaded a table can begin to be populated.

    What is the best way of doing this and how? Once the general table containing data from all sheets is produced I will just simply carry out my further calculations from this.

    Thank you for any help and ideas.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Collating data from varying number of sheets

    Hi munkee
    From what you've described, I believe this code can be modified to create the merged file for your further analysis
    Option Explicit
    Dim sFileName As String
    
    '********************************************************
    'Reference to Microsoft Scripting Runtime must be set
    'Tools->References-> select Microsoft Scripting Runtime
    '********************************************************
    
    Public Sub ImportData()
        Dim wbThis As Workbook              'the target workbook
        Dim wswbThis As Worksheet           'the target workbook sheets
        Dim wb2 As Workbook                 'the source workbooks
        Dim wsWb2 As Worksheet              'the source workbook sheets
        Dim fso As New FileSystemObject     'create file sys object
        Dim myFolder As Folder              'name of file sys object
        Dim myFile As File                  'files being imported
        Dim myPath As String                'path of the target workbook
    
        MsgBox "Select The File You Wish To Import To" & vbCrLf & _
        "Target & Source Files Must Be In Same Folder"
        
        Call RetrieveFileName
        If sFileName = "False" Then Exit Sub
    
        myPath = ActiveWorkbook.Path
        Set myFolder = fso.GetFolder(myPath)
        Set wbThis = ActiveWorkbook
    
        Application.ScreenUpdating = False
        For Each myFile In myFolder.Files    'loop through the files in the folder
            If myFile.Name <> "MacroBook.xls" And myFile.Name <> wbThis.Name _
               And myFile.Type Like "*" & "Excel" & "*" Then  'include only excel files
                Workbooks.Open myFile, UpdateLinks:=False
    
                Set wb2 = ActiveWorkbook    'make found file active
                For Each wswbThis In wbThis.Sheets
                    For Each wsWb2 In wb2.Sheets    'loop thru it's sheets
                        If wsWb2.Name = wswbThis.Name Then    'find a sheetname match
                            ' insert your code here
                            ' insert your code here
                            ' insert your code here
                            ' insert your code here
                        Else
                        End If
                    Next
                Next
                Workbooks(myFile.Name).Close False    'saves source files without change
            End If
        Next myFile
        Application.ScreenUpdating = True
    End Sub
    
    Sub RetrieveFileName()
        sFileName = Application.GetOpenFilename
        If sFileName = "False" Then Exit Sub
        Workbooks.Open sFileName
    End Sub
    If you need assistance in modifying the code, attach a couple of sample source files and an example of what the merged data is to look like. I'll be glad to look at it with you.
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

+ Reply to Thread

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