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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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,213,510
Messages
6,114,037
Members
448,543
Latest member
MartinLarkin

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