Clear contents of cell if value is present in specified column on different excel sheet

jmk1153

New Member
Joined
Jun 27, 2018
Messages
14
Hello,

I see others have asked similar questions to this but what I've found is based on defined values within the code or a single cell on the same sheet opposed to referencing all values contained in a column on a different sheet. I'm wondering if there's an efficient way to code the following:

If any cell in specified range (columns E:H) contains value from column N on Sheet2, clear that cell value.

e.g. there would be multiple cells in columns E, F, G and H that contain words contained in column N on sheet 2 and I'm trying to clear the cells with those values.

This would allow users to adjust column N on sheet 2 themselves without having to alter the code itself.

Appreciate any help!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
If any cell in specified range (columns E:H) contains value from column N on Sheet2, clear that cell value.
By contains do you mean the words match in whole or it could be in part?
Could you give an example?
 
Upvote 0
By contains do you mean the words match in whole or it could be in part?
Could you give an example?
Ah, good question. To clarify, column N examples could be rad, kr, ply.

Some of the cells in columns E:H will match these words exactly and others will have additional characters but would still need to be identified and cleared.

Sheet1
E2: rad
E3: GGrad
F3: MMply

Sheet2
N1: rad
N2: kr
N3: ply
 
Upvote 0
If any cell in specified range (columns E:H) contains value from column N on Sheet2, clear that cell value.
Assuming data in both sheets start at row 2:

VBA Code:
Sub jmk1153_1()
Dim i As Long, j As Long, n As Long, k As Long
Dim va, vb

Sheets("Sheet1").Activate 'change sheet name to suit

n = Range("E:H").Find("*", LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row
va = Range("E2:H" & n)

With Sheets("Sheet2")
    vb = .Range("N2", .Cells(.Rows.Count, "N").End(xlUp))
End With

For k = 1 To UBound(vb, 1)
    For i = 1 To UBound(va, 1)
        For j = 1 To UBound(va, 2)
            If InStr(1, va(i, j), vb(k, 1), vbTextCompare) > 0 Then
                Debug.Print vb(k, 1) & " : " & i & " : " & j
                va(i, j) = ""
            End If
        Next
    Next
Next

Range("E2").Resize(UBound(va, 1), UBound(va, 2)) = va

End Sub
 
Upvote 0
Solution
Assuming data in both sheets start at row 2:

VBA Code:
Sub jmk1153_1()
Dim i As Long, j As Long, n As Long, k As Long
Dim va, vb

Sheets("Sheet1").Activate 'change sheet name to suit

n = Range("E:H").Find("*", LookIn:=xlValues, lookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row
va = Range("E2:H" & n)

With Sheets("Sheet2")
    vb = .Range("N2", .Cells(.Rows.Count, "N").End(xlUp))
End With

For k = 1 To UBound(vb, 1)
    For i = 1 To UBound(va, 1)
        For j = 1 To UBound(va, 2)
            If InStr(1, va(i, j), vb(k, 1), vbTextCompare) > 0 Then
                Debug.Print vb(k, 1) & " : " & i & " : " & j
                va(i, j) = ""
            End If
        Next
    Next
Next

Range("E2").Resize(UBound(va, 1), UBound(va, 2)) = va

End Sub
Thank you for the help with this!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,224,826
Messages
6,181,192
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top