Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear cell that contains a certain value

Status
Not open for further replies.

yoshi88

Technical User
Mar 18, 2001
48
CA
Hi I am trying to use a code to clear cells if their value is the same as the P1 cell. The if part doesn't seem to work and I can not find what is wrong.

I dont want to use the find and replace option in Excel.

Range("D1").Select
Do Until Range("D50").Select
If ActiveCell.Value = ("P1") Then
ActiveCell.Value = ""
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(1, 0).Select
End If
Loop

Thanks

Frank
 
Various ways of dealing with this, but here are a couple:-

Sub ClearCells()

Dim r As Long
Dim LastRow As Long
Application.ScreenUpdating = False

LastRow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row
For r = 1 To LastRow
If StrComp(Cells(r, "D"), Range("P1"), vbBinaryCompare) = 0 Then
Cells(r, "D").ClearContents
Else: End If
Next r

Application.ScreenUpdating = True
End Sub
Sub ClearCells2()

Dim r As Long
Dim LastRow As Long
Dim cel As Range
Application.ScreenUpdating = False

Set Comp = Range("P1")
Set Rng = Range("D1:D50")

For Each cel In Rng
If StrComp(cel, Comp, vbBinaryCompare) = 0 Then
cel.ClearContents
Else: End If
Next cel

Application.ScreenUpdating = True
End Sub

Always try and avoid selecting if possible.

Regards
Ken.....................

----------------------------------------------------------------------------
[peace]It's easier to beg forgiveness than ask permission[2thumbsup]

----------------------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top