Excel VBA宏根据单元格值调用多个宏

时间:2017-06-02 16:45:03

标签: excel vba excel-vba

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

换句话说,我想要的是,例如,如果D7范围内的任何或多个单元格:D446 =" 1000ABC"," 1000EFG"或者" 1000HIJ& #34;,列F7:F446中的任何/所有单元格将变为红色,以向用户指示他们需要在F7:F446中单击该单元格,当用户单击F列中的该单元格时,它将调用正确的我已经创建了宏。

示例:如果单元格D25 =" 1000EFG"单元格F25将变为红色,当用户单击单元格F25时,它将转到与值1000EFG相关联的宏。我已经创建了其他宏,我只需要将它们与此功能绑定在一起。 (这些值是假设的)

我遇到的问题是,无论单元格D中的值如何,当我单击F列中的关联单元格时,它将只带一个宏和一个宏(不是与值关联的正确宏)在细胞D)中。我也不确定如何根据值更改单元格颜色的语法。我似乎无法将这些功能集中在一个宏中。我将发布我在下面尝试过的代码。非常感谢 ANY 帮助。你们真棒,谢谢!

Sub gotorefs()

For Each c In Worksheets("JE").Range("D7:D446")

    If c.Value = "1000GP" Then
        Call gotoref1
     Worksheets("JE").Range("F7:F446").Select.ActiveCell.Interior.ColorIndex = 3

ElseIf c.Value = "1000MM" Then
        Call gotoref2
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

 ElseIf c.Value = "19FEST" Then
        Call gotoref3
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "20IEDU" Then
        Call gotoref4
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

 ElseIf c.Value = "20ONLC" Then
        Call gotoref5
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "20PART" Then
        Call gotoref6
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "20PRDV" Then
        Call gotoref7
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "20SPPR" Then
        Call gotoref8
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "22DANC" Then
        Call gotoref9
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "22LFLC" Then
        Call gotoref10
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "22MEDA" Then
        Call gotoref11
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "530CCH" Then
        Call gotoref12
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "60PUBL" Then
        Call gotoref13
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "74GA01" Then
        Call gotoref14
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "74GA17" Then
        Call gotoref15
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "74GA99" Then
        Call gotoref16
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3

ElseIf c.Value = "78REDV" Then
        Call gotoref17
        Worksheets("JE").Range("F7:F446").Cell.Interior.ColorIndex = 3
End If

Next c

End sub

1 个答案:

答案 0 :(得分:0)

尝试通过处理innerText --------- $31.00 $72.00 $209.95 来实现。为此,请将此添加到工作表" JE"代码模块

Worksheet_Change event

最后的建议,这种调度到这么多不同的例程的方式是乏味且容易出错的。您可能会想到遵循一些命名约定以使被调用的例程与值匹配。例如,如果您将例程命名为Private Sub Worksheet_Change(ByVal Target As Range) Dim c As Range: Set c = Range("D7:D446") For Each c In c.Cells Select Case c.Value Case "1000GP", "1000MM", "19FEST", "20IEDU", "20ONLC", "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, -2).Value2 Case "1000GP": gotoref1 Case "1000MM": gotoref2 Case "19FEST": gotoref3 Case "20PRDV": gotoref4 Case "20IEDU": gotoref5 Case "20ONLC": gotoref6 Case "20PART": gotoref7 Case "20SPPR": gotoref8 Case "22DANC": gotoref9 Case "22LFLC": gotoref10 Case "22MEDA": gotoref11 Case "530CCH": gotoref12 Case "60POUBL": gotoref13 Case "74GA01": gotoref14 Case "74GA17": gotoref15 Case "74GA99": gotoref16 Case "78REDV": gotoref17 End Select End If End Sub Ref_1000GP等,则第二个Ref_1000MM语句将缩减为单行,如下所示:

Select Case