I have values in column D of a sheet (Sheet2) in a workbook thats values are derived from a formula to get just the final part of longer values in column C cells. The longer values are automatically entered in column C thru a double-click. On a separate sheet (Sheet5), I have a list of values in the C column of that sheet. If any of the values in column D of Sheet2 equal any of the values in column C of Sheet5, I want the cell in the F column of Sheet2 to turn red. Once the F column cell in Sheet2 is red, depending on the value in column D, I want the red colored cell to become clickable which will take the user to a different sheet listing reference values associated with the value in column D. The code below does just that. However I would like to avoid hard coding the different values that need reference codes in anticipation that some of the values which require a reference code, will be added or taken away from the list in column C of Sheet5 in the future.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim c As Range: Set c = Union(Range("D7:D446"), Range("F7:F446"))
Dim CellF As Range, CellD As Range, Cell As Range
If Not Application.Intersect(c, Range(Target.Address)) Is Nothing Then
Set CellF = Range("F" & Target.Row)
Set CellD = Range("D" & Target.Row)
If CellF.Value <> "" And getMacroDictionary.Exists(CellD.Text) Then
CellF.Interior.ColorIndex = 3
Else
CellF.Interior.ColorIndex = 0
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim key As String, dic As Object
Set sourceRange = Nothing
If Target.Column = 6 And Target.Cells.Count = 1 And Target.Interior.ColorIndex = 3 Then
Set sourceRange = Target
Set dic = getMacroDictionary
key = Target.Offset(0, -2).Value2
If dic.Exists(key) Then Application.Run dic(key)
End If
End Sub
Function getMacroDictionary() As Object
Dim dic As Object
Set dic = CreateObject("SCripting.Dictionary")
dic.Add "1000GP", "gotoref1"
dic.Add "1000MM", "gotoref2"
dic.Add "19FEST", "gotoref3"
dic.Add "20IEDU", "gotoref4"
dic.Add "20ONLC", "gotoref5"
dic.Add "20PART", "gotoref6"
dic.Add "20PRDV", "gotoref7"
dic.Add "20SPPR", "gotoref8"
dic.Add "22DANC", "gotoref9"
dic.Add "22LFLC", "gotoref10"
dic.Add "22MEDA", "gotoref11"
dic.Add "530CCH", "gotoref12"
dic.Add "60PUBL", "gotoref13"
dic.Add "74GA01", "gotoref14"
dic.Add "74GA17", "gotoref15"
dic.Add "74GA99", "gotoref16"
dic.Add "78REDV", "gotoref17"
Set getMacroDictionary = dic
End Function
I thought the best way to go about avoiding hard-coding was to create an array. In the first sub you'll see the commented out snip-its of code, I tried to do that but, I am unsure of how I can call the different macros since they are associated with certain values.
Thanks in advance for any help!