I've been spending time online to work out how to import worksheets from a folder and import them as their own sheets into one master workbook.
The spreadsheet is located in its own folder StatConverter with another folder named Users in the same directory.
Here it is:
Dim FolderName As String
FolderName = Environ$("userprofile") & "\OneDrive - {Redacted}\Desktop\StatConverter\Users\"
In the users folder, the total number of sheets varies and I would need to put the script in a loop.
On an excel forum I have found this example:
Sub Import()
Dim directory As String, fileName As String, sheet As Worksheet, total As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
directory = Environ$("userprofile") & "\OneDrive - {Redacted}\Desktop\StatConverter\Users\"
fileName = Dir(directory & "*.xl??")
Do While fileName <> ""
Workbooks.Open (directory & fileName)
For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("import-sheets.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("import-sheets.xlsm").Worksheets(total)
Workbooks(fileName).Close
fileName = Dir()
Next sheet
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I have tried to modify it to suit my needs but I cannot get this working, nor is there an error message for me to troubleshoot.
Can you please help?
Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
' If it's not, tnen instead use:
'Set swb = Workbooks("import-sheets.xlsm")
Do While Filename <> ""
With Workbooks.Open(directory & Filename)
.Worksheets.Copy After:=swb.Sheets(swb.Sheets.Count)
.Close SaveChanges:=False
End With
Filename = Dir
Loop