vba dailog box

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, i need vba to pop up dailog box and let user select/type in the folder path and enter that in userform textbox1, just the the path...Is this possible?

Thanks in advance.

I found something close but it compiles me to select the file/something...

I just want the user to select the folder and then click OK/something to confirm the path...

Code:
[/FONT]
Sub Get_Data()
ChDrive "C:\"
ChDir "C:\"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End Sub
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Got one from Cpersons....
Please let me know if there is anything better...
Code:
[FONT=Courier New]Private Const BIF_RETURNONLYFSDIRS As Long = &H1[/FONT]
[FONT=Courier New]Private Const BIF_DONTGOBELOWDOMAIN As Long = &H2[/FONT]
[FONT=Courier New]Private Const BIF_RETURNFSANCESTORS As Long = &H8[/FONT]
[FONT=Courier New]Private Const BIF_BROWSEFORCOMPUTER As Long = &H1000[/FONT]
[FONT=Courier New]Private Const BIF_BROWSEFORPRINTER As Long = &H2000[/FONT]
[FONT=Courier New]Private Const BIF_BROWSEINCLUDEFILES As Long = &H4000[/FONT]
[FONT=Courier New]Private Const MAX_PATH As Long = 260[/FONT]
[FONT=Courier New]Type BrowseInfo[/FONT]
[FONT=Courier New]   hOwner As Long[/FONT]
[FONT=Courier New]   pidlRoot As Long[/FONT]
[FONT=Courier New]   pszDisplayName As String[/FONT]
[FONT=Courier New]   lpszINSTRUCTIONS As String[/FONT]
[FONT=Courier New]   ulFlags As Long[/FONT]
[FONT=Courier New]   lpfn As Long[/FONT]
[FONT=Courier New]   lParam As Long[/FONT]
[FONT=Courier New]   iImage As Long[/FONT]
[FONT=Courier New]End Type[/FONT]
[FONT=Courier New]Type SHFILEOPSTRUCT[/FONT]
[FONT=Courier New]   hwnd As Long[/FONT]
[FONT=Courier New]   wFunc As Long[/FONT]
[FONT=Courier New]   pFrom As String[/FONT]
[FONT=Courier New]   pTo As String[/FONT]
[FONT=Courier New]   fFlags As Integer[/FONT]
[FONT=Courier New]   fAnyOperationsAborted As Boolean[/FONT]
[FONT=Courier New]   hNameMappings As Long[/FONT]
[FONT=Courier New]   lpszProgressTitle As String[/FONT]
[FONT=Courier New]End Type[/FONT]
[FONT=Courier New]Declare Function SHGetPathFromIDListA Lib "shell32.dll" ( _[/FONT]
[FONT=Courier New]   ByVal pidl As Long, _[/FONT]
[FONT=Courier New]   ByVal pszBuffer As String) As Long[/FONT]
[FONT=Courier New]Declare Function SHBrowseForFolderA Lib "shell32.dll" ( _[/FONT]
[FONT=Courier New]   lpBrowseInfo As BrowseInfo) As Long[/FONT]
 
[FONT=Courier New]Function BrowseFolder(Optional Caption As String = "") As String[/FONT]
[FONT=Courier New]Dim BrowseInfo As BrowseInfo[/FONT]
[FONT=Courier New]Dim FolderName As String[/FONT]
[FONT=Courier New]Dim ID As Long[/FONT]
[FONT=Courier New]Dim Res As Long[/FONT]
[FONT=Courier New]With BrowseInfo[/FONT]
[FONT=Courier New]  .hOwner = 0[/FONT]
[FONT=Courier New]  .pidlRoot = 0[/FONT]
[FONT=Courier New]  .pszDisplayName = String$(MAX_PATH, vbNullChar)[/FONT]
[FONT=Courier New]  .lpszINSTRUCTIONS = Caption[/FONT]
[FONT=Courier New]  .ulFlags = BIF_RETURNONLYFSDIRS[/FONT]
[FONT=Courier New]  .lpfn = 0[/FONT]
[FONT=Courier New]End With[/FONT]
[FONT=Courier New]FolderName = String$(MAX_PATH, vbNullChar)[/FONT]
[FONT=Courier New]ID = SHBrowseForFolderA(BrowseInfo)[/FONT]
[FONT=Courier New]If ID Then[/FONT]
[FONT=Courier New]  Res = SHGetPathFromIDListA(ID, FolderName)[/FONT]
[FONT=Courier New]  If Res Then[/FONT]
[FONT=Courier New]      BrowseFolder = Left$(FolderName, InStr(FolderName, vbNullChar) - 1)[/FONT]
[FONT=Courier New]  End If[/FONT]
[FONT=Courier New]End If[/FONT]
[FONT=Courier New]End Function[/FONT]
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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