繁体   English   中英

Excel VBA查找/突出显示,但检查相邻单元格的值

[英]Excel VBA Find/Highlight, but check value of adjacent cell

底部修订

我有以下代码,用于在大型excel文档中搜索数字列表,如果找到,则突出显示该单元格。 数字存储在单独的excel文档中,因此“ For i =#到##”和“ With Workbookx(“ $$$$”)。Sheets(“ %%%%”)“”中的符号是根据他们使用此宏的哪些文档进行替换:

Sub PNsToRemove()

    Dim i       As Integer
    Dim findStr As String
    Dim ws      As Worksheet
    Dim lColor    As Long

    lColor = RGB(211, 211, 211)

    'Set i = # to ### to the first and last row numbers of the P/N list
    For i = # To ###

        'set $$$$$ to name of the excel sheet containing the pn's
        'set %%%%% to the sheet name that the pn's are on
            With Workbooks("$$$$$").Sheets("%%%%%")

                'set @ to the column that contains the pn's
                findStr = .Range("@" & i).Value

            End With

            For Each cell In Intersect(Sheets("&&&&").Range("A:Z"), Sheets("&&&&").UsedRange)

            If InStr(cell.Value, findStr) > 0 Then
                cell.Interior.Color = lColor

            End If

            Next
    Next

End Sub

前几天我遇到了一种情况,在突出显示该单元格之前,我需要检查与其相邻的单元格的值。 它将在同一行上,但在左侧的1到3列之间。 有一个字符串列表,如果存在于相邻的单元格中,则应避免宏突出显示包含该数字的单元格。 我怀疑If InStr(cell.value, findStr) > 0 Then会出现一些代码If InStr(cell.value, findStr) > 0 Then有条件的,但是不确定如何进行。

谢谢! 杰米

改版

我能够使它适用于prefix1中的值,但是2至4将不起作用(感谢对cell.offset()Siddharth的评论!)。 我是否缺少InStr函数的功能? 我读过的所有内容都表明它对于数字搜索应该可以正常工作:

            prefix1 = "DG"
            prefix2 = "70"
            prefix3 = "72"
            prefix4 = "73"

            For Each cell In Intersect(Sheets("CAT. NO.").Range("I:BO"), Sheets("CAT. NO.").UsedRange)

            If InStr(cell.Value, findStr) > 0 Then


                If InStr(cell.Offset(-1).Value, prefix1) = 0 Then
                If InStr(cell.Offset(-1).Value, prefix2) = 0 Then
                                'debug1 = InStr(cell.Offset(-1).Value, prefix1)
                                'Range("U6604").Value = debug1
                If InStr(cell.Offset(-1).Value, "72") = 0 Then
                If InStr(cell.Offset(-1).Value, "73") = 0 Then
                     cell.Interior.Color = lColor
                     cell.Offset(ColumnOffset:=-1).Interior.Color = lColor
                     cell.Offset(ColumnOffset:=1).Interior.Color = lColor
                     cell.Offset(ColumnOffset:=2).Interior.Color = lColor


                End If
                End If
                End If
                End If

            End If

我的确在If / And行中包含了所有If语句,但是更容易处理。

谢谢!

未测试

不太确定# to ##的工作方式-无疑会有更好的方法动态地添加这些数字,但这不在此问题的范围内,因此我将忽略。

尝试在选择的情况下包装if语句:

Sub PNsToRemove()

    Dim i       As Integer
Dim findStr As String
Dim ws      As Worksheet
Dim lColor    As Long
Dim chkStr1 As String, chkStr2 As String, chkStr3 As String

chkStr1 = "Check for this"
chkStr2 = "Or For This"
chkStr3 = "Or Even for This"

lColor = RGB(211, 211, 211)

'Set i = # to ### to the first and last row numbers of the P/N list
For i = # To ###

    'set $$$$$ to name of the excel sheet containing the pn's
    'set %%%%% to the sheet name that the pn's are on
        With Workbooks("$$$$$").Sheets("%%%%%")

            'set @ to the column that contains the pn's
            findStr = .Range("@" & i).Value

        End With

        For Each cell In Intersect(Sheets("sheet1").Range("A:Z"), Sheets("sheet1").UsedRange)
            Select Case (cell)
                Case cell.Offset(0, 1) = chkStr1, chkStr2, chkStr3
                    GoTo MoveOn
                Case cell.Offset(0, 2) = chkStr1, chkStr2, chkStr3
                    GoTo MoveOn
                Case cell.Offset(0, 3) = chkStr1, chkStr2, chkStr3
                    GoTo MoveOn
                Case Else
                    If InStr(cell.Value, findStr) > 0 Then
                        cell.Interior.Color = lColor
                    End If
            End Select
MoveOn:
            Next
        Next

End Sub

我们在顶部为要检查的字符串声明并分配字符串变量。

我们检查左边的一个单元格是否与任何检查字符串匹配的情况-如果是这样,我们将跳过选择的情况并继续执行下一个单元格而没有任何操作,否则我们将移至下一个条件。

我们检查左边两个单元格是否与任何检查字符串匹配的情况-如果是这样,我们会跳出选择条件,然后继续执行下一个单元格而没有任何操作,否则我们将移至下一个条件。

我们检查左边的三个单元格是否与任何检查字符串匹配的情况-如果是,我们将跳过选择的情况并继续执行下一个单元格,而无需执行任何操作,否则我们移至其他情况。

仅当我们要检查的单元格左侧的1到3之间的任何单元格都没有与要检查的任何字符串匹配时,我们才运行if语句并应用格式化(如果其结果为true)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM