Clear Cells with Certain Criteria after specific date in VBA

Kobus1264

New Member
Joined
Sep 10, 2015
Messages
6
Hi,

I am Fairly new to Excel VBA Coding, I would like to know what coding can I use if I want to clear a range of cells with certain criteria after a specific date. The cell's ranges are G5:J23 that I want to clear, I want to clear them if they contain "O/off" or "Weekly", but the cells containing "Monthly" must remain the same. I want to clear the cells after the 2nd of each month which I have a cell for ( B1 ) which only displays the day of the month ( eg. 2 ). This is all for a budget sheet I am creating for myself. If someone can help me with the code that would be able to do this or any other codes that will do the same I will greatly appreciate it. Thank you
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How far have you got with attempting this? I think the way you have asked the question is a little misleading as I doubt the "O/off" and "Weekly" etc are in the cells in the range you mention
 
Upvote 0
I have no Idea on how to code this as I have only knowledge of Visual Basic coding and it looks almost the same, I see what you mean with misleading, I want a formula or VBA code that can clear all these cells, if the cells I5:I23 have the criteria of "Weekly" or "O/off" but not Monthly. so I want to clear these rows ( from 5 - 23 and only the Columns G - J )
 
Upvote 0
Simplest code would be along the lines of

Dim iRow As Integer

For iRow = 5 To 23

If Range("i" & iRow).Value = "Weekly" Or Range("i" & iRow) = "O/off" Then
Range("g" & iRow & ":j" & iRow).ClearContents
End If

Next iRow
 
Upvote 0
Thank you, I will test this code soon. Can you also please assist me, I want this code to run on the second of each month, I have a cell in my sheet set out for this as it only indicates the day of the month ( eg. 2 ). Something like:

Dim iRow As Integer
Dim iCell As integer

For iRow = 5 To 23

For iCell = 1

If cell("a" & iCell).value = "2" then
Range("i" & iRow).Value = "Weekly" Or Range("i" & iRow) = "O/off" Then
Range("g" & iRow & ":j" & iRow).ClearContents
End If

Next iRow

I don't know if any variable can be used as an integer so I just used "iCell" as an example. I hope what I did above gives you some clarity on what I want to do as that is the only way I know how to explain it
 
Upvote 0
This is the code I have thus far:

Dim iRow As Integer
Dim iCell As Integer


For iRow = 5 To 23
For iCell = 1 To 1


If Range("a" & iCell).Value = "2" Then
If Range("i" & iRow).Value = "Weekly" Or Range("i" & iRow) = "O/off" Then
Range("g" & iRow & ":j" & iRow).ClearContents
End If
End If

But as soon as I run it, it gives the error "compile error: For without next"
Can someone please help with this as I can't seem to find a solution
 
Upvote 0
I got it, works perfectly everytime. Thank you. Here is my final code:

Sub DeleteCells()
Dim iRow As Integer
Dim iCell As Integer


For iRow = 5 To 23
For iCell = 1 To 1


If Range("a" & iCell).Value = "2" Then
If Range("i" & iRow).Value = "Weekly" Or Range("i" & iRow) = "O/off" Then
Range("g" & iRow & ":j" & iRow).ClearContents
End If
End If
Next iCell
Next iRow

End Sub

Thank you WaterGypsy for your help
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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