Doghouse308
New Member
- Joined
- May 28, 2019
- Messages
- 13
I am trying to open all of the files that meet a search criteria and are in a specified folder. I want to copy and paste the contents of certain worksheets into one worksheet that will contain the combined data of the sheets. I had it working earlier and now it is copying the contents of each worksheet into the correct work sheet in the combined file, but the data in the second file is overwriting the data that was previously copied from the first file. Subsequent files do not overwrite the previous data. This is the portion of the macro responsible for the copy and paste portion of the overall macro.
Code:
[/COLOR]Sub copyData()
Dim fileToOpen As Variant
Dim path As Variant
Dim masterWB As Workbook
Dim currentWorkbook As Workbook
Dim currentSheet As Worksheet
Dim startCell As Range
Set masterWB = ThisWorkbook
path = masterWB.Sheets("Support").Range("C6").Value
Do While path = VBA.Constants.vbNullString
MsgBox "Select Location of the Individual Master Files"
Call SelectPath
Loop
'open individual master files, copy data and close files
fileToOpen = Dir(path & "*master*xls?")
Do Until fileToOpen = vbNullString
DoEvents
Set currentWorkbook = Workbooks.Open(path & fileToOpen)
currentWorkbook.Activate
Set currentSheet = currentWorkbook.Sheets("Master Schedule")
currentSheet.Activate
Set startCell = currentSheet.Range("A2")
startCell.EntireRow.Select
currentSheet.Range(Selection, Selection.End(xlDown).End(xlToRight)).Select
Selection.Copy
masterWB.Activate
masterWB.Sheets("Master Schedule").Activate
masterWB.Sheets("Master Schedule").Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
currentWorkbook.Activate
Set currentSheet = currentWorkbook.Sheets("Complete")
currentSheet.Activate
Set startCell = currentSheet.Range("A2")
startCell.EntireRow.Select
currentSheet.Range(Selection, Selection.End(xlDown).End(xlToRight)).Select
Selection.Copy
masterWB.Activate
masterWB.Worksheets("Complete").Activate
masterWB.Worksheets("Complete").Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
currentWorkbook.Close
fileToOpen = Dir
Loop
End Sub
[COLOR=#333333]