looping & testing

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

looping & testing

Post by dasadler »

Having learned how to determine the last row of worksheet. I now am trying to sort the worksheet (done successfully) then compare the date cell in each row to today's date. If the date cell is in the past, I want to color some cells in that row gray. Here is the code I am using but I continue to get a syntax error. I don't think I am that far off but cannot find the solution.

Code: Select all

Sub doit()
 Dim LastRow As Long

LastRow = Range("A65536").End(xlUp).Row
              Range("A" & LastRow + 1).Value = LastRow
Range("A1:R1000").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes
For i = 2 To LastRow
If "Bi" < today() Then Range("Ai":"Ri").Select
   
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = -0.149998474074526
        .PatternTintAndShade = 0
    End With
    Next
    End If
    End Sub
Don

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: looping & testing

Post by VegasNath »

Try:

If "B" & i < today() Then Range("A" & i & ":R" & i).Select
:wales: Nathan :uk:
There's no place like home.....

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: looping & testing

Post by dasadler »

Thanks but I get an error - see image
You do not have the required permissions to view the files attached to this post.
Don

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: looping & testing

Post by VegasNath »

I think this may be (roughly) what you are looking for?

Code: Select all

Option Explicit

Sub doit()

Dim LastRow As Long
Dim i As Long

LastRow = Range("A65536").End(xlUp).Row
Range("A" & LastRow + 1).Value = LastRow
Range("A1:R1000").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes

For i = 2 To LastRow
    If Range("B" & i) < Date Then
        With Range("A" & i & ":R" & i).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.149998474074526
                .PatternTintAndShade = 0
        End With
    End If
Next

End Sub

:wales: Nathan :uk:
There's no place like home.....

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: looping & testing

Post by dasadler »

Thank you. Perfect - so the problem was that today() is not used by VBA? That DATE is used instead?
Don

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: looping & testing

Post by VegasNath »

dasadler wrote:Thank you. Perfect - so the problem was that today() is not used by VBA? That DATE is used instead?
Yes, Date is the VBA equivalent of the worksheet function today().
Also, you had your Next before 'End If'
In addition, I removed the selecting and correctly indented the loop.

Glad to be of help.
:wales: Nathan :uk:
There's no place like home.....

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: looping & testing

Post by dasadler »

LOL - I guess I was further off than I thought. Thank you so much for the help.
Don

User avatar
StuartR
Administrator
Posts: 12615
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: looping & testing

Post by StuartR »

dasadler wrote:...I now am trying to ... compare the date cell in each row to today's date. If the date cell is in the past, I want to color some cells in that row gray...
This is very easy to do using just conditional formatting, with no code at all.
StuartR


dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: looping & testing

Post by dasadler »

That is how I was originally doing it... along with a lot of other testing and formatting... all CF based. However, I found that as I inserted rows & deleted rows then replicated formulas, it became awkward and I found myself having to delete all CF then manually reapply the various formats.

I thought I might be able to do it with a macro but that was problematic so i concluded that with only a few rows (1000), I could easily (relative term) do the sorting, testing, and formatting with a macro by a single click. So now I am trying to build the code to do it... part of it is done - sorting then graying out the past dates. My next challenge is to expand the cell testing & formatting within the loop.

I will likely (no... certainly) return with more questions.
Don

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: looping & testing

Post by VegasNath »

As Steve mentioned in your other thread, it would be better to use 'Rows.Count' instead of 65536, to ensure accuracy in versions 2007 upwards. Also, you may wish to use 'LastRow' in your sort instead of 1000. Like so:

Code: Select all

Option Explicit

Sub doit()

Dim LastRow As Long
Dim i As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row
Range("A" & LastRow + 1).Value = LastRow
Range("A1:R" & LastRow).Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes

For i = 2 To LastRow
    If Range("B" & i) < Date Then
        With Range("A" & i & ":R" & i).Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorDark1
                .TintAndShade = -0.149998474074526
                .PatternTintAndShade = 0
        End With
    End If
Next

End Sub
As a side note, if in future you wish to delete rows within your loop, you should loop backwards by changing:

For i = 2 To LastRow
to:
For i = LastRow To 2

:whisper:This is something that took me a while to get my head around in my early days. My lounge membership has served me well. :grin:
:wales: Nathan :uk:
There's no place like home.....

dasadler
5StarLounger
Posts: 889
Joined: 25 Jan 2010, 16:26
Location: Garden Grove, CA 92844 USA

Re: looping & testing

Post by dasadler »

What i meant by deleting rows is something I do manually as needed. This worksheet is a sort of schedule of paid events that contains, initially, one row per event. As I determine what other vendors and contractors I need for these events, I insert rows and use the same date & event number with one row per vendor/contractor (meaning possibly multiple rows per event). If the event is later canceled or downsized, I delete those rows no longer needed.

In this case, why would it make a difference looping backwards? I defer to your experience and will make the change... I would just like to understand the benefit/advantage. or, at least, the disadvantage of not looping backwards.
Don

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: looping & testing

Post by VegasNath »

dasadler wrote:In this case, why would it make a difference looping backwards? I defer to your experience and will make the change... I would just like to understand the benefit/advantage. or, at least, the disadvantage of not looping backwards.
There is no need to loop backwards unless you are intending to delete rows within the loop. See Hans tutorial on looping backwards. I mentioned the matter as a side note, just in case.
:wales: Nathan :uk:
There's no place like home.....