Hi all!
Hope everyone is keeping well.
I have a small issue with my VBA and it would be great if someone could please help!
Basically, I have the following VBA which allows me to import an XLS or XLSB file into my Masterfile, and it is pasted under the current data. But I have to keep selecting multiple files each time for the import process.
Objective:
I would like to import all the XLS or XLSB files from within a selected folder and let the VBA loop through all the files for the import just like my code does. This would save me from selecting each individual file manually!
I have about 190 workbooks to import into the Master, so I don't want to click on 190 files! ?
My Code:
Please let me know if you have any ideas! Thank you in advance, this one has been challenging me for over a week now!
I can provide more information if required.
Thank you,
Manerlao
Hope everyone is keeping well.
I have a small issue with my VBA and it would be great if someone could please help!
Basically, I have the following VBA which allows me to import an XLS or XLSB file into my Masterfile, and it is pasted under the current data. But I have to keep selecting multiple files each time for the import process.
Objective:
I would like to import all the XLS or XLSB files from within a selected folder and let the VBA loop through all the files for the import just like my code does. This would save me from selecting each individual file manually!
I have about 190 workbooks to import into the Master, so I don't want to click on 190 files! ?
My Code:
VBA Code:
Sub Imp()
Dim fName As Variant, sh As Worksheet, wb As Workbook
CYCLE:
fName = Application.GetOpenFilename("Excel Files (*.xls*), *.xls*", Title:="Select a file")
If fName = False Then Exit Sub
Set wb = Workbooks.Open(fName)
For Each sh In ThisWorkbook.Sheets
If ShtExists(sh.Name, wb) Then
wb.Sheets(sh.Name).UsedRange.Offset(1).Copy sh.Cells(Rows.Count, 1).End(xlUp)(2)
End If
Next
ans = MsgBox("Workbook " & Mid(fName, InStrRev(fName, "\") + 1) & " is incorporated. Add more?", _
vbYesNo, "Add more?")
wb.Close False
If ans = vbYes Then GoTo CYCLE:
MsgBox ("Ready.")
End Sub
Public Function ShtExists(ShtName As String, Optional Wbk As Workbook) As Boolean
If Wbk Is Nothing Then Set Wbk = ActiveWorkbook
On Error Resume Next
ShtExists = (LCase(Wbk.Sheets(ShtName).Name) = LCase(ShtName))
On Error GoTo 0
End Function
Please let me know if you have any ideas! Thank you in advance, this one has been challenging me for over a week now!
I can provide more information if required.
Thank you,
Manerlao