I have found varying solutions online to this, but haven't been able to conform them to what I want. Here is the problem set:
Open all .xlsx files in selected folder DONE
Copy Master Workbook to Archive folder (selected folder path/Archive) DONE
Clear data in Master Workbook in Worksheet titled "FY19 Source" (Worksheet 3) from Row 2 and below. DONE
Clear data in Master Workbook in Worksheet titled "Travel-Events Calendar" (Worksheet 4) from Row 2 and below. DONE
Clear data in Master Workbook in Worksheet titled "Transfer" (Worksheet 5) from Row 5 and below. DONE
For each open Workbook (except Master Workbook), copy the non-hidden/non-null data from all rows south of A2:M2 in Worksheets titled "FY19 Source"
Paste data continuously in Master Workbook's "FY19 Source" Worksheet starting on Row 2.
For each open Workbook, if they have a Worksheet labeled "Transfer" OR "Transfer2" OR "Transfer 3", copy the non-hidden/non-null data from all rows south of A2:M2 for each.
Paste data continuously in Master Workbooks "Transfer" Worksheet starting on Row 2
For each open Workbook clear filters from the Worksheets titled "Travel-Events Calendar"
For each open Workbook (except Master Workbook), copy the non-hidden/non-null data from all rows south of A5:L5
Paste data continuously in Master Workbooks "Travel-Events Calendar" Worksheet starting on Row 5.
Execute Refresh Links in Master Workbooks DONE
I could really use help with the copying/combining aspect of this from open workbooks as noted above.
I have found a couple of like-minded questions during my research but can't seem to apply them totally to this which is really frustrating :( It seems like I can do most of these steps in turn but I can't put anything together that works! Any guidance would be extremely appreciated. Thank you!
Code I have so far annotated:
Sub MasterWorkbookCompile()
'Declaring Variables
Dim myPath As String
Dim archivePath As String
Dim endSourceSheet As Worksheet
Dim endTransferSheet As Worksheet
Dim endTravelSheet As Worksheet
fName = Dir(Application.ThisWorkbook.FullName)
myPath = Application.ThisWorkbook.FullName
archivePath = "C:\Users\XX\" & (fName)
'Debug.Print myPath, archivePath
'Saving current file to archive folder
ThisWorkbook.SaveCopyAs Filename:=archivePath
'Unfilters data on last worksheet
On Error Resume Next
ThisWorkbook.Worksheets("Travel-Events Calendar").ListObjects("Table2").AutoFilter.ShowAllData
'Clearing data in relevant worksheets
ThisWorkbook.Sheets("XXFY19 Source").Range(ThisWorkbook.Sheets("XXFY19 Source").Range("A2:M2"), ThisWorkbook.Sheets("XXFY19 Source").Range("A2:M2").End(xlDown)).ClearContents
ThisWorkbook.Sheets("Transfer Funds").Range(ThisWorkbook.Sheets("Transfer Funds").Range("A2:M2"), ThisWorkbook.Sheets("Transfer Funds").Range("A2:M2").End(xlDown)).ClearContents
With ThisWorkbook.Sheets("Travel-Events Calendar").ListObjects("Table2")
.Range.AutoFilter
.DataBodyRange.Offset(1).Resize(.DataBodyRange.Rows.Count - 1, .DataBodyRange.Columns.Count).Rows.ClearContents
.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
End With
On Error GoTo 0
'Opens all .xlsx files
Dim xStrPath As String
Dim xFileDialog As FileDialog
Dim xFile As String
On Error Resume Next
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "Select a folder"
xFileDialog.InitialFileName = "C:\Users\XX"
If xFileDialog.Show = -1 Then
xStrPath = xFileDialog.SelectedItems(1)
End If
If xStrPath = "" Then Exit Sub
xFile = Dir(xStrPath & "\*.xlsx")
Do While xFile <> ""
If Not ActiveWorkbook Then
Workbooks.Open xStrPath & "\" & xFile
xFile = Dir
End If
Loop
'Refreshes any PivotTable Links
ThisWorkbook.RefreshAll
End Sub
DATA PROCESSING
Dim wsCopy_F19 As Long
Dim wsCopy_Transfer As Long
Dim wsCopy_Travel As Long
Dim wsCopy As Worksheet
Dim numWs As Double
Dim i As Double
Dim wsCopyName As String
Dim Target1 As Range
Dim Target2 As Range
Dim Target3 As Range
numWs = wbCopy.Worksheets.Count
For i = 0 To numWs
wsCopy = wbCopy.Worksheets(i)
wsCopyName = wsCopy.Name
If wsCopyName = "FY19 Source" Then
wsCopy_F19 = wsCopy.Cells(Rows.Count, 1).End(xlUp).Row
Set Target1 = wsCopy.Range("A2:M" & wsCopy_F19)
Target1.Copy Destination:=wsMSTR_XXF19.Range("A" & rowMSTR_F19).PasteSpecial(xlPasteValues)
rowMSTR_F19 = wsMSTR_XXF19.Cells(Rows.Count, 1).End(xlUp).Row + 1
ElseIf InStr(wsCopyName, "Transfer") > 0 Then
wsCopy_Transfer = wsCopy.Cells(Rows.Count, 1).End(xlUp).Row
Set Target2 = wsCopy.Range("A2:M" & wsCopy_Transfer)
Target2.Copy Destination:=wsMSTR_Transfer.Range("A" & rowMSTR_Transfer).PasteSpecial(xlPasteValues)
rowMSTR_Transfer = wsMSTR_Transfer.Cells(Rows.Count, 1).End(xlUp).Row + 1
ElseIf wsCopyName = "Travel-Events Calendar" Then
wsCopy_Travel = wsCopy.Cells(Rows.Count, 1).End(xlUp).Row
Set Target3 = wsCopy.Range("A2:M" & wsCopy_Travel)
Target3.Copy Destination:=wsMSTR_Travel.Range("A" & rowMSTR_Travel).PasteSpecial(xlPasteValues)
rowMSTR_Travel = wsMSTR_Travel.Cells(Rows.Count, 1).End(xlUp).Row + 1
Else
End If
Next