Option Explicit
Private fso As Object
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim Col$, r&
Dim x$
Dim i%
Dim colAssociation
Dim TextToShow$, Url$
colAssociation = Split("B,B,google.com" & _
"|C,X,yahoo.com" & _
"|D,something else,excelforum.com" _
, "|")
If Target.Count = 1 And Target.Column > 1 Then
Col = Split(Target.Address, "$")(1)
If Col = "K" Then
Call Open_Folder
Else
r = Target.Row
If r >= 2 And r <= 1000 Then
For i = 0 To UBound(colAssociation)
If Col = Split(colAssociation(i), ",")(0) Then
TextToShow = Split(colAssociation(i), ",")(1)
Url = "https://" & Split(colAssociation(i), ",")(2) & "=100"
Exit For
End If
Next
If Len(TextToShow) = 0 Then TextToShow = Col
If Len(Url) Then
x = GetNum(Cells(Target.Row, 1))
If (x <> "") * (Target.Hyperlinks.Count = 0) Then
Me.Hyperlinks.Add Target, Url & x, TextToDisplay:=TextToShow
End If
End If
End If
End If
End If
End Sub
Function GetNum(ByVal txt As String) As String
With CreateObject("VBScript.RegExp")
.Pattern = "\d+(?=(\-|$))"
If .Test(txt) Then GetNum = .Execute(txt)(0)
End With
End Function
Sub Open_Folder()
Dim strRoot As String
Dim strID As String
Dim strFolder As String
Dim nMaxDepth As Long
strID = ActiveCell.Value
If strID = "" Then Exit Sub 'User canceled input
strRoot = "V:\" ' Root path for all subfolders
If Right(strRoot, 1) <> "\" Then strRoot = strRoot & "\"
nMaxDepth = 2 ' Maximum search depth
strFolder = FindFolder("V:\0. Test-0", strID, 1, 1)
If strFolder = "" Then strFolder = FindFolder("V:\1. Test-1", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\2. Test-2", strID, 4, 1)
If strFolder = "" Then strFolder = FindFolder("V:\3. Test-2", strID, 3, 1)
If strFolder = "" Then strFolder = FindFolder("V:\4. Test-4", strID, 3, 1)
If strFolder = "" Then strFolder = FindFolder("V:\5. Test-5", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\6. Test-6", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\7. Test-7", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\8. Test-8", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\9. Test-9", strID, 2, 1)
If strFolder = "" Then strFolder = FindFolder("V:\10. Test-10", strID, 2, 1)
If strFolder <> "" Then
Shell "Explorer """ & strFolder & "", vbNormalFocus
Else
MsgBox strID & "...", , "No Folder Found"
End If
End Sub
Function FindFolder(ByVal strPath As String, ByVal strID As String, nMaxDepth As Long, nDepth As Long) As String
Dim strFolder As String
Dim fsoSubfolder As Object
If fso Is Nothing Then Set fso = CreateObject("Scripting.FileSystemObject")
DoEvents
If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
On Error Resume Next
' Test current folder
strFolder = Dir(strPath & strID & "*", vbDirectory)
If strFolder <> "" Then
FindFolder = strPath & strFolder
ElseIf nDepth < nMaxDepth Then
'Search sub folders
For Each fsoSubfolder In fso.GetFolder(strPath).SubFolders
FindFolder = FindFolder(fsoSubfolder.Path, strID, nMaxDepth, nDepth + 1)
If FindFolder <> "" Then Exit For
Next fsoSubfolder
End If
End Function
Bookmarks