Forum Discussion
minhhai91
Jun 30, 2021Copper Contributor
Select all files in a folder automatically
Hello, I have a code for fussing with several sheets in the same folder by but you have to select the files manually: this is the code I actually have This is the code i want to ...
- Jun 30, 2021
Replace
With Application.FileDialog(4) ' msoFileDialogFolderPicker .InitialFileName = ThisWorkbook.Path If .Show Then stDossier = .SelectedItems(1) & Application.PathSeparator Else Beep Exit Sub End If End With
with a line that specifies the path:
stDossier = "C:\MyFolder\MySubfolder\"
The path must end in a backslash \
HansVogelaar
Jun 30, 2021MVP
Here you go:
Sub Fusionner()
Dim wbFusion As Workbook
Dim wbCible As Workbook
Dim shCible As Worksheet
Dim stDossier As String
Dim stFichier As String
With Application.FileDialog(4) ' msoFileDialogFolderPicker
.InitialFileName = ThisWorkbook.Path
If .Show Then
stDossier = .SelectedItems(1) & Application.PathSeparator
Else
Beep
Exit Sub
End If
End With
Set wbFusion = ThisWorkbook
stFichier = Dir(stDossier & "*.xls*")
Do While stFichier <> ""
Set wbCible = Workbooks.Open(stDossier & stFichier)
For Each shCible In wbCible.Worksheets
shCible.Copy After:=wbFusion.Worksheets(wbFusion.Worksheets.Count)
Next shCible
wbCible.Close SaveChanges:=False
stFichier = Dir
Loop
End Sub
minhhai91
Jun 30, 2021Copper Contributor
Thanks for your anwser.
how can I add a path so I don't have to manually select the folder ?
how can I add a path so I don't have to manually select the folder ?
- HansVogelaarJun 30, 2021MVP
Replace
With Application.FileDialog(4) ' msoFileDialogFolderPicker .InitialFileName = ThisWorkbook.Path If .Show Then stDossier = .SelectedItems(1) & Application.PathSeparator Else Beep Exit Sub End If End With
with a line that specifies the path:
stDossier = "C:\MyFolder\MySubfolder\"
The path must end in a backslash \
- minhhai91Jun 30, 2021Copper ContributorThank you very much, it work.