[英]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.