icomefromchaos
New Member
- Joined
- Nov 21, 2014
- Messages
- 14
So far the code loops through all of the files, but only copies sheet 1 instead of making the combined sheet. It also creates hundreds of blank sheets in my new workbook that only has 1 combined tab that keeps looping the same information from the first file in my directory.
The end Goal of this Macro is to copy all of the information that is in all of my workbooks, copy it to a tab that is called 'Combined' and then copy the 'Combined' sheet and then place it in my Target workbook. The Target workbook will house all of the information from 300 workbooks in one tab. For example, Workbook 1 has Sheet 1, sheet 2... sheets 40The selection of code below combines sheets 1...40 into a new sheet called 'Combined'
Code:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; white-space: inherit; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Sub BatchProcessing()
MyPath = "C:\Users\USERNAME\Desktop\OCCREPORTS\Files\"
MyTemplate = "*.xls*" ' Set the template.
MyName = Dir(MyPath & MyTemplate) 'Retrieve the first file
Do While MyName <> ""
Workbooks.Open MyPath & MyName
Combine 'do your thing
Workbooks(MyName).Close 'close
MyName = Dir 'Get next file
Loop
End Sub
Sub Combine()
Dim J As Integer
Dim s As Worksheet
Dim LastCol As Integer
On Error Resume Next
Sheets(1).Select
Worksheets.Add ' add a sheet in first place
Sheets(1).Name = "Combined"
For Each s In ActiveWorkbook.Sheets
If s.Name <> "Combined" Then
Application.Goto Sheets(s.Name).[A1]
Selection.CurrentRegion.Select
Sheet.UsedRange.Clear
LastCol = Sheets("Combined").Cells(1, Columns.Count).End(xlToLeft).Column
Selection.Copy Destination:=Sheets("Combined"). _
Cells(1, LastCol + 1)
End If
Next
End Sub
</code>
The end Goal of this Macro is to copy all of the information that is in all of my workbooks, copy it to a tab that is called 'Combined' and then copy the 'Combined' sheet and then place it in my Target workbook. The Target workbook will house all of the information from 300 workbooks in one tab. For example, Workbook 1 has Sheet 1, sheet 2... sheets 40The selection of code below combines sheets 1...40 into a new sheet called 'Combined'
Code:
'Selection.CurrentRegion.Select
'Sheet.UsedRange.Clear
'LastCol = Sheets("Combined").Cells(1, Columns.Count).End(xlToLeft).Column
'Selection.Copy Destination:=Sheets("Combined"). _
'Cells(1, LastCol + 1)From there I need to read the other workbooks, loop the code above and then paste the combined sheet into Sheet 1 of the Target workbook.
Last edited: