Search For Directory & Save File

christian2016

Board Regular
Joined
Oct 6, 2016
Messages
123
Hi Guys,

Need help with a VBA code to save to directory if found.

Saving bit I know how to do it's just finding the sub-folder in a directory path is the bit im having trouble with.

Example:
G:\Test\New\Folder
In this directory you will have folders
1 Jan
2 Feb
3 Mar
4 Apr
5 May
6 Jun (etc)


What I need the VBA code to do.
Depending on what today's current month is I need the VBA code to find the correct month sub folder.

Today its the month of November so directory save path should be
G:\Test\New\Folder\11 Nov

Also The drive letter path is a variable as the user can set it to what they like, so I need the VBA code to also check all letters of drive paths from A-Z to try find a directory path match.
A:\Test\New\Folder\11 Nov
B:\Test\New\Folder\11 Nov
C:\Test\New\Folder\11 Nov (etc.)

If path is found then save file and do what you want it to do if not found a msgbox "directory path not found"

I hope this makes sense and is possible.

Any help is greatly appreciated

Thnaks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hello,

some code to play around. Please check the reaction of code in case of finding the directory.

Code:
Sub Test()
On Error Resume Next
BasePath = ":\Test\New\Folder\"
FolderName = Month(Now) & " " & MonthName(Month(Now), True)
For i = 65 To 65 + 25
    If Dir(Chr(i) & BasePath & FolderName, vbDirectory) <> "" Then
        Debug.Print Chr(i), Err.Number
        Debug.Print Dir(Chr(i) & BasePath & FolderName, vbDirectory)
        Err.Clear
    End If
Next i
Debug.Print i, Chr(i)
End Sub

regards
 
Upvote 0
Thanks Fennek

How do i change the format of the folder name to read the full month example 11 November instead of 11 Nov.

Also in the if statement it finds matches for each letter drive. How do i specifically tell it when the correct file path is found then save file.

Sorry im fairly new to VBA although i'm learning quickly.

Thanks again for your help.
 
Upvote 0
without testing:

Code:
FolderName = Month(Now) & " " & MonthName(Month(Now), false)

To search for all possible drives (A-Z) is very unusual (for me). Normally the base path is known. Please describe, what is know and what vba has to search.

regards
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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