Change directory with user needs

Ulisses_Carso

New Member
Joined
Sep 4, 2020
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hello everyone!

I have this code (which I found on the internet due to my rush) that merges all the files in the directory into a single one, but in it I need to force the directory to make it work, I need to make the user able to select the directory manually.

I managed to get the path with the code below, I just can't include it in the existing code.

Can anyone one tell me what i have to do please?


Obs: I don't need to select the files inside the directory, the code selects them all as it should be.


Code I made to get folder path.
VBA Code:
FolderPath as String
With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    If .SelectedItems.Count > 0 Then
        FolderPath = .SelectedItems(1)
    End If
End With


Full code
VBA Code:
Sub MergeFiles()

Dim bookList As Workbook
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
 
Application.ScreenUpdating = False

'Line i need to change
Set dirObj = mergeObj.Getfolder("forced folder path")

Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
 
Range("A2:IV" & Range("A100000").End(xlUp).Row).Copy
Workbooks("Master.xlsx").Sheets("Master").Activate
 
Range("A300000").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
How about
VBA Code:
Sub MergeFiles()

Dim bookList As Workbook
Dim FolderPath As String
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
 
Application.ScreenUpdating = False
With Application.FileDialog(4)
   .AllowMultiSelect = False
   If .Show Then
      FolderPath = .SelectedItems(1)
   Else
      Exit Sub
   End If
End With
'Line i need to change
Set dirObj = mergeObj.Getfolder(FolderPath & "\")

Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
 
Range("A2:IV" & Range("A100000").End(xlUp).Row).Copy
Workbooks("Master.xlsx").Sheets("Master").Activate
 
Range("A300000").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub MergeFiles()

Dim bookList As Workbook
Dim FolderPath As String
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
Application.ScreenUpdating = False
Set mergeObj = CreateObject("Scripting.FileSystemObject")
 
Application.ScreenUpdating = False
With Application.FileDialog(4)
   .AllowMultiSelect = False
   If .Show Then
      FolderPath = .SelectedItems(1)
   Else
      Exit Sub
   End If
End With
'Line i need to change
Set dirObj = mergeObj.Getfolder(FolderPath & "\")

Set filesObj = dirObj.Files
For Each everyObj In filesObj
Set bookList = Workbooks.Open(everyObj)
 
Range("A2:IV" & Range("A100000").End(xlUp).Row).Copy
Workbooks("Master.xlsx").Sheets("Master").Activate
 
Range("A300000").End(xlUp).Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
bookList.Close
Next
Application.ScreenUpdating = True

End Sub

It worked perfectly, thanks dude!!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top