Finding Last Saturday in Range

tjlaser shepard

New Member
Joined
Jun 1, 2016
Messages
7
I am writing a macro to try and sort some payroll information. On the worksheet, column "I" contains the "payroll end dates" for accounting records in the spreadsheet. I have used the following section of code to find the max date column "I" and assign it to a variable:

Dim lastEndDate As Long

With ThisWorkbook.Sheets("100")

lastEndDate = WorksheetFunction.Max(Range("I:I"))

End With

This works fine for finding the latest date, but I need a way to make it more specifically the latest Saturday. Is there someway to incorporate the WEEKDAY function or something else to specify that the date stored in lastEndDate must be a Saturday? Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
See if this works:

Code:
Set sh = Sheets("100")
lr = sh.Range("I" & Rows.Count).End(xlUp).Row

lastEndDate = Evaluate("=MAX(IF(ISNUMBER('" & sh.Name & "'!I1:I" & lr & "),IF(WEEKDAY('" & sh.Name & "'!I1:I" & lr & ")=7,'" & sh.Name & "'!I1:I" & lr & "),0))")
 
Upvote 0
Can you do anything like that with the sheet name too? I put the isnumber in to prevent errors with text.
 
Upvote 0
See if this works:

Code:
Set sh = Sheets("100")
lr = sh.Range("I" & Rows.Count).End(xlUp).Row

lastEndDate = Evaluate("=MAX(IF(ISNUMBER('" & sh.Name & "'!I1:I" & lr & "),IF(WEEKDAY('" & sh.Name & "'!I1:I" & lr & ")=7,'" & sh.Name & "'!I1:I" & lr & "),0))")
You can write your Evaluate code line a little more compactly like this...
Code:
lastEndDate = Evaluate(Replace(Replace("MAX(IF(WEEKDAY('@'!I1:I#)=7,'@'!I1:I#))", "#", lr), "@", sh.Name))
 
Upvote 0
Seeing as you are only looking for the one value (for a change) I don't see any reason why you can't use an 'orrible loop.

Code:
 Sub GetSat()
    Dim lastEndDate As Date, lstRw As Long, xLng As Long
    With ThisWorkbook.Sheets("100")
        lstRw = .Range("I" & Rows.Count).End(xlUp).Row
        For xLng = lstRw To 2 Step -1
            If Weekday(.Cells(xLng, "i").Value, vbSaturday) = 1 Then
                lastEndDate = .Cells(xLng, "i").Value
                Debug.Print lastEndDate
                Exit Sub
            End If
        Next
    End With
End Sub
 
Upvote 0
@Mark

I had thought of that as well, however that doesn't take into account the 'Max' date If Saturday.
It would work if the dates are sorted in Ascending order, but we're not sure if they are..
 
Upvote 0
@Mark

I had thought of that as well, however that doesn't take into account the 'Max' date If Saturday.
It would work if the dates are sorted in Ascending order, but we're not sure if they are..

Good point, me being dumb. paying too much attention to the thread title and not the question.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
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