extract either a 6 or 5 digit number from string

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Is the number you want always located just before the dot separating the file name from the file extension?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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