Path without filename

dcoledc

Active Member
Joined
May 6, 2010
Messages
403
I am trying to prevent the user from changing directories when I call getopenfilename. This sort of works:

Code:
10: Dim sFullPath As String
    Dim Force As String
    ChDir ThisWorkbook.Path
    ChDir ".\..\" & "Patient Files"
    Force = CurDir
    sFullPath = Application.GetOpenFilename("Excel Files (*.xl*)," & _
    "*.xl*", 1, "Select Patient File", "Open", False)
    If Force <> CurDir Then
        MsgBox "Invalid selection.", vbOKOnly + vbCritical, "Reports to Go..."
        GoTo 10
    End If

I am thinking if instead of using CurDir here:

Code:
If Force <> CurDir Then

I use sfullPath - Filename, I will get what I am looking for. Anyone know how to do that?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
For those interested, I got using this function I found in a search:

Function FindLast(ByVal StringToSearch, ByVal StringToFind As String) As
Code:
Integer
Dim LastCount
LastCount = 0
While InStr(LastCount + 1, StringToSearch, StringToFind) > 0
  LastCount = InStr(LastCount + 1, StringToSearch, StringToFind)
Wend
FindLast = LastCount
End Function

And here is the code using the function:

Code:
10: Dim sFullPath As String
Dim LastSlash As String
Dim x As String
Dim force As String
ChDir ThisWorkbook.Path
ChDir ".\..\" & "Patient Files"
force = CurDir & Application.PathSeparator
sFullPath = Application.GetOpenFilename("Excel Files (*.xl*)," & _
"*.xl*", 1, "Select Patient File", "Open", False)
    
LastSlash = FindLast(sFullPath, "\")
x = Left(sFullPath, LastSlash)
If sFullPath = "False" Then
    beforeClose = False
    beforeSave = False
    Exit Sub
End If
    
If force <> x Then
    MsgBox "Invalid selection.", vbOKOnly + vbCritical, "Reports to Go..."
    GoTo 10
Else
 
Upvote 0
I've always used =CELL("Filename") to determine Path being used and filename. If it doesn't meet with what you want then get the Coding to do whatever you want it to do.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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