Sub StackResults()
Dim LastRow As Long
Dim xWS As Worksheet, xTWB As Workbook, DestSheet As Worksheet, Head As Range
Dim xStrAWBName As String, FolderName As String, sItem As String, Header As Long
Dim FolderPath As String, fldr As FileDialog, Lr As Long, LCD As Long, r As Long
Dim os As Long, LrS As Long, LCS As Long, FileName As String, DD As Long, FN2 As String
Dim N As Long, T As Long, N2 As Long
On Error Resume Next
Set xTWB = ThisWorkbook
Worksheets.Add
Set DestSheet = xTWB.ActiveSheet
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder containing files to merge"
.AllowMultiSelect = False
'.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
FolderName = sItem
DD = InStrRev(FolderName, "\")
FN2 = Right(FolderName, Len(FolderName) - DD)
Set fldr = Nothing
FolderPath = FolderName & "\"
T = CountFilesInFolder(FolderPath, "*.xls*")
FileName = Dir(FolderPath & "*.xls*")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Do While FileName <> ""
Workbooks.Open FileName:=FolderPath & FileName, ReadOnly:=True
xStrAWBName = ActiveWorkbook.Name
N = N + 1
For Each xWS In ActiveWorkbook.Sheets
If xWS.Name = "Results" Then
Sheets("Results").Range("ED1:EQ1").Copy (Sheets("Results").Range("ED2"))
Lr = DestSheet.Range("A" & Rows.Count).End(xlUp).Row
LrS = xWS.Range("A" & Rows.Count).End(xlUp).Row
LCS = xWS.Cells(1, Columns.Count).End(xlToLeft).Column + 1
LCD = DestSheet.Cells(1, Columns.Count).End(xlToLeft).Column + 1
If Lr = 1 Then
Range(DestSheet.Cells(0, 2), DestSheet.Cells(2, LCS)).Value = Range(xWS.Cells(1, 1), xWS.Cells(2, LCS - 1)).Value
DestSheet.Range("A1").Value = "FileName"
End If
Set Head = xWS.Range("A1")
For os = 0 To xWS.Cells(2, Columns.Count).End(xlToLeft).Column - 1
On Error Resume Next
Header = 0
Header = WorksheetFunction.Match(Head.Offset(0, os), DestSheet.Rows(1), 0)
On Error GoTo 0
If Header = 0 Then
DestSheet.Cells(1, LCD) = Head.Offset(0, os)
Header = LCD
LCD = LCD + 1
End If
If Lr = 1 Then
N2 = (N - 1) * LCS + os + 1
Application.StatusBar = "Importing Data.. " & Round(N2 * 100 / (T * LCS), 2) & "% Completed"
UserForm1.Text.Caption = Round(N2 * 100 / (T * LCS), 2) & "% Completed"
UserForm1.Bar.Width = Round(N2 * 100 / (T * LCS), 2) * 4.8
DoEvents
Range(DestSheet.Cells(Lr + 2, Header), DestSheet.Cells(Lr + LrS - 1, Header)).Value = Range(xWS.Cells(25, os + 1), xWS.Cells(LrS, os + 1)).Value
Else
N2 = (N - 1) * LCS + os + 1
Application.StatusBar = "Importing Data.. " & Round(N2 * 100 / (T * LCS), 2) & "% Completed"
UserForm1.Text.Caption = Round(N2 * 100 / (T * LCS), 2) & "% Completed"
UserForm1.Bar.Width = Round(N2 * 100 / (T * LCS), 2) * 4.8
DoEvents
Range(DestSheet.Cells(Lr + 1, Header), DestSheet.Cells(Lr + LrS - 2, Header)).Value = Range(xWS.Cells(3, os + 1), xWS.Cells(LrS, os + 1)).Value
End If
Next os
If Lr = 1 Then
Range(DestSheet.Cells(Lr + 2, 1), DestSheet.Cells(Lr + LrS - 1, 1)).Value = Left(FileName, Application.WorksheetFunction.Find(".", FileName) - 1)
Else
Range(DestSheet.Cells(Lr + 1, 1), DestSheet.Cells(Lr + LrS - 2, 1)).Value = Left(FileName, Application.WorksheetFunction.Find(".", FileName) - 1)
End If
End If
Next xWS
Workbooks(xStrAWBName).Close
FileName = Dir()
Loop
UserForm1.Text.Caption = "100% Completed"
UserForm1.UnloadThisForm
DestSheet.Activate
DestSheet.Name = "xStrAWBName"
Columns("A:EA").EntireColumn.AutoFit
'================================================== rows with BLANK,NON-NUMERI OR <1
'THIS IS WHERE I NEED ENTIRE ROW DELETE IF COLUMN EA, STARTING FROM ROW 2 IS BLANK,NON-NUMERI OR <1.
'================================================== rows with BLANK,NON-NUMERI OR <1
On Error Resume Next
Rows("2:2").Select
Range("A2").EntireRow.Insert
'Rows("3:3").Select
Selection.AutoFilter
ActiveWindow.FreezePanes = True
xTWB.Save
Sheets("xStrAWBName").Select
Sheets("xStrAWBName").Move
ActiveWorkbook.SaveAs FileName:=FolderPath & FN2 & "-" & "ResultsSTACK", FileFormat:=xlCSV, CreateBackup:=False
Application.StatusBar = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
MsgBox "ResultsS MERGED for " & FN2 & vbLf & vbLf _
& "YOU CAN NOW USE THE FILTER HANDLES TO KNOCK OUT UNWANTED SITES OR GIGO!", vbInformation
End Sub
'==========================================================================================================
Function CountFilesInFolder(strDir As String, Optional strType As String) As Long
Dim file As Variant, i As Integer, T As Integer
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
i = i + 1
file = Dir
Wend
CountFilesInFolder = i
End Function