1

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!

3
  • Why don't you put the values "0GP", "0MM", "FEST" etc in a hidden sheet in the workbook itself, and use that to drive the code? Next to each one you could place its reference code. You don't need to hard-code data, Excel is the place for data, Jul 7, 2017 at 20:43
  • @RobSedgwick I do have that. Each account code I have listed like "0GP, has many potential reference codes. So, each code I have hard-coded above, has it's own sheet listing their many reference codes. The reference codes will be housed in the F column cells so my goal is to have the F column cells turn red to indicate that account code in the D column needs a reference code and once the F column cell is red, it will become clickable and upon the click, it will call the macro which will take the user to that hidden sheet which lists all the account codes associated with that value.
    – anve
    Jul 10, 2017 at 15:21
  • Well you could loop through the worksheets collection and look at sheet.Name rather hard-code all the sheet names. As far as I understand you second sub you just want to go to the sheet with that name, so something like Worksheets(sheet).Activate should do it? Jul 10, 2017 at 20:31

0

You must log in to answer this question.

Browse other questions tagged .