extract either a 6 or 5 digit number from string

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
57
Hi,

The below code will loop through a folder of files and look for pdf's.

Each filename will contain a unique ID either 6 digits or 5 digits in length. What I would like to do is extract that numerical value, i.e. sample234567.pdf and assign my UniqueID variable into a string value 234567

There should only ever be a single numerical value in the filename so there may be an easier way to accomplish this rather than specifically testing the length of the numerical part of the string.

VBA Code:
Sub SelectFolder()

    Dim fldr As FileDialog
    Dim MyFolder As String
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim UniqueID As String

    'Create an instance of the FileSystemObject
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)

    With fldr
        .Title = "Select folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> 0 Then
            MyFolder = .SelectedItems(1)
        Else
            MsgBox "User Cancelled"
            Exit Sub
        End If
    End With

    Set objFolder = objFSO.GetFolder(MyFolder)

    'loops through each file in the directory and prints their names and path
    For Each objFile In objFolder.Files
        If Right(objFile.Name, 3) = "pdf" Then
        End If
    Next objFile

End Sub

Thanks
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,877
Office Version
  1. 2010
Platform
  1. Windows
Is the number you want always located just before the dot separating the file name from the file extension?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,606
Messages
5,625,767
Members
416,136
Latest member
senthil_sk

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