Sub GetLastRow()
Dim SelectFolder As Integer
Dim x As Long
Dim strPath As String
Dim wsSummary As Worksheet
Dim wb As Workbook
Dim FSOLibrary As FileSystemObject
Dim FSOFolder As Object
Dim sFileName As Object
On Error GoTo err_chk
Set wsSummary = Sheet1
SelectFolder = Application.FileDialog(msoFileDialogFolderPicker).Show
If Not SelectFolder = 0 Then
strPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
Else
End
End If
Application.ScreenUpdating = False
'Set all the references to the FSO Library
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSOLibrary.GetFolder(strPath)
x = 1
'Loop through each file in a folder
For Each sFileName In FSOFolder.Files
Set wb = Workbooks.Open(sFileName)
If x = 1 Then
wb.Sheets("Sheet1").Range("A1", wb.Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp)).Copy wsSummary.Cells(1, x)
x = x + 1
Else
wb.Sheets("Sheet1").Range("B1", wb.Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp)).Copy wsSummary.Cells(1, x)
End If
x = x + 1
wb.Close True
Next
Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Application.ScreenUpdating = True
Exit Sub
'Error handling routime below
err_chk:
If Err.Number = 9 Then
MsgBox "The value of x at the time of error is: " & x, vbOKOnly, "Run Time Error 9!!!"
Else
MsgBox Err.Number & ":" & Err.Description
End If
Set FSOLibrary = Nothing
Set FSOFolder = Nothing
Application.ScreenUpdating = True
End Sub