Clearing cells based on other cell values

sschluet

New Member
Joined
Apr 5, 2013
Messages
12
I have the following code that trolls through 3 columns with some blank spaces in them and it gives me a list of values in I2:I5 that I'm looking for but the red highlighted part isn't working:

Sub common2()
Dim sh As Worksheet, lr As Long, rng As Range, Brng As Range, Crng As Range
Set sh = Sheets(6) 'Edit sheet name

If sh.Range("C1") Is Nothing Then
Range("I2:I5").ClearContents

Else

lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A1:A" & lr)
For Each C In rng
Set Brng = sh.Range("B:B").Find(C.Value, LookIn:=xlValues)
Set Crng = sh.Range("C:C").Find(C.Value, LookIn:=xlValues)
If Not Brng Is Nothing And Not Crng Is Nothing Then
sh.Cells(Rows.Count, 9).End(xlUp)(2) = C.Value
End If
Set Brng = Nothing
Set Crng = Nothing
Next

End If

End Sub

When I run this if there are previous values in I column then it writes the new values below the first rather than clearing them, which is not what I'm looking for.

The 3 columns that have data in them get it from VLOOKUP because I'm very new to VBA and don't really know what I'm doing and I have a feeling that they are not returning "Nothing" to the code so that's why it's not working:

=IFERROR(IF(VLOOKUP(Searching_problem_AND_equip!$B$15,Source!$X$3:$AG$13,2,FALSE)=0,"",VLOOKUP(Searching_problem_AND_equip!$B$15,Source!$X$3:$AG$13,2,FALSE)),"")

Could someone help me get this piece working so my answers don't repeat?
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This is simply testing if the reference is a Valid Range..Not if the range is empty..
If sh.Range("C1") Is Nothing Then

instead, try
If sh.Range("C1").Value = "" Then
 
Upvote 0
Oh wait, you need the sh reference on both lines

If sh.Range("C1").Value = "" Then
sh.Range("I2:I5").ClearContents
 
Upvote 0

Forum statistics

Threads
1,216,110
Messages
6,128,890
Members
449,477
Latest member
panjongshing

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