VBA to find a directory based off a cell value, but not exact match

paun_shotts

New Member
Joined
Nov 4, 2021
Messages
41
Office Version
  1. 2013
Platform
  1. Windows
Appreciate your help with this one please.
In my workbook on "Sheet1" In column F is where a return number is given to a return shipment.
The format of these return numbers is as follows "2307070" - 23 refers to calendar year, 07 refers to the calendar month, and 070 is the return number.
The return documents are stored in:
T:\Operations\Inventory\GRA\GRAs\GRAs - 2023
Inside this folder, are more folders for each month such as "07 - July 2023" "08 - August 2023" etc..
So now we are in folder :
T:\Operations\Inventory\GRA\GRAs\GRAs - 2023\07 - July 2023
Inside this folder, are folders for individual returns and the folders are names as "GRA2307070 - Hospital Name"
Example:
T:\Operations\Inventory\GRA\GRAs\GRAs - 2023\07 - July 2023\GRA2307070 - Hospital Name Goes Here
I have some VBA code but I cannot get it working, I want to hyperlink the cell in column F to link to the word doc (.docx) which would be in the above folders.
I will share the spreadsheet and the code and would really like someone to assist me in finding the error in my code, as it is not hyperlinking to the document.
Obviously the hospital name will change for each return, so I have inserted the "*" to try to account for variables in the folder name.
The code I have is:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)



 Dim GRAPath As String
 Dim GRAFilePath As String
 Dim GRAFileName As String
  
    GRAPath = "T:\Operations\Inventory\GRA\GRAs\GRAs - 2023\07 - July 2023\"
    
    If Target.Count = 1 And Target.Column = 6 And Target.Row >= 2 Then
        If Left(Target.Value, 4) = "2307" Then
            GRAFileName = "GRA" & Target.Value
            GRAFilePath = Dir(GRAPath & GRAFileName & "*.docx")
            MsgBox GRAPath & GRAFileName & "*.docx"
            If GRAFilePath <> "" Then
                GRAFilePath = GRAPath & GRAFilePath & "\" & GRAFileName & ".docx"
                If Dir(GRAFilePath) <> "" Then
                    Target.Hyperlinks.Add Target, GRAFilePath, , , GRAFileName
                End If
            End If
        End If
    End If

End Sub

Book1
ABCDEFGHI
1DATE INTIME ININITIALSCOURIER USEDCON NOTE / INVOICE #GRA#DESCRIPTION# OF BOXESATTENTION TO:
24/08/202310AMSPSTARTRACK23070643XMESH1DM
34/08/202310AMSPBUDGET23070673XMESH1DM
44/08/202310AMSPSTARTRACK23070684XMESH1DM
54/08/202310AMSPSTARTRACK23070721XMESH1LF
67/08/20238AMSPSTARTRACK23070376XMESH1LF
77/08/20238AMSPSTARTRACK23070441XMESH1LF
Sheet1
 

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.
For testing purposes, I have renamed one folder, taking out the hospital name from the folder and just leaving "GRA2307068"
Using the below code, this worked perfectly, and added the hyperlink to the correct cell and linked to the correct word document.
Now I just need to work out how to do it with only a partial match to the folder name, ignoring the hospital name after "GRA2307068"

Any ideas please?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)



 Dim GRAPath As String
 Dim GRAFilePath As String
 Dim GRAFileName As String
  
    GRAPath = "T:\Operations\Inventory\GRA\GRAs\GRAs - 2023\07 - July 2023\"
    
    If Target.Count = 1 And Target.Column = 6 And Target.Row >= 2 Then
        If Left(Target.Value, 4) = "2307" Then
            GRAFileName = "GRA" & Target.Value
            GRAFilePath = GRAPath & GRAFileName & "\" & GRAFileName & ".docx"
            'MsgBox GRAFilePath
                If Dir(GRAFilePath) <> "" Then
                    Target.Hyperlinks.Add Target, GRAFilePath
                End If
        End If
    End If

End Sub
 
Upvote 0
After some more research I have found a solution that works for me.


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Const staticFolder = "T:\Operations\Inventory\GRA\GRAs\GRAs - 2023\07 - July 2023\"

Dim Foldername As String, GRAFileName As String
Dim doc As String
 If Target.Count = 1 And Target.Column = 6 And Target.Row >= 2 Then
        If Left(Target.Value, 4) = "2307" Then
            GRAFileName = "GRA" & Target.Value
        End If
 End If

Foldername = Dir(staticFolder & GRAFileName & "*", vbDirectory)
doc = GRAFileName & ".docx"
 If Foldername <> "" Then
        Target.Hyperlinks.Add Target, staticFolder & Foldername & "\" & doc
    Else
        MsgBox "GRA document not found."
    Exit Sub
 End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,169
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