Excel VBA-创buildmacros来更改单元格颜色并根据单元格值调用其他macros

我正在尝试编写一个调用不同的macros和更改单元格颜色的macros。 所以,如果整个列D(D4:D446)中的一个或多个单元格等于某个值,则这个macros将调用与该值相关联的单独的macros。

换句话说,我想要的是,例如,如果F7:F446列中的任何/所有单元格(D7:D446)=“1000ABC”,“1000EFG”或“1000HIJ”中的任何一个或多个单元格将变为红色向用户表明他们需要在F7:F446中单击该单元格,并且当用户单击列F中的该单元格时,它将调用已经创build的正确的macros。

例如:如果单元格D25 =“1000EFG”单元格F25将变为红色,并且当用户将光标移到单元格F25上并单击单元格F25时,它将把它们带到与值1000EFG相关的macros。 我已经创build了与这些特定值相关联的其他macros,我只需要F列中的单元格来改变颜色,并成为可点击的用户和点击时,调用一个特定的macros。 我将发布我在下面尝试的代码。 很感谢任何forms的帮助。 你们真棒,谢谢!

Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) Dim c As Range c = Range("D7:D446") For Each c In Intersect(ActiveCell, Range("D7:D446")) 'this is where the error is occuring Select Case c.Value Case "1000GP", "1000MM", "19FEST", "20IEDU", "20PART", "20PRDV", "20SPPR", "22DANC", "22LFLC", "22MEDA", "530CCH", "60POUBL", "74GA01", "74GA17", "74GA99", "78REDV" Cells(c.Row, "F").Interior.ColorIndex = 3 Case Else Cells(c.Row, "F").Interior.ColorIndex = 0 End Select Next c End Sub Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Column = 6 And Target.Cells.Count = 1 And Target.Interior.ColorIndex = 3 Then Cancel = True ' Now call the appropriate routine according to column C Select Case Target.Offset(0, -3).Value2 Case "1000GP": gotoref1 Case "1000MM": gotoref2 Case "19FEST": gotoref3 Case "20IEDU": gotoref4 Case "20ONLC": gotoref5 Case "20PART": gotoref6 Case "20PRDV": gotoref7 Case "20SPPR": gotoref8 Case "22DANC": gotoref9 Case "22LFLC": gotoref10 Case "22MEDA": gotoref11 Case "530CCH": gotoref12 Case "60PUBL": gotoref13 Case "74GA01": gotoref14 Case "74GA17": gotoref15 Case "74GA99": gotoref16 Case "78REDV": gotoref17 End Select End If End Sub 

调整Worksheet_Change事件对于此结构:

 Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("D7:D446")) is Nothing Then Dim c As Range For Each c In Target Select Case c.Value Case "1000GP", "1000MM", "19FEST", "20IEDU", "20PART", "20PRDV", "20SPPR", "22DANC", "22LFLC", "22MEDA", "530CCH", "60POUBL", "74GA01", "74GA17", "74GA99", "78REDV" Cells(c.Row, "F").Interior.ColorIndex = 3 Case Else Cells(c.Row, "F").Interior.ColorIndex = 0 End Select Next c End If End Sub