Getting the Day Number Related To A (Text) Day Of The Week (eg "Sat" = 7)

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
VBA function WEEKDAY(date) returns the the number relating to the day of the week. eg Sun = 1, Mon = 2 etc.
Suppose I have a day text value, like Sat. What is a VBA solution to getting it's value 7?

I could do an If Elseif Then routine, but I'm hoping their might be a shorter method.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
One way is to create your own Custom Function in VBA like this:
VBA Code:
Function GetWeekday(d As Variant) As Integer

    Select Case d
        Case "Sun."
            GetWeekday = 1
        Case "Mon."
            GetWeekday = 2
        Case "Tue."
            GetWeekday = 3
        Case "Wed."
            GetWeekday = 4
        Case "Thu."
            GetWeekday = 5
        Case "Fri."
            GetWeekday = 6
        Case "Sat."
            GetWeekday = 7
        Case Else
            GetWeekday = 0
    End Select
    
End Function

You can reference it like any other Excel function in VBA or on the worksheet, i.e.
VBA Code:
Sub Test()
    MsgBox GetWeekday("Sat.")
End Sub
 
Upvote 1
Solution
Thanks Joe. A very clever and practical approach. Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,883
Messages
6,122,077
Members
449,064
Latest member
MattDRT

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