• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Copy data from multiple files in a single master file

Hello all,

I am new to VBA macro hence not able to build the code to copy multiple data from multiple worksheets(16) into 1.

I have a RAID log and want to copy all the data form the worksheet 'Risk Log' in each of the 16 workbooks and paste in a master file. The data should be pasted such that it doesn't override any data from the other workbooks. Is it possible? Please guide. Thanks for the help in advance!

Attaching one of the workbooks and master file.
 

Attachments

  • RAID Log WS01.xlsx
    70.8 KB · Views: 10
  • Master.xlsx
    8.3 KB · Views: 8
It's most often asked query. Pls have a search for the same by search box located at top right corner .
 
It's most often asked query. Pls have a search for the same by search box located at top right corner .
Hi Deepak,

I have been searching the same since yesterday but could not find exact match to my problem. I have even gone through several threads where you have provided solution. If you could help me with this one?
 
Hi !

Attach a second source workbook and a result workbook filled out
according to both source workbooks …
And you must explain where workbooks are saved on hard disk,
their real names, and so on … That needs a complete description !
 
Check this...

Code:
Sub merge_files()
Dim myfolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False:    .Show
        On Error Resume Next: myfolder = .SelectedItems(1): Err.Clear: On Error GoTo 0
End With

If Not myfolder <> "" Then Exit Sub
    merge_xl_V_1 myfolder, "*.xlsx", "Risk Log", "Table1"

End Sub


Sub merge_xl_V_1(strsoucefolder As String, xlfiles As String, sheetname As String, copyrnage As String)
Dim strfiles As String, openworkbook As Workbook, copy_rows As Integer, copy_rng As Range
Dim s As Integer: s = 2

If Right(strsoucefolder, 1) <> "\" Then strsoucefolder = strsoucefolder & "\"
strfiles = Dir(strsoucefolder & xlfiles)
If strfiles = "" Then MsgBox "No XL Files found!!!", vbCritical: Exit Sub

Application.ScreenUpdating = False
    Do While strfiles <> ""
        Set openworkbook = Workbooks.Open(strsoucefolder & strfiles)
    
        With openworkbook.Sheets(sheetname).Range(copyrnage)
            copy_rows = WorksheetFunction.CountA(.Columns(2))
                Set copy_rng = .Resize(copy_rows)
        End With
    
            copy_rng.Copy
    
            With ThisWorkbook.ActiveSheet.Cells(s, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            Application.CutCopyMode = False
      openworkbook.Close 0
  
      s = s + copy_rows
    strfiles = Dir
    Loop
Application.ScreenUpdating = True

End Sub
 
Hi !

Attach a second source workbook and a result workbook filled out
according to both source workbooks …
And you must explain where workbooks are saved on hard disk,
their real names, and so on … That needs a complete description !


Hi Mark,

The name is same as mentioned, I am attaching the master updated with result and second sheet. Path is 'C:\Users\vaibhavtandon\Desktop\RAID - Copy'.
The row until where data is filled needs to be copied to the master
I hope this would now answer your query.Please let me know in case I have missed anything
 

Attachments

  • Master.xlsx
    10.6 KB · Views: 10
  • RAID Log WS02.xlsx
    67.6 KB · Views: 9
Check this...

Code:
Sub merge_files()
Dim myfolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False:    .Show
        On Error Resume Next: myfolder = .SelectedItems(1): Err.Clear: On Error GoTo 0
End With

If Not myfolder <> "" Then Exit Sub
    merge_xl_V_1 myfolder, "*.xlsx", "Risk Log", "Table1"

End Sub


Sub merge_xl_V_1(strsoucefolder As String, xlfiles As String, sheetname As String, copyrnage As String)
Dim strfiles As String, openworkbook As Workbook, copy_rows As Integer, copy_rng As Range
Dim s As Integer: s = 2

If Right(strsoucefolder, 1) <> "\" Then strsoucefolder = strsoucefolder & "\"
strfiles = Dir(strsoucefolder & xlfiles)
If strfiles = "" Then MsgBox "No XL Files found!!!", vbCritical: Exit Sub

Application.ScreenUpdating = False
    Do While strfiles <> ""
        Set openworkbook = Workbooks.Open(strsoucefolder & strfiles)
    
        With openworkbook.Sheets(sheetname).Range(copyrnage)
            copy_rows = WorksheetFunction.CountA(.Columns(2))
                Set copy_rng = .Resize(copy_rows)
        End With
    
            copy_rng.Copy
    
            With ThisWorkbook.ActiveSheet.Cells(s, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            Application.CutCopyMode = False
      openworkbook.Close 0
  
      s = s + copy_rows
    strfiles = Dir
    Loop
Application.ScreenUpdating = True

End Sub
Hi Deepak, Thanks for the code.I have pasted this in the master file and its asking me to open the folder, once I reach the folder where I have all the files to be copied message comes - No items match your search. Am I doing it correctly? In query to Marc's message I have also posted the path of the files.

Thanks Again!
Cheers!
 
vaibhav tandon

I didn't find any issue with the same. as you have shared the location so just change the below & try!

Code:
Sub merge_files()
    merge_xl_V_1 "C:\Users\vaibhavtandon\Desktop\RAID - Copy", "*.xlsx", "Risk Log", "Table1"
End Sub
 
vaibhav tandon

I didn't find any issue with the same. as you have shared the location so just change the below & try!

Code:
Sub merge_files()
    merge_xl_V_1 "C:\Users\vaibhavtandon\Desktop\RAID - Copy", "*.xlsx", "Risk Log", "Table1"
End Sub
thanks for the updated code.
Once I run this updated code, it gives me a prompt - "Master.xlsx is already open.Reopening will cause changes you made to be discarded, do you wish to continue. IF I click yes then everything disappers if I select No then I get error - Method ' Open' of the object 'Workbooks failed' .
 
Hi,

xlsx doesn't store the macro so you need to save it as xlsb/xlsm/xls

however check the attached!

I have also optimized the code for this silly mistake.
 

Attachments

  • Master_1.xlsb
    13.9 KB · Views: 9
Meanwhile you may try after changing below line.

Code:
merge_xl_V_1 "C:\Users\vaibhavtandon\Desktop\RAID - Copy", "*.xlsx", "Risk Log", "C7:T2000"
 
You got the error as there was a file named sample in that folder which don't have the said sheet.

Just paste the attached file in same folder & check.
 

Attachments

  • Master_2.xlsb
    18.1 KB · Views: 7
T
You got the error as there was a file named sample in that folder which don't have the said sheet.

Just paste the attached file in same folder & check.
Thanks Deepak! this indeed is working great! I added a new file to see if it is taking all the new values in new file and indeed its working great without any issues. Since I am novice in VBA, could you please guide me how this file works or provide some instructions on how this code works so that if I have to make some minor changes I am able to do so?

Thanks a ton for the help above , you are doing a great job by helping new users like us!
 
vaibhav tandon

I have added some comments.

let me know if any further support is req!!


Code:
Sub merge_files()

'In this subroutine there are our inputs required
' path- where all xls are stoted [ThisWorkbook.Path - all are in same folder]
'xl file type - which excel's are to process as there are several xl format depending on the req, so that in which format data u have
'write each sheet name with seprated by "|", [A|B|C|D]
'write relevent sheet data range & same order
    merge_xl_V_2 ThisWorkbook.Path, "*.xlsx", "Risk Log|Issue Log", "C8:T2000|C10:Q2000"
End Sub

Sub merge_xl_V_2(strsoucefolder As String, xlfiles As String, sheetname As String, copyrnage As String)

'Declare variable
Dim strfiles As String, openworkbook As Workbook, copy_rows As Integer, copy_rng As Range, myXL As String

'As sheet name & range are in a array type string so get it splitted
Dim strsheet1 As String: strsheet1 = Split(sheetname, "|")(0)
Dim strsheet2 As String: strsheet2 = Split(sheetname, "|")(1)
Dim rng1 As String: rng1 = Split(copyrnage, "|")(0)
Dim rng2 As String: rng2 = Split(copyrnage, "|")(1)


'first row where to paste,two variable for two sheet as both have diff ranges
Dim s As Integer: s = 2
Dim s1 As Integer: s1 = 2

'Check & add path sep
If Right(strsoucefolder, 1) <> "\" Then strsoucefolder = strsoucefolder & "\"

'check that in given path Excel's with asked extention are available or not
strfiles = Dir(strsoucefolder & xlfiles)
If strfiles = "" Then MsgBox "No XL Files found!!!", vbCritical: Exit Sub

'turn off screen updating to fast the process
Application.ScreenUpdating = False
'strat loop for each xl
    Do While strfiles <> ""
       
        'skip thisworkbook
        myXL = strsoucefolder & strfiles
        If Not myXL <> ThisWorkbook.Name Then GoTo L2
        'open xl
        Set openworkbook = Workbooks.Open(myXL)
       
        'check if it have the asked sheet
        If Evaluate("ISREF('" & strsheet1 & "'!A1)") Then
       
            With openworkbook.Sheets(strsheet1).Range(rng1)
            'how many rows to be copied, here i have taken it from col D which is just 2nd col of the range C8:T2000
                copy_rows = WorksheetFunction.CountA(.Columns(2))
                'set range to copy
                    Set copy_rng = .Resize(copy_rows)
            End With
            copy_rng.Copy
            'paste the range with format too
            With ThisWorkbook.Sheets(strsheet1).Cells(s, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            'clear clipboard
            Application.CutCopyMode = False
        End If
        'same thing for another sheet
      If Evaluate("ISREF('" & strsheet2 & "'!A1)") Then
            With openworkbook.Sheets(strsheet2).Range(rng2)
            'how many rows to be copied, here i have taken it from col K which is just 9th col of the range C10:Q2000
                copy_rows1 = WorksheetFunction.CountA(.Columns(9))
                    Set copy_rng = .Resize(copy_rows1)
            End With
            copy_rng.Copy
            With ThisWorkbook.Sheets(strsheet2).Cells(s1, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            Application.CutCopyMode = False
        End If
      'close the workbook without save
      openworkbook.Close 0
      'change, now where to copy from next file
      s = s + copy_rows: s1 = s1 + copy_rows1
L2:
'loop to another file
    strfiles = Dir
    Loop
    'strat screen update
Application.ScreenUpdating = True

End Sub
 
vaibhav tandon

I have added some comments.

let me know if any further support is req!!


Code:
Sub merge_files()

'In this subroutine there are our inputs required
' path- where all xls are stoted [ThisWorkbook.Path - all are in same folder]
'xl file type - which excel's are to process as there are several xl format depending on the req, so that in which format data u have
'write each sheet name with seprated by "|", [A|B|C|D]
'write relevent sheet data range & same order
    merge_xl_V_2 ThisWorkbook.Path, "*.xlsx", "Risk Log|Issue Log", "C8:T2000|C10:Q2000"
End Sub

Sub merge_xl_V_2(strsoucefolder As String, xlfiles As String, sheetname As String, copyrnage As String)

'Declare variable
Dim strfiles As String, openworkbook As Workbook, copy_rows As Integer, copy_rng As Range, myXL As String

'As sheet name & range are in a array type string so get it splitted
Dim strsheet1 As String: strsheet1 = Split(sheetname, "|")(0)
Dim strsheet2 As String: strsheet2 = Split(sheetname, "|")(1)
Dim rng1 As String: rng1 = Split(copyrnage, "|")(0)
Dim rng2 As String: rng2 = Split(copyrnage, "|")(1)


'first row where to paste,two variable for two sheet as both have diff ranges
Dim s As Integer: s = 2
Dim s1 As Integer: s1 = 2

'Check & add path sep
If Right(strsoucefolder, 1) <> "\" Then strsoucefolder = strsoucefolder & "\"

'check that in given path Excel's with asked extention are available or not
strfiles = Dir(strsoucefolder & xlfiles)
If strfiles = "" Then MsgBox "No XL Files found!!!", vbCritical: Exit Sub

'turn off screen updating to fast the process
Application.ScreenUpdating = False
'strat loop for each xl
    Do While strfiles <> ""
      
        'skip thisworkbook
        myXL = strsoucefolder & strfiles
        If Not myXL <> ThisWorkbook.Name Then GoTo L2
        'open xl
        Set openworkbook = Workbooks.Open(myXL)
      
        'check if it have the asked sheet
        If Evaluate("ISREF('" & strsheet1 & "'!A1)") Then
      
            With openworkbook.Sheets(strsheet1).Range(rng1)
            'how many rows to be copied, here i have taken it from col D which is just 2nd col of the range C8:T2000
                copy_rows = WorksheetFunction.CountA(.Columns(2))
                'set range to copy
                    Set copy_rng = .Resize(copy_rows)
            End With
            copy_rng.Copy
            'paste the range with format too
            With ThisWorkbook.Sheets(strsheet1).Cells(s, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            'clear clipboard
            Application.CutCopyMode = False
        End If
        'same thing for another sheet
      If Evaluate("ISREF('" & strsheet2 & "'!A1)") Then
            With openworkbook.Sheets(strsheet2).Range(rng2)
            'how many rows to be copied, here i have taken it from col K which is just 9th col of the range C10:Q2000
                copy_rows1 = WorksheetFunction.CountA(.Columns(9))
                    Set copy_rng = .Resize(copy_rows1)
            End With
            copy_rng.Copy
            With ThisWorkbook.Sheets(strsheet2).Cells(s1, 1)
                .PasteSpecial xlPasteFormats
                    .PasteSpecial xlPasteValues
            End With
            Application.CutCopyMode = False
        End If
      'close the workbook without save
      openworkbook.Close 0
      'change, now where to copy from next file
      s = s + copy_rows: s1 = s1 + copy_rows1
L2:
'loop to another file
    strfiles = Dir
    Loop
    'strat screen update
Application.ScreenUpdating = True

End Sub
This indeed is helpful Deepak! in case I need further help should I respond on this thread or is there any alternative as per the blog rules?
 
You may write here!
Hi Deepak,

I wanted to populate similarly for 2 tabs - ASsumption and Dependency log and tried building the code with the help instructions but not able to do so. I am stuck at the logic of the copy paste of the files where the code starts " 'how many rows to be copied, here i have taken it from col D which is just 2nd col of the range C8:T2000"

I am attaching till where I have built the modified code to copy and paste but not able to complete it, If you could help me?
 

Attachments

  • Macro explanation - Copy.txt
    3.9 KB · Views: 4
Back
Top