delete cells based on text value of adjacent cell

Tony12zz

Board Regular
Joined
Feb 21, 2004
Messages
56
I am trying to get the following code to delete cells if the cell in column A has a certain value and the cell next to it in column B is equal to 0.75, but it is not working properly

Any ideas?

Sub Deleter1()

LastRow = Range("A65536").End(xlUp).Row

For x = LastRow To 1 Step -1

If Range("A" & x).Value = "Test" And Range("B" & x).Value = 0.75 Then _
Range("A" & x).Select
Selection.ClearContents
Range("B" & x).Select
Selection.ClearContents


Next x

End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
deleting cells or clearing the contents is not the same

at the end of the "IF"statement you putted a "_"
==> your macro only executes OR the first line under OR the next three lines

Code:
you probably ment to write
If Range("A" & x).Value = "Test" And Range("B" & x).Value = 0.75 Then 
Range("A" & x).Select 
Selection.ClearContents 
Range("B" & x).Select 
Selection.ClearContents
end if

cleaning up your code, you have

Code:
Sub Deleter1()

Lastrow = Range("A65536").End(xlUp).Row

For x = Lastrow To 1 Step -1
If Range("A" & x).Value = "Test" And Range("B" & x).Value = 0.75 Then
Range("A" & x, "B" & x).ClearContents
End If
Next x

End Sub

do you want to delete than write delete instead of clearcontents. Celss will be deleted and cells under will "climb up"

If there is a lot of rows to do post back for faster code
but it would be good to first understand what this code does
go therefor in the VBAeditor, click in your macro, anywhere and push the function button F8 to see line per line what happens

hope this will help you,
Erik
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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