搜索多个表后突出显示

时间:2018-11-30 22:18:11

标签: excel vba

这个问题很长。 我在诸如sheet1的12个表中都有SKU,表标题是该产品所需的材料。 我在另一张纸上列出了所需的材料清单(Sheet2)。 我在Sheet2中为SKU创建了一个下拉菜单,突出显示了该SKU所需的材料。

单个SKU可以出现在多个表中。 我之所以采用这种方法,是因为更新表中的SKU比所需的材料容易。

我已经编写了用于突出显示所需材料的代码,并且只要我从下拉列表中选择SKU,该代码就已经可以运行。

我希望以下代码在多个表格中搜索同一SKU,以突出显示该SKU所需的所有材料。 可以做到吗?

由于我已经搜索了很多类似的东西,因此非常感谢任何建议和帮助。

下面是突出显示的代码:

Sub Hilight(RNG As range, Hilight As Boolean)
        With RNG.Interior
            If Hilight Then
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = RGB(100, 250, 150)
                .TintAndShade = 0
                .PatternTintAndShade = 0
            Else
                .Pattern = xlNone
                .PatternTintAndShade = 0
            End If
        End With
    End Sub

这是我Sub的使用方式

Dim L8Product As String
Dim L11Product As String
Dim L12Product As String
Dim L10Product As String
Dim i As Long
Dim L8Rnge As range
Dim L11Rnge As range
Dim L12Rnge As range
Dim L10Rnge As range

L11Product = range("Line11_P")
L12Product = range("Line12_P")
L10Product = range("Line10_P")
L8Product = range("Line8_P")

If Not Intersect(Target, Me.range("Line8_P, Line11_P, Line12_P, Line10_P")) Is Nothing Then
        Hilight range("Line8_Hilight_Mon, Line8_Prep_Mon, Line11_Hilight_Mon, Line12_Hilight_Mon, Line10_Hilight_Mon"), False
'Below Code searches in the Table and then highlights the appropriate cells
    If Trim(L8Product) <> "" Then
        With Sheets("Products").range("KP_Table")                     'searchs in the KP Table on Sheet Overtime_Pos_Table

            'The code below will search the KP table for the product that you will select from the Line 8 drop down
            Set L8Rnge = .Find(what:=L8Product, _
                            after:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            lookat:=xlWhole, _
                            searchorder:=xlByRows, _
                            searchdirection:=xlNext, _
                            MatchCase:=False)
            If Not L8Rnge Is Nothing Then
                    Hilight range("KP_Hilight_Mon, Line8_Prep_Mon"), True               'Hilights the cells for the KP and the Prep material required
            Else: With Sheets("Products").range("Osgood_Table")
                    Set L8Rnge = .Find(what:=L8Product, _
                                    after:=.Cells(.Cells.Count), _
                                    LookIn:=xlValues, _
                                    lookat:=xlWhole, _
                                    searchorder:=xlByRows, _
                                    searchdirection:=xlNext, _
                                    MatchCase:=False)
                    If Not L8Rnge Is Nothing Then
                            Hilight range("Osgood_Hilight_Mon, Line8_Prep_Mon"), True   'Hilights the cells for the Osgood and the Prep material required
                    End If
                  End With
            End If
        End With
    Else: Hilight range("Line8_Hilight_Mon, Line8_Prep_Mon"), False
    End If
End If

0 个答案:

没有答案