Extract a portion of folder name using VBA

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
Hello to all.

I have a macro that allows me to select a folder and then save that selected location as a variable in a String format.

Code:
Sub SelectFolder()


Dim SourcePath As String


    MsgBox "Please choose the correct folder", vbQuestion, "Folder"
    Application.DisplayAlerts = False
    With Application.FileDialog(msoFileDialogFolderPicker)
          If .Show <> -1 Then MsgBox "No folder selected! Exiting script.": End
        SourcePath = .SelectedItems(1)
    End With
    
End Sub

Once I select the folder, I will need to extract a portion of that folder's name to save as a variable to use in another section of my coding.

The portion I need will always be the first two characters of the folder name and they will always be numerical.

An example would be: C:\Location\Of\The\Folder\I\Have\Selected\09 - Sep

The folder is "09 - Sep", and I need to extract the "09" from that folder name to save as my variable.

Any ideas?

I believe that I will need to use the FileSystemObject, but am not 100% sure on how I should implement it.

I was thinking of something such as this:

Code:
Sub SelectFolder()


Dim fso as New FileSystemObject
Dim SourcePath As String
Dim foldername As String


    MsgBox "Please choose a folder", vbQuestion, "Folder"
    Application.DisplayAlerts = False
    With Application.FileDialog(msoFileDialogFolderPicker)
          If .Show <> -1 Then MsgBox "No folder selected! Exiting script.": End
        SourcePath = .SelectedItems(1)
        foldername = fso.GetFolder(.SelectedItems(1))
    End With
    
End Sub

However, I can't seem to figure out from here how to isolate the first two characters of the folder selected.

Any pointers?

-Spydey
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hello,

try

Code:
Ar = split(SourcePath, "\") 'an Array
Result = val(Ar(ubound(Ar)) 'a number

regards
 
Upvote 0
Hello,

try

Code:
Ar = split(SourcePath, "\") 'an Array
Result = val(Ar(ubound(Ar)) 'a number

regards


Fennek,

Thanks for the assistance. I will give it a try here in a moment and let you know my results. Thanks!

-Spydey
 
Upvote 0

Forum statistics

Threads
1,215,499
Messages
6,125,163
Members
449,210
Latest member
grifaz

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