Sub Create_Hyperlinks()
Dim JPGList As Worksheet, _
DESTSHT As Worksheet, _
NameList As Range, _
TestName As Range, _
AnchorCell As Range, _
Structure As String, _
ESTName As String, _
IDName As String, _
ESTandID As Boolean, _
Ctrl As Long, _
RevNum As Long, _
RevRow As Long, _
RevCols As Variant
'Call GetFileNames ' delete this line
'---------------------------------------------------------------
Dim fs, fol, fil, FileCount
FileCount = 1
Set fs = CreateObject("Scripting.filesystemobject")
Set fol = fs.getfolder("\\abcdefg.com\workgroup\TDBU26\E&TS DR\Working\Quality\Inspections\M&I_Inspections\Photos_ODI\05 May\David Butler\") 'Change This
ThisWorkbook.Worksheets.Add after:=Sheets(Sheets.Count)
ActiveSheet.Name = "jpg list"
Set JPGList = Sheets("jpg list")
For Each fil In fol.Files
JPGList.Range("A" & FileCount).Value = fil.Name
JPGList.Range("B" & FileCount).Value = fil.Path
FileCount = FileCount + 1
Next fil
FileCount = FileCount - 1
With JPGList
.Sort.SortFields.Clear
.Sort.SortFields.Add _
Key:=Range("A1"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
End With
With JPGList.Sort
.SetRange Range("A1:A" & FileCount)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
JPGList.Range("C1").Formula = "=LEFT(A1,9)=LEFT(A2,9)"
Range("C1").Select
Selection.AutoFill _
Destination:=JPGList.Range("C1:C" & FileCount)
'JPGList.Range("D1").Value = FileCount
'---------------------------------------------------------------
RevCols = Array("", "M", "O", "Q", "S")
Set JPGList = Sheets("jpg list")
Set DESTSHT = Sheets("CheckSheet")
'FileCount = JPGList.Range("D1").Value
Set NameList = JPGList.Range("A1:A" & FileCount)
For Ctrl = 1 To FileCount
Structure = Left(JPGList.Cells(Ctrl, "A").Value, 8)
ESTandID = JPGList.Cells(Ctrl, "C").Value
Set AnchorCell = Nothing
ESTName = ""
IDName = ""
Select Case ESTandID
Case Is = True 'both est and id names are present in the list
Set AnchorCell = DESTSHT.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
AnchorCell.Value = Structure
RevRow = AnchorCell.Row
ESTName = Left(JPGList.Cells(Ctrl, "A").Value, Len(JPGList.Cells(Ctrl, "A").Value) - 4)
IDName = Left(JPGList.Cells(Ctrl + 1, "A").Value, Len(JPGList.Cells(Ctrl + 1, "A").Value) - 4)
With DESTSHT
.Hyperlinks.Add _
Anchor:=AnchorCell.Offset(0, 8), _
Address:=JPGList.Cells(Ctrl + 1, "B").Value, _
TextToDisplay:=IDName
.Hyperlinks.Add _
Anchor:=AnchorCell.Offset(0, 9), _
Address:=JPGList.Cells(Ctrl, "B").Value, _
TextToDisplay:=ESTName
End With
Ctrl = Ctrl + 1
Case Is = False 'either est or or id or both are missing
' est is present not id
If InStr(JPGList.Cells(Ctrl, "A").Value, "est") > 0 Then
Set AnchorCell = DESTSHT.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
AnchorCell.Value = Structure
RevRow = AnchorCell.Row
ESTName = Left(JPGList.Cells(Ctrl, "A").Value, Len(JPGList.Cells(Ctrl, "A").Value) - 4)
DESTSHT.Hyperlinks.Add _
Anchor:=AnchorCell.Offset(0, 9), _
Address:=JPGList.Cells(Ctrl + 1, "B").Value, _
TextToDisplay:=ESTName
'id is present not est
ElseIf InStr(JPGList.Cells(Ctrl, "A").Value, "id") > 0 Then
Set AnchorCell = DESTSHT.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
AnchorCell.Value = Structure
IDName = Left(JPGList.Cells(Ctrl + 1, "A").Value, Len(JPGList.Cells(Ctrl + 1, "A").Value) - 4)
RevRow = AnchorCell.Row
DESTSHT.Hyperlinks.Add _
Anchor:=AnchorCell.Offset(0, 8), _
Address:=JPGList.Cells(Ctrl + 1, "B").Value, _
TextToDisplay:=IDName
'neither est nor id is present, so check for a single digit in parentheses
'indicating a revision # present
ElseIf JPGList.Cells(Ctrl, "A").Value Like "*(#)*" Then
' get the revision number as pointer to the destination column
RevNum = Left(Right(JPGList.Cells(Ctrl, "A").Value, 6), 1)
DESTSHT.Hyperlinks.Add _
Anchor:=DESTSHT.Cells(RevRow, RevCols(RevNum)), _
Address:=JPGList.Cells(Ctrl + 1, "B").Value, _
TextToDisplay:=Replace(UCase(JPGList.Cells(Ctrl, "A").Value), ".JPG", "")
End If
End Select
Next Ctrl
Application.DisplayAlerts = False
JPGList.Delete
Application.DisplayAlerts = True
End Sub
Bookmarks