VBA GetOpenFileName

shamas21

Active Member
Joined
Nov 19, 2007
Messages
280
Hi All

The following gets the file path of the file but Im trying to get the folder path only.

Code:
Dim sFile1 As Variant

sFile1 = Application.GetOpenFilename

How can I change this?

Thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi,

You can use .Path for this. Hope this helps;

Code:
ActiveWorkbook.Path

Hi

I dont want the activeworkbooks file path. Instead i want a dialog box for the user to select a folder and i want the file path for that folder.

Any suggestions?
 
Upvote 0
Hi,

Do me a favour, run your code with this line added at the end;

Code:
Dim sFile1 As Variant

sFile1 = Application.GetOpenFilename

Msgbox sFile1

It does provide the full path....
 
Upvote 0
Hi,

Do me a favour, run your code with this line added at the end;

Code:
Dim sFile1 As Variant

sFile1 = Application.GetOpenFilename

Msgbox sFile1

It does provide the full path....

Yes it provides the full path...but im looking for the folder path.

i.e. it returns: C:\Documents and Settings\Desktop\TCF\Forecast.xls
I want it to be: C:\Documents and Settings\Desktop\TCF

Does that make sence?

Thanks
 
Upvote 0
It is unclear just what you want. Did you want to browse for a folder and return the foldername or browse for a file and return the folder part? If the latter, this will do it.
Code:
Function FolderPart(sPath As String) As String
  FolderPart = Left(sPath, InStrRev(sPath, "\"))
End Function

If the former:
Code:
Sub Test_GetFolder()
  MsgBox GetFolder("Get My Folder", ThisWorkbook.path)
End Sub


Function GetFolder(Optional sTitle As String = "Select Folder", _
  Optional sInitialFilename As String)
  Dim myFolder As String
  With Application.FileDialog(msoFileDialogFolderPicker)
    If sInitialFilename = "" Then sInitialFilename = ThisWorkbook.path

    .initialFilename = sInitialFilename
    .Title = "Greetings"
    If .Show = -1 Then
      GetFolder = .SelectedItems(1)
      If Right(GetFolder, 1) <> "\" Then
            GetFolder = GetFolder & "\"
      End If
      Else: GetFolder = ""
    End If
  End With
End Function
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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