Hello, I require code to search for a specific folder on my c:\ drive using a user input wildcard. Then, I would like to give the option to open the folder parth. Thanks in advance!
VBA Code to find a folder and store its path
- Markus1989
- Thread is marked as Resolved.
-
-
Hello and Welcome to the Forum :smile:
Below is an example to open the File Dialog ..
Code
Display More[FONT=courier new][COLOR=#0000FF]Sub [/COLOR] Example()[/FONT] [FONT=courier new][COLOR=#0000FF]Dim [/COLOR] intResult [COLOR=#0000FF]As [/COLOR] [COLOR=#0000FF]Integer [/COLOR][/FONT] [FONT=courier new][COLOR=#0000FF]Dim [/COLOR] strPath [COLOR=#0000FF]As [/COLOR] [COLOR=#0000FF]String [/COLOR][/FONT] [FONT=courier new][COLOR=#00B400]'the Dialog Box is displayed to the user [/COLOR][/FONT] [FONT=courier new]intResult = Application.FileDialog(msoFileDialogFolderPicker).Show[/FONT] [FONT=courier new][COLOR=#00B400]'checks if user has cancelled the dialog [/COLOR][/FONT] [FONT=courier new][COLOR=#0000FF]If [/COLOR] intResult <> 0 [COLOR=#0000FF]Then [/COLOR][/FONT] [FONT=courier new][COLOR=#00B400]'display Message Box [/COLOR][/FONT] [FONT=courier new][COLOR=#0000FF]Call [/COLOR] MsgBox(Application.FileDialog(msoFileDialogFolderPicker _[/FONT] [FONT=courier new]).SelectedItems(1), vbInformation, "Selected Folder") [/FONT] [FONT=courier new][COLOR=#0000FF]End If [/COLOR][/FONT] [FONT=courier new][COLOR=#0000FF]End [/COLOR] [COLOR=#0000FF]Sub [/COLOR][/FONT]
Hope this will help
-
Thanks for that! But not exactly what I am looking for. I would like to use an input box prompting a user to enter a search variable. Then upon submitting the macro will look in a preset Dir for a folder containing the variable. Then give the option to open the found folder.
-
Thanks for that! But not exactly what I am looking for. I would like to use an input box prompting a user to enter a search variable. Then upon submitting the macro will look in a preset Dir for a folder containing the variable. Then give the option to open the found folder.
Understand your point ...
However, you do need to rely on extremely disciplined users ... to always spell correctly their input searches ...unless you are planning on providing a Validation List of the possible search variables ...
-
Ok, so theres no way to search a dir for a folder like this:
Location: c:\test\test1\test2\
User input: mark
Search: c:\test\test1\test2 & *mark*
If found: store path
Would you like to open folder?: path
Yes --> Open folder
These are the steps I would like to follow. Just need to convert them to VBA :).
-
-
Once the user inputs mark ...
Could you get a long list of subfolders for which their names would contain *mark* ...
or ... are you 100% sure, thanks to your actual structure ... that the result can only be Nothing or 1 folder ...???
-
In the dir I want to search there are only unique folder names. Theres strick control on formating. Thanks again foe the help.
-
Hello,
You could adapt the following macro to test if ... after the input of your Search Text ... the folder exists ...or not ...
Code
Display MoreSub Test_Folder_Exists() Dim sval As String Dim path As String Dim folder As String sval = InputBox("Please Input your Search Text") path = "C:\test\test1\test2\" & sval folder = Dir(path, vbDirectory) If folder = vbNullString Then MsgBox "Folder does not exist" Else MsgBox "Folder exists" ' If need be Open Folder Call Shell("explorer.exe " & folder, vbNormalFocus) End If End Sub
Hope this will help
Participate now!
Don’t have an account yet? Register yourself now and be a part of our community!