For Each - Invalid Range

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I'm trying to use a For Each macro to erase dates in a range that are greater than Today. The range is C3 and D3 to Lr (I have Lr set earlier in the macro and it works other places for the same purpose). I keep getting an invalid range when I use:

Code:
Dim c As Range

For Each c In ws2.range("C3:D" & Lr)
        If c.value > Today Then
              c.value = ""
        Else
        End If
Next

But if I replace the range with "ws2.range("C:C")", it works for that range.

I'm probably making a very rookie mistake. Greatly appreciate any help. Also, I'm very open to alternative methods of accomplishing the same idea if someone has a better method.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Can you supply ALL the code so we don't rewrite it differently to what you already have please??
 
Upvote 0
Unfortunately I don't have it with me. This file is stored on a high-security network at work with no access to the internet. I can only get to the internet from home. I can print out the code and type the whole thing in (I usually do for my more complex questions), but was hoping that someone could help with this snippet by itself. Sorry
 
Upvote 0
Try using Date rather than today....which isn't a valid VBA function
If it's a Date AND Time use Now() instead of date

Code:
Sub MM1()
Dim c As Range, lr As Long, ws2 As Worksheet
Set ws2 = Sheets("Sheet1")
lr = Cells(Rows.Count, "D").End(xlUp).Row
For Each c In ws2.Range("C3:D" & lr)
        If c.Value > Date Then
              c.Value = ""
        Else
        c.Value = "ok"
        End If
Next
End Sub
 
Upvote 0
That seems oddly similar to what I was using, but had trouble with. I will double check it and try anything different I see. Looks like it should work! Thanks!
 
Upvote 0
Don't forget to change
Code:
 c.Value = "ok"
to whatever is relevent to you
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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