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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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