+ Reply to Thread
Results 1 to 24 of 24

VBA script for collation of data

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2008
    Posts
    12

    VBA script for collation of data

    Hi guys :
    I've been working on a requirement that involves, collating data from multiple sales reps and consolidating them into one file. Once this collation is done, we would be using this worksheet to generate reports . This is required on a monthly basis
    I managed to find some code on the web , that helps me collate the data, but need help in these two aspects :
    1) I'm able to collate the data from multiple sheets (it is appended in the same worksheet), but i loose the header in the master sheet. The header will be in the same lines as
    Sl No Sales Rep Customer Product Month Unit Price Qty
    How can i ensure that the master has this header and the collation of data starts after their headers.

    2) On running of the macro, all files open up and remains open after the data is collected on the master. How can i close the sheets after the master has been populated with data.

    3) Since the data is collected from multiple sheets, each users data have their line items numbered, but in the master sheet, we find that the numbering (Sl no) is not consistnt. How can i have a sequential numbering in column A for all data.

    Would appreciate any inputs , pointers in this regard...

    TIA.

    Sushesh

    The code that i use is :

    Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
         
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
             
            On Error Resume Next
             
            Set wbThis = ThisWorkbook
             'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
            uRng.Columns.Count).Clear
            With .FileSearch
                .NewSearch
                 'Change path to suit
                .LookIn = "C:\Test"
                .FileType = msoFileTypeExcelWorkbooks
                 
                If .Execute > 0 Then 'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count ' Loop through all.
                         'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.Worksheets(1).UsedRange
                        Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
                        rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                        rToCopy.Columns.Count).Copy rNextCl
                      
                        
                    Next lCount
                Else:  MsgBox "No workbooks found"
                End If
            End With
             
             
            On Error GoTo 0
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub
    Last edited by VBA Noob; 05-09-2008 at 01:25 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The example code that you have used copies data from several workbooks to one. It starts by clearing the data that exists in the master sheet, leaving header rows, it then copies the data without header rows. So if you have no data in the sheet the first copy must import the header rows. Do this by checking for data in the master sheet & setting a Boolean variable. The following cod should work but I haven't tested it.
    Option Explicit
    
    Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If uRng.Cells.Count <= 1 Then
                'no data in master sheet
                bHeaders = False
                GoTo search
            End If
            uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
                                     uRng.Columns.Count).Clear
    search:
            With .FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "C:\Test"
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.Worksheets(1).UsedRange
                        Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
                        If bHeaders Then
                            'headers exist so don't copy
                            rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                        rToCopy.Columns.Count).Copy rNextCl
                            'no headers so copy
                        Else: rToCopy.Copy rNextCl
                            bHeaders = True
                            End If
                        Next lCount
                    Else: MsgBox "No workbooks found"
                    End If
                End With
    
    
                On Error GoTo 0
                .ScreenUpdating = True
                .DisplayAlerts = True
                .EnableEvents = True
            End With
     End Sub
    Your second question should be in a separate thread! However, if you use a formula to set the sequential numbering in each workbook based on the ROW() function, this would update when the new workbook calculates.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    05-08-2008
    Posts
    12

    Some more tweaking required.

    Roy,
    I tried this out, I use a blank excel worksheet, add the code in VBA. When i run the macro, the data gets collated.

    How do i set that the data should start getting appended , from row 3 or 4. with row 2 being the headers.

    2) This problem still persists,all files are opened and data is copied into the master document. After the copying, the files remain open.. If the number of documents are more, then it may cause a problem. Can you help me tweak the code so that it closes the file after it copies the data ?

    3) On the row numbers, i will work on it and confirm.


    I really appreciate your time on this ! Thanks,
    Sushesh

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this
    
    Option Explicit
    
    Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If uRng.Cells.Count <= 1 Then
                'no data in master sheet
                bHeaders = False
                GoTo search
            End If
            uRng.Offset(1, 0).Resize(uRng.Rows.Count - 1, _
                                     uRng.Columns.Count).Clear
    search:
            With .FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "C:\Test"
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.Worksheets(1).UsedRange
                        Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
                        If bHeaders Then
                            'headers exist so don't copy
                            rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                        rToCopy.Columns.Count).Copy rNextCl
                            'no headers so copy
                            'place headers in Row 2
                        Else: rToCopy.Copy Cells(2, 1)
                            bHeaders = True
                        End If
                        wbSource.Close False     'close source workbook
                    Next lCount
                Else: MsgBox "No workbooks found"
                End If
            End With
    
    
            On Error GoTo 0
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub

  5. #5
    Registered User
    Join Date
    05-08-2008
    Posts
    12

    That's excellent !

    Roy,
    It worked great !!
    Headers get copied onto the master and if there is a header then just the data gets copied. Also, the files are closed after the import of data.

    One last thing.. there is a blank row between the data from different sheets.. Could you help me out removing this and making the data range in the master continuous ?

    Thanks !!

    Sushesh

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    If you try to follow the code you will see that this line skips a row

     Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(2, 0)
    So change it to

     Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can zip the4 files together then use www.yousendit.com, post the download link here

  8. #8
    Registered User
    Join Date
    05-08-2008
    Posts
    12
    http://www.yousendit.com/download/bV...R3NCMTZGa1E9PQ

    Master file has the macro. a,b,c are the input files

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I cannot open any of these files .

  10. #10
    Registered User
    Join Date
    05-08-2008
    Posts
    12
    hmm.. can't see where the problem is , just downloaded the zip file from the link posted up and i was able to open all the files..

    Sushesh

  11. #11
    Registered User
    Join Date
    05-08-2008
    Posts
    12
    Let me put the contents of the three files here..all files start from A2.
    file a.xls
    S.No Sales Team Customer Name Part Sept
    1 Maruth ABC 23 7000
    2 Maruth ABC 22 2000
    3 Maruth AC 42 20000
    4 Maruth AD 43 20000
    5 Maruth FD 55 50000
    6 Maruth GG 22 100000
    7 Maruth GS 55 25000
    8 Maruth BB 44 7000
    9 Maruth BB 52 2000
    10 Maruth SS 45 20000
    11 Maruth NK 33 20000
    12 Maruth NK 22 50
    13 Maruth NK 44 50
    14 Maruth NK 55 7000
    15 Maruth NK 55 2000
    16 Maruth NK 55 20000
    17 Maruth NK 55 20000

    file b.xls
    S.No Sales Team Customer Name Part Sept
    1 Pawan ABC 23 7000
    2 Pawan ABC 22 2000
    3 Pawan AC 42 20000
    4 Pawan AD 43 20000
    5 Pawan FD 55 50000
    6 Pawan GG 22 100000
    7 Pawan GS 55 25000
    8 Pawan BB 44 7000
    9 Pawan BB 52 2000
    10 Pawan SS 45 20000
    11 Pawan NK 33 20000
    12 Pawan NK 22 50
    13 Pawan NK 44 50
    14 Pawan NK 55 7000
    15 Pawan NK 55 2000
    16 Pawan NK 55 20000
    17 Pawan NK 55 20000

    file c.xls
    S.No Sales Team Customer Name Part Sept
    1 John ABC 23 20000
    2 John ABC 22 20000
    3 John AC 42 50000
    4 John AD 43 100000
    5 John FD 55 50000
    6 John GG 22 100000
    7 John GS 55 50000
    8 John BB 44 100000
    9 John BB 52 50000
    10 John SS 45 100000
    11 John NK 33 50000
    12 John NK 22 100000
    13 John NK 44 50000
    14 John NK 55 100000
    15 John NK 55 50000
    16 John NK 55 100000
    17 John NK 55 50000

    and the code that is used in the master sheet..
    Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If uRng.Cells.Count <= 1 Then
                'no data in master sheet
                bHeaders = False
                GoTo search
            End If
            uRng.Offset(2, 0).Resize(uRng.Rows.Count - 1, _
                                     uRng.Columns.Count).Clear
    search:
            With .FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "C:\Test"
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.Worksheets(1).UsedRange
                        Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                        If bHeaders Then
                            'headers exist so don't copy
                            rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                        rToCopy.Columns.Count).Copy rNextCl
                            'no headers so copy
                            'place headers in Row 2
                        Else: rToCopy.Copy Cells(2, 1)
                            bHeaders = True
                        End If
                        wbSource.Close False     'close source workbook
                    Next lCount
                Else: MsgBox "No workbooks found"
                End If
            End With
    
    
            On Error GoTo 0
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub

  12. #12
    Registered User
    Join Date
    05-08-2008
    Posts
    12
    Hi roy,
    Did you find the data sufficient to work on ?

    Thanks,
    Sushesh

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I'm looking at it now

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The code works exactly as I expect. You haven't placed the master workbook in the same folder as the data workbooks?

    This is the code that I have ended up with

    Option Explicit
    
    Sub Get_Value_From_All()
        Dim wbSource As Workbook
        Dim wbThis As Workbook
        Dim rToCopy As Range
        Dim uRng   As Range
        Dim rNextCl As Range
        Dim lCount As Long
        Dim bHeaders As Boolean
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            'clear the range except  headers
            Set uRng = wbThis.Worksheets(1).UsedRange
            If Application.WorksheetFunction.CountA(uRng) = 0 Then
                'no data in master sheet
                bHeaders = False
            End If
            With .FileSearch
                .NewSearch
                'Change path to suit
                .LookIn = "C:\Test"
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count   ' Loop through all.
                        'Open Workbook x and Set a Workbook  variable to it
                        Set wbSource = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                        Set rToCopy = wbSource.Worksheets(1).UsedRange
                        Set rNextCl = wbThis.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
                            If bHeaders Then
                            'headers exist so don't copy headers
                            Set rToCopy = rToCopy.Offset(1, 0).Resize(rToCopy.Rows.Count - 1, _
                                                                      rToCopy.Columns.Count)
                        End If
                        rToCopy.Copy rNextCl
                        wbSource.Close False     'close source workbook
                        bHeaders = True
                    Next lCount
                Else: MsgBox "No workbooks found"
                End If
            End With
    
    
            On Error GoTo 0
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With
    End Sub

  15. #15
    Registered User
    Join Date
    05-08-2008
    Posts
    12
    Roy,
    Previously i was placing the input files in C:\test\ and the master file on the desktop. It worked fine other than the problems that i had mentioned.

    Today with the code u just posted up, when i run the macro, it just freezes the file and after a few mins, it throws up a blank file. (this is with the master and the input files in the same folder)

    When i tried the same with the master file on the desktop and the input files in a diff folder, it did freeze for a few mins but it collated the data from all three files, but with the headers repeated thrice. So one problem solved,ie data gets collated from all 'n' files.

    any specific changes u made to the macro ?

    Anything that i'm doing wrong ?

    Sushesh

  16. #16
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The master file should not be in the same file as the data files.

    The code that I posted last copies all the data for me with only one header row.

    I couldn't open your files so I created my own, just 3 files with the same layout containing different data.

  17. #17
    Registered User
    Join Date
    10-23-2008
    Location
    Virginia, US
    Posts
    3

    Collating specific columns from multiple workbooks

    I am relatively new to writing macros, and have been developing a macro to collate some data sets. In searching for people working on similar macros, I came across your proposed solution above, and have had some success modifying it to overcome the trivial issues, unique to my data set (i.e. 2 row header, etc.).

    What I am looking to achieve with the macro I'm working on is to only copy particular columns from each data set, and I have been having some problems. When modifying the code provided above to copy a defined range (i.e. Range("B:C,G:G,I:I,L:O"), as opposed to all the data (usedrange), I am finding the macro only copies data from the first file. The macro loops through all of the available files, but does not copy anything beyond the first set. I'm not sure if there is something inherent in the "usedrange" property that makes this macro work, or if I'm just overlooking another issue. I have exhausted my knowledge of macros to this point, and decided to come back to the source to see if you had any expertise/suggestions you could share.

    Is modifying this macro the way to go, or should I be starting from a different direction!?
    Last edited by matenginerd; 10-23-2008 at 09:32 AM. Reason: poor grammar...lol

  18. #18
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Please ask your own question, with a link to this one. This is covered in the Forum Rules.

+ 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