I am trying to figure out the best way to shorten my image hyperlink paths when clicking the submit button. Right now, all userform data and image file paths go to their appropriate rows/columns but it's ugly. I want to see how to use VBA to shorten the filepath to either the filename or change the path to an entire different word like "image". Ideally, I would want to replace the hyperlink with the word "image" but I am not sure that is possible?
I found some ideas on this site about creating functions to call that would shorten the path but I wasn't sure how to use those functions when submitting the data to a database.
My current code is below, followed by the function that I found that could work.
Private Sub CommandButton1_Click()
Dim TargetRow As Long
Dim linked_path1 As Variant
Dim linked_path2 As Variant
TargetRow = Sheets("Engine").Range("B3").Value + 1 'plus 1 move the row down 1 so it doesn't overrite last row value
Sheets("Database").Range("Data_Start").Offset(TargetRow, 1) = orderid
Sheets("Database").Range("Data_Start").Offset(TargetRow, 2) = ComboBox1
Sheets("Database").Range("Data_Start").Offset(TargetRow, 3) = ComboBox2
Sheets("Database").Range("Data_Start").Offset(TargetRow, 4) = ComboBox3
Sheets("Database").Range("Data_Start").Offset(TargetRow, 5) = TextBox2
Sheets("Database").Range("Data_Start").Offset(TargetRow, 6) = TextBox3
'Set named range and a variable in teh Hyperlink.Add function
Set linked_path1 = Sheets("Database").Range("Data_Start").Offset(TargetRow, 7)
Sheets("Database").Hyperlinks.Add Anchor:=linked_path1, _
Address:=filepath1
Set linked_path2 = Sheets("Database").Range("Data_Start").Offset(TargetRow, 8)
Sheets("Database").Hyperlinks.Add Anchor:=linked_path2, _
Address:=filepath2
Unload UserForm2
End Sub
The Function I found on this site that could do it - this only grabs the filename and not the extension
Function FileNameNoExtensionFromPath(strFullPath As String) As String
Dim intStartLoc As Integer
Dim intEndLoc As Integer
Dim intLength As Integer
intStartLoc = Len(strFullPath) - (Len(strFullPath) - InStrRev(strFullPath, "\") - 1)
intEndLoc = Len(strFullPath) - (Len(strFullPath) - InStrRev(strFullPath, "."))
intLength = intEndLoc - intStartLoc
FileNameNoExtensionFromPath = Mid(strFullPath, intStartLoc, intLength)
End Function
Thanks so much April
