VBA macro does not consistently point to the current directory

LisaDz

New Member
Joined
Oct 15, 2015
Messages
8
I have a macro written in Excel 2007 that looks in the same directory as my open workbook and copies the worksheets from all of the other files in the same directory. At least, that's what it is supposed to do.

The problem I am having is that when I first open my .xlsm worksheet and run that macro, it pulls worksheets from who knows where. If I do a Save As first, then run the macro, it works fine and pulls from the same directory as needed. It would be nice to have a cleaner macro that doesn't require Save As.

As a side note, I had it pointing to the generic "CurrentDirectory" because in the future, this file will be shared with others as a tool to use for reporting. Thus, the file path will change because we each have a unique 6 character ID. Saving the folder on my desktop has the path C:\users\LISA12\Desktop\MSW_ReportTool". If I give it to Joe to save on his desktop, then his path will be C:\users\JOE123\Desktop\MSW_ReportTool.

Here's what I do have:

Sub ImportReports()

Dim directory As String, fileName As String, sheet As Worksheet, total As Integer

'Turn off screen updating and displaying alerts
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Use Dir function to find the first *.xl?? file stored in the directory
directory = CurrentDirectory
fileName = Dir(directory & "*.xlsx")

'Copy each file worksheet, close the file, and move to the next one.
Do While fileName <> ""

Workbooks.Open (directory & fileName)

For Each sheet In Workbooks(fileName).Worksheets
total = Workbooks("MSW_ReportTool_v1.0.xlsm").Worksheets.Count
Workbooks(fileName).Worksheets(sheet.Name).Copy _
after:=Workbooks("MSW_ReportTool_v1.0.xlsm").Worksheets(total)
Next sheet

Workbooks(fileName).Close

fileName = Dir()

Loop

'Turn on screen updating and displaying alerts
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Unfortunately that did not fix it. I inserted that code above "directory = CurrentDirectory".
 
Upvote 0
Eric, I responded too quickly earlier... Thank you for your suggestion; I built on it and the following seems to have fixed my problem:

ChDrive (ActiveWorkbook.Path)
ChDir (ActiveWorkbook.Path)
directory = CurrentDirectory
fileName = Dir(directory & "*.xlsx")

Crisis averted :)
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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