+ Reply to Thread
Results 1 to 6 of 6

VBA to copy data from multiple workbooks to Master Workbook

Hybrid View

wotsup VBA to copy data from... 02-23-2012, 08:29 AM
arlu1201 Re: VBA to copy data from... 02-23-2012, 09:03 AM
wotsup Re: VBA to copy data from... 02-23-2012, 09:22 AM
arlu1201 Re: VBA to copy data from... 02-24-2012, 02:49 PM
wotsup Re: VBA to copy data from... 03-02-2012, 05:18 PM
arlu1201 Re: VBA to copy data from... 03-05-2012, 09:31 AM
  1. #1
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    VBA to copy data from multiple workbooks to Master Workbook

    I have 4 workbooks which each contain a sales log. 1 is a Master Log and 3 are maintained by separate users. I am looking for the easiest way to update the Master Log with the data from the other 3 workbooks that is fast, user friendly and avoids data being corrupted by the users.

    Process:-

    On running Macro open alert with information - ok / cancel
    Go to "Sales Log" unprotect sheet, show all, clear all filters, sort Range ("B3:B100") ascending
    Open Sales Person 1 Workbook, go to 'Sales Log", unprotect sheet
    Select "Range1" sort by ("B3:B100") Copy "Range1"
    Paste Range1 data (values only) in Master Log Range1
    SaveAs Sales Person 1 [date and time] .xlsm and Close [no alerts]
    Repeat for Sales Person 2 and 3 Workbook
    Master Log: Select "RangeALL" sort by ("H3:H100")
    SaveAs Master Log [date and time] .xlsm and Close [no alerts] Attributes: Read Only
    Close excel application

    Option Explicit
    Dim Master As Workbook
    Dim sourceBook As Workbook
    Dim sourceData As Worksheet
    Dim CurrentFileName As String
    Dim myPath As String
    Dim frow As Long
    Dim lrow As Long
    
    Private Sub update_master()
    '
    
    'Open an Alert that I can display information with OK or Cancel.
    
    'Prepare This Workbook: On macro start, unprotect sheet, sort Range ("B3:B100") ascending
    
    'The folder containing the files to be recap'd
    myPath = "C:\Test"
    
    'Finds the name of the first file of type .xlsm in the current directory
    CurrentFileName = Dir(myPath & "\*.xlsm")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    'With each file: On open, unprotect sheet, sort Range ("B3:B100") ascending before copying range
    
    Do
    Workbooks.Open (myPath & "\" & CurrentFileName)
    Set sourceBook = Workbooks(CurrentFileName)
    Set sourceData = sourceBook.Worksheets("SALES LOG")
    
    With sourceData
    If Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson1" Then
    Call PreparetoCopy
    .Range("RANGE1").Copy
    Master.Worksheets("SALES LOG").Range("RANGE1").PasteSpecial , Paste:=xlValues
    ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson2" Then
    Call PreparetoCopy
    .Range("RANGE2").Copy
    Master.Worksheets("SALES LOG").Range("RANGE2").PasteSpecial , Paste:=xlValues
    ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson3" Then
    Call PreparetoCopy
    .Range("RANGE3").Copy
    Master.Worksheets("SALES LOG").Range("RANGE3").PasteSpecial , Paste:=xlValues
    End If
    
    End With
    
    sourceBook.Close
    
    'Calling DIR w/o argument finds the next .xlsm file within the current directory.
    CurrentFileName = Dir()
    Loop While CurrentFileName <> ""
    
    'With current workbook Select Range ("RANGE_ALL") and sort ascending by Range("H3:H100").
    
    'Save This Workbook: File Name, Current Date and Time. Ignore Alerts. Attributes=Read Only
    ThisWorkbook.Save
    
    'Save current workbook File Name, Current Date and Time. Ignore Alerts not working.
    
    Application.DisplayAlerts = False
    
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG1.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG2.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG3.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
    
    ThisWorkbook.Close
    
    'Close Excel Application.
    
    End Sub
    Last edited by wotsup; 02-23-2012 at 09:24 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Hey Wotsup - Please put your code within code tags (as per forum rules)
    Also, you have not explained anything here. Since i have worked with you on a similar thread, i know the requirement, but others will not. Please explain what you require.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Thanks Arlette

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    All code changes given in bold.
    Option Explicit
    Dim Master As Workbook
    Dim sourceBook As Workbook
    Dim sourceData As Worksheet
    Dim CurrentFileName As String
    Dim myPath As String
    Dim frow As Long
    Dim lrow As Long
     Dim FName as string 
    
    Private Sub update_master()
    '
    
    'Open an Alert that I can display information with OK or Cancel.
    Msgbox "Your message here", vbokcancel 
    
    'Prepare This Workbook: On macro start, unprotect sheet, sort Range ("B3:B100") ascending
    worksheets("SALES LOG").UnProtect Password:="456"
    Columns("B:B").Select
        ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Add Key:=Range("B3:B100") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("SALES LOG").Sort
            .SetRange Range("B:B")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    'The folder containing the files to be recap'd
    myPath = "C:\Test"
    
    'Finds the name of the first file of type .xlsm in the current directory
    CurrentFileName = Dir(myPath & "\*.xlsm")
    
    'Create a workbook for the recap report
    Set Master = ThisWorkbook
    
    'With each file: On open, unprotect sheet, sort Range ("B3:B100") ascending before copying range
    
    Do
    Workbooks.Open (myPath & "\" & CurrentFileName)
    Set sourceBook = Workbooks(CurrentFileName)
    Set sourceData = sourceBook.Worksheets("SALES LOG")
    
    With sourceData
     .Unprotect Password:="456"
    .Columns("B:B").Select
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=Range("B3:B100") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With sourcedata.Sort
            .SetRange Range("B:B")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With 
    
    If Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson1" Then
    Call PreparetoCopy
    .Range("RANGE1").Copy
    Master.Worksheets("SALES LOG").Range("RANGE1").PasteSpecial , Paste:=xlValues
    ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson2" Then
    Call PreparetoCopy
    .Range("RANGE2").Copy
    Master.Worksheets("SALES LOG").Range("RANGE2").PasteSpecial , Paste:=xlValues
    ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson3" Then
    Call PreparetoCopy
    .Range("RANGE3").Copy
    Master.Worksheets("SALES LOG").Range("RANGE3").PasteSpecial , Paste:=xlValues
    End If
    
    End With
    
    sourceBook.Close
    
    'Calling DIR w/o argument finds the next .xlsm file within the current directory.
    CurrentFileName = Dir()
    Loop While CurrentFileName <> ""
    
    'With current workbook Select Range ("RANGE_ALL") and sort ascending by Range("H3:H100").
     Activeworkbook.worksheets("SALES LOG").range("RANGE_ALL").Select
        ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Add Key:=Range("HB3:H100") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("SALES LOG").Sort
            .SetRange Range("H:H")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    'Save This Workbook: File Name, Current Date and Time. Ignore Alerts. Attributes=Read Only
    Fname=date & " " & time
    ThisWorkbook.SaveAs Filename:=Fname, Fileformat:=xlopenxlmworkbook, createbackup:=false
    
    'Save current workbook File Name, Current Date and Time. Ignore Alerts not working.
    
    Application.DisplayAlerts = False
    
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG1.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG2.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG3.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
    
    
    ThisWorkbook.Close
    
    'Close Excel Application.
    
    End Sub

  5. #5
    Registered User
    Join Date
    02-13-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Hi Arlette... this code is now not calling up the other files. It sorts the master then re-opens it. None of the other files are opened. Can you take another look please?

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA to copy data from multiple workbooks to Master Workbook

    Do you have a file that you can upload so i can test it? Otherwise, its a lil difficult to find out the bugs.

+ 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