I have made a VBA code to open the file explorer to select different files and copy a range and paste that range into a master file. I would like to grab the name of the file that got opened to copy from and place that next to the first line of data that was pasted into the master file. I will put the current code that I have wrote below.
Sub Get_Data_From_File()
Dim FiltToOpen As Variant
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*), *xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set wsCopy = OpenBook.Worksheets("review")
Set wsDest = Workbooks("Pull Data Sheet 03282023").Worksheets("review")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("A2:Q" & lCopyLastRow).Copy _
wsDest.Range("B" & lDestLastRow)
Set wsCopy = OpenBook.Worksheets("excluded")
Set wsDest = Workbooks("Pull Data Sheet 03282023").Worksheets("excluded")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("A2:Q" & lCopyLastRow).Copy _
wsDest.Range("B" & lDestLastRow)
OpenBook.Close False
End If
Application.ScreenUpdating = True
End Sub
Sub Get_Data_From_File()
Dim FiltToOpen As Variant
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Application.ScreenUpdating = False
FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*.xls*), *xls*")
If FileToOpen <> False Then
Set OpenBook = Application.Workbooks.Open(FileToOpen)
Set wsCopy = OpenBook.Worksheets("review")
Set wsDest = Workbooks("Pull Data Sheet 03282023").Worksheets("review")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("A2:Q" & lCopyLastRow).Copy _
wsDest.Range("B" & lDestLastRow)
Set wsCopy = OpenBook.Worksheets("excluded")
Set wsDest = Workbooks("Pull Data Sheet 03282023").Worksheets("excluded")
lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
wsCopy.Range("A2:Q" & lCopyLastRow).Copy _
wsDest.Range("B" & lDestLastRow)
OpenBook.Close False
End If
Application.ScreenUpdating = True
End Sub