VBA Clear Cell if

Sparda142

Board Regular
Joined
Dec 19, 2018
Messages
52
Hello,

I'm new to VBA and need some help. Im bascially trying to say - look in column C, if the value is "State ID" then clear contents


Sub ClearRowWith_ID()
Last = Cells(Rows.Count, "C").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "C").Value) = "State ID" Then

End If
Next i
End Sub


<tbody>
</tbody>
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
No need for a loop.
Code:
Sub Sparda142()
   Range("C:C").Replace "State ID", "", xlWhole, , False, , False, False
End Sub
 
Upvote 0
Try:
Code:
[TABLE="class: cms_table"]
<tbody>[TR]
[TD]Sub ClearRowWith_ID()

Last = Cells(Rows.Count, "C").End(xlUp).Row
For i = 1 to Last
    If Cells(i, "C").Value = "State ID" Then Cells(i, "C").ClearContents   
Next i

End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Note that you do not have to iterate threw the rows backwards if you are just clearing cells and not deleting rows.


EDIT: Fluff's way is more efficient/faster, as it does not use loops.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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