excel vba - How to get the Day Name of Date?

自古美人都是妖i 提交于 2019-12-07 14:28:14

问题


I have a small code that gets the Day Name of the Dates listed in Excel. But the problem is, it's not getting the right Day Name(e.g. Tuesday).

For Example:

sDayName = Format(Day(11/1/2016), "dddd")

Then the output produces an incorrect Day Name which is:

sDayName = "Sunday"

when it's supposed to be "Tuesday". Thanks for the help guys.


回答1:


For your specific request, assuming your date string is formatted as your regional setings:

    sDayName = Format("11/01/2016", "dddd")

If you want the weekday for today:

    sDayName = Format(Date, "dddd")

If you want the weekday for any date in any regional settings

    sDayName = Format(DateSerial(2016, 11, 4), "dddd")
    'change 2016, 11, 4 for your year, month, day values

:)




回答2:


WEEKDAY Function can be used on this topic and in Vba Codes . For example:

Label2.Caption = WeekdayName(Weekday(TextBox2, 0), False, 0)

I used this function in when I created userform about date entry to active cell.



来源:https://stackoverflow.com/questions/40408812/excel-vba-how-to-get-the-day-name-of-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!