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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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