Combine from current folder

Saab95

New Member
Joined
Mar 26, 2021
Messages
40
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have this vba that works well but you need to update the path everytime. i wanted to update it to have it using the current path of the active workbook but couldn't make it work.

I have tried many options with the activeworkbook.path but couldn't make it work. Any idea ?

Sub CombineWorkbooks()

Dim Path As String
Path = "N:\ABC\"

Dim FileName As String
FileName = Dir(Path & "*.csv")

Dim ws As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While FileName <> ""
Workbooks.Open Path & FileName
For Each ws In ActiveWorkbook.Sheets
ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
Next ws
Workbooks(FileName).Close
FileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try changing this:
Path = "N:\ABC\"
to this:
VBA Code:
myPath = ActiveWorkbook.Path & "\"

And use "myPath" instead of "Path" in your code (note that "Path" is a reserved word and should NOT be used for variables! Doing so can cause errors and unexpected results!).
The same is true for "Filename".

So your code should look like:
VBA Code:
Sub CombineWorkbooks()

Dim myPath As String
myPath = ActiveWorkbook.Path & "\"

Dim myFileName As String
myFileName = Dir(myPath & "*.csv")

Dim ws As Worksheet

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Do While myFileName <> ""
    Workbooks.Open myPath & myFileName
    For Each ws In ActiveWorkbook.Sheets
        ws.Copy After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    Next ws
    Workbooks(myFileName).Close
    myFileName = Dir()
Loop

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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