VBA to pull in and generate hyperlink for files with the letter *R* in the first 4 letters of the file name.

Bvendett4

New Member
Joined
Apr 10, 2018
Messages
24
Hello,
I am currently using the below vba, this generates a hyperlink into a database once the item is saved into the specific file path.

Code:
Sub Example1()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer

Sheet2.Activate

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

'Get the folder object
Set objFolder = objFSO.GetFolder("G:\Shared\Health & Safety\5. Risk Assessments\Risk Assessments New")
i = 1

'loops through each file in the directory
For Each objFile In objFolder.Files
Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    Dim File
    For Each File In Folder.Files
    'create hyperlink in appropriate cell
    ActiveSheet.Hyperlinks.Add Anchor:=ActiveSheet.Cells(i + 1, 2), Address:=objFile.path, TextToDisplay:=objFile.Name
    'add date created to the right
    ActiveSheet.Cells(i + 1, 3).Value = objFile.DateCreated
    i = i + 1
Next objFile

End Sub

This is placed over 3 individual worksheets and pulls in from 3 different file paths
i.e.
Risk Assessments New - is replaced with Task Assessments New on the 2nd worksheet and replaced with PUWER Assessmnet New on the 3rd worksheet.

The files are saved as below
101R A Assessment - Risk Assessment
101T A Assessment - Task Assessment
101P A Assessment - PUWER Assessment

I have now been asked if if we can store all the files in the same location, i.e. in a new Equipment Assessment Folder.

If the vba can read the letter R, T or P as the 4th digit, then hopefully I can carry on using this database and it will pull in the correct files based on these digits.

I hope this explains what I require.

Many Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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