Parsing BrowseForFolder result odd behavior

couch1971

New Member
Joined
Jan 13, 2005
Messages
12
Hi all,

I am using this snippet of code (sorry code-->html is not functioning atm) to get a folder name, which is also a project name that the user needs to enter into a cell.

I want to automate this as the names are very long, convoluted and I don't want them to have to rename-copy-paste to get them into the sheet.

Code:
-----------------------------------------
Private Sub cmdGetfolder_Click()

Dim TheChosenPath As String
TheChosenPath = BrowseForFolder("Please select your folder", 17)
If TheChosenPath <> "" Then
txtprojname = TheChosenPath
Else
'do something else or nothing at all...
End If
End Sub


Private Function BrowseForFolder(Title As String, Optional RootFolder As Variant) As String
Dim Shell, Folder

Set Shell = CreateObject("Shell.Application")
Set Folder = Shell.BrowseForFolder(0, Title, 0, RootFolder)
If (Not Folder Is Nothing) Then
BrowseForFolder = Folder.Self.Path

BrowseForFolder = Right(BrowseForFolder, InStrRev(BrowseForFolder, "\") - 1)
Else

End If
End Function
-----------------------------

The problem I am having is at this point:
BrowseForFolder = Right(BrowseForFolder, InStrRev(BrowseForFolder, "\") - 1)

When i have a folder named: Li_Pinal_FC3_4_031907 it is being parsed to inal_FC3_4_031907 for some reason it is dropping off the "Li_P"

BUT if I have a folder named: 11172006_Downtown it is being parsed correctly and the text return is: 11172006_Downtown

What is happening, and is there a better way to get the folder name by selection. The folder has nothing to do with the sheet the form is in, the form is a data entry tool to add many lines of metadata.

Note: The folder names will always have at least one, if not more 'underscore's in the foldername.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Shouldn't that be:

Code:
BrowseForFolder = Right(BrowseForFolder, Len(BrowseForFolder)-InStrRev(BrowseForFolder, "\"))
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
Richard appears to have given you a solution, but I would suggest that instead of Right you use Mid.

Untested.
Code:
BrowseForFolder = Mid(BrowseForFolder, InStrRev(BrowseForFolder, "\")+1)
 

Forum statistics

Threads
1,181,420
Messages
5,929,801
Members
436,696
Latest member
Mr Rice

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
Top