Finding the right folder with VBA

gr1410

New Member
Joined
Dec 18, 2013
Messages
6
Hello,

I have a spreadsheet that is updated with a macro finding the latest download in an appropriate folder. It looks as follows:

MyPath = "C:\User1\Downloads"

MyFile = Dir(MyPath & "*.xls", vbNormal)

......

Everything is working fine. Unfortunately, recently all the users have been assigned separate profiles,
so now the path to the folder depends on who is signed in and the macro works only
in one profile.

"MyPath" would have to be chosen from several different possibilities like:

"C:\User1\Downloads"
"C:\User2\Downloads"
"C:\User3\Downloads" etc.

Only one of them can be found at any one time.

Is there a way for write a code which would find which folder is available at any one time in "MyPath" variable. With an IF statement perhaps?

Thanks in advance for help with this issue.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
If the file is always going to be in the windows user's downloads folder, this might work for MyPath:
Code:
[COLOR=#333333]MyPath =[/COLOR]"C:\Users\" & environ("username") & "\Downloads"


this assumes that they each have a different copy of the file in their own userprofile's downloads folder. If they are all sharing the same file, you probably need to set the file up to just save to C:\ in the first place. and then reference it there.
 
Last edited:
Upvote 0
This will look for the file with the newest date and time

Code:
Sub findnewest()

MyPath1 = "C:\User1\Downloads\"
MyPath2 = "C:\User2\Downloads\"
MyPath3 = "C:\User3\Downloads\"


MyFile1 = Dir(MyPath1 & "*.xls", vbNormal)
MyFile2 = Dir(MyPath2 & "*.xls", vbNormal)
MyFile3 = Dir(MyPath3 & "*.xls", vbNormal)


Time1 = FileDateTime(MyPath1 & MyFile1)
Time2 = FileDateTime(MyPath2 & MyFile2)
Time3 = FileDateTime(MyPath3 & MyFile3)


If Time1 > Time2 And Time1 > Time3 Then
MsgBox "Time1 greatest"
ElseIf Time2 > Time1 And Time2 > Time3 Then
MsgBox "Time2 greatest"
Else
MsgBox "Time3 greatest"
End If


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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