Syntax Error "nth day of month"

richard blaine

Board Regular
Joined
May 19, 2005
Messages
67
Can someone please correct this, thanks:

Function NthDayOfMonth(Which_Day As String, Which_Date As String, Occurence As Byte) As Date

Dim i As Integer

Dim iDay As Integer

Dim iDaysInMonth As Integer

Dim FullDateNew As Date

Dim lCount As Long



Which_Date = CDate(Which_Date)



Select Case UCase(Which_Day)

Case "SUN"

iDay = 1

Case "MON"

iDay = 2

Case "TUE"

iDay = 3

Case "WED"

iDay = 4

Case "THU"

iDay = 5

Case "FRI"

iDay = 6

Case "SAT"

iDay = 7

End Select





FullDateNew = DateSerial(Year(Which_Date), Month(Which_Date), 1)



iDaysInMonth = Day(DateAdd("d", -1, DateSerial _

(Year(Which_Date), Month(Which_Date) + 1, 1)))



For i = 0 To iDaysInMonth

If Weekday(FullDateNew + i) = iDay Then

lCount = lCount + 1

End If

If lCount = Occurence Then

NthDayOfMonth = FullDateNew + i

Exit For

End If

Next i



End Function
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
What's it supposed to do and what problem are you having exactly?
 
Upvote 0
It returns your desired weekday of a month (e.g. 2nd tuesday of november 2009). The cell formula:

=nthDayOfMonth("tue","11/11/2005",2)

This worked fine on my old worksheet until my computer crashed and I lost the entire file. Then when I pasted the code into the new sheet (VBA), I got a syntax error. Even after fooling with it, I get a #NAME? error.
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,442
Members
448,898
Latest member
drewmorgan128

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