Excel VBA Weekday Function - WallStreetMojo
文章推薦指數: 80 %
The weekday function returns the provided date's day number in the week. For example, if you have dates 01st April to 07th April and if you want to know the day ... SkiptoprimarynavigationSkiptomaincontentSkiptoprimarysidebarSkiptofooter Home»Excel,VBA&PowerBI»VBAResources»VBAWeekdayArticlebyJeevanAYReviewedbyDheerajVaidya,CFA,FRM ExcelVBAWeekdayFunction WeekdayinVBAisadateandtimefunctionwhichisusedtoidentifytheweekdayofagivendateprovideditasaninput,thisfunctionreturnsanintegervaluefrom1to7range,thereisanoptionalargumentprovidedtothisfunctionwhichisthefirstdayoftheweekbutifwedonotprovidethefirstdayoftheweekthenthefunctionassumesSundayasthefirstdayoftheweekbydefault. Canwetelltheweekdaynumberbylookingataparticulardate?Yes,wecantellthedaynumberthatweek,dependinguponthestartingdayoftheweek.Inregularworksheetfunctions,wehaveafunctioncalledWEEKDAY inexceltotellthenumberoftheweekforaparticulardate.InVBA,too,wehavethesamefunctiontofindthesamething. Youarefreetousethisimageonyourwebsite,templatesetc,PleaseprovideuswithanattributionlinkHowtoProvideAttribution?ArticleLinktobeHyperlinkedForeg:Source:VBAWeekday(wallstreetmojo.com) WhatdoesWeekdayFunctiondo? Theweekdayfunctionreturnstheprovideddate’sdaynumberintheweek.Forexample,ifyouhavedates01stAprilto07thAprilandifyouwanttoknowthedayofthedate05thAprilifthestartingdayoftheweekisfromMonday,itisthe5thday. Tofindthis,wehavethesamefunctionas“Weekday”inaworksheetaswellasinVBA.Belowisthesyntaxofthefunction. Date:Forwhichdatewearetryingtofindtheweekday.Thisshouldbeaproperdatewiththecorrectformat. [FirstDayofWeek]:InordertodeterminetheweekdayoftheprovidedDate,weneedtomentionwhatisthefirstdayoftheweek.Bydefault,VBAconsiders“Monday”asthestartingdayoftheweek.Apartfromthis,wecansupplythebelowdaysaswell. Examples YoucandownloadthisVBAWeekDayFunctionExcelTemplatehere– VBAWeekDayFunctionExcelTemplate Example#1 Tostarttheproceedings,letmestartwithasimpleexamplefirstup.Nowwewilltrytofindtheweekdayforthedate“10-April-2019”. Step1:DefinethevariableasString Code: SubWeekday_Example1() DimkAsString EndSub Step2:Assignvaluetothevariable Assignthevaluetothevariable“k”byapplyingtheWEEKDAYfunction. Code: SubWeekday_Example1() DimkAsString k=Weekday( EndSub Step3:EnterDateinFunction Thedatewearetestinghereis“10-Apr-2019”,sopassthedateas“10-Apr-2019”. Code: SubWeekday_Example1() DimkAsString k=Weekday("10-Apr-2019" EndSub Step4:ShowValueofVariableinMsgBox Bydefault,ittakesthefirstdayoftheweekas“Monday,”soignorethispart.Closethebracket.Thenextlineshowsthevalueofvariable“k”intheVBAmessageboxVBAMessageBoxVBAMsgBoxfunctionisanoutputfunctionwhichdisplaysthegeneralizedmessageprovidedbythedeveloper.Thisstatementhasnoargumentsandthepersonalizedmessagesinthisfunctionarewrittenunderthedoublequoteswhileforthevaluesthevariablereferenceisprovided.readmore. Code: SubWeekday_Example1() DimkAsString k=Weekday("10-Apr-2019") MsgBoxk EndSub Ok,wearedone. Ifwerunthecode,wewillgettheresultas“4”becausestartingfromSunday,theprovideddate(10-Apr-2019)fallsonthe4thdayoftheweek. Note:Mysystem’sstartingdayoftheweekis“Sunday.” Similarly,ifyouchangethestartdayoftheweek,itkeepsvarying.Belowisanexamplelineforthesame. Code: k=Weekday("10-Apr-2019",vbMonday) ‘Thisreturns3 k=Weekday("10-Apr-2019",vbTuesday) ‘Thisreturns2 k=Weekday("10-Apr-2019",vbWednesday) ‘Thisreturns1 k=Weekday("10-Apr-2019",vbThursday) ‘Thisreturns7 k=Weekday("10-Apr-2019",vbFriday) ‘Thisreturns6 k=Weekday("10-Apr-2019",vbSaturday) ‘Thisreturns5 k=Weekday("10-Apr-2019",vbSunday) ‘Thisreturns4 Example#2–ArriveWhethertheDateisonWeekendorNot Assumeyouhaveadatelikethebelow,andyouwanttofindthenextweekenddate,thenwecanusetheWEEKDAYfunctiontoarriveattheresults. WeneedtouseWEEKDAYwithIFconditionandloopstoarriveattheresult.Ihavewrittenthecodeforyoutogolinebylinetogetthelogic. Code: SubWeekend_Dates() DimkAsInteger Fork=2To9 IfWeekday(Cells(k,1).Value,vbMonday)=1Then Cells(k,2).Value=Cells(k,1)+5 ElseIfWeekday(Cells(k,1).Value,vbMonday)=2Then Cells(k,2).Value=Cells(k,1)+4 ElseIfWeekday(Cells(k,1).Value,vbMonday)=3Then Cells(k,2).Value=Cells(k,1)+3 ElseIfWeekday(Cells(k,1).Value,vbMonday)=4Then Cells(k,2).Value=Cells(k,1)+2 ElseIfWeekday(Cells(k,1).Value,vbMonday)=5Then Cells(k,2).Value=Cells(k,1)+1 Else Cells(k,2).Value="ThisisactuallytheweekendDate" EndIf Nextk EndSub Thiswillarriveattheresultsbelow. LookatthecellsB6&B7.Wegottheresultas“Thisisactuallytheweekenddate”becausedates“04-May-2019”and“06-Apr-2019”areactuallyweekenddates,sononeedtoshowtheweekenddateforweekenddates.Bydefault,wegettheresultasthis. RecommendedArticles ThishasbeenaguidetoVBAWeekday.HerewelearnhowtousetheVBAweekdayfunctiontogetthelastdayoftheweekwithexamplesandadownloadableexceltemplate. BelowaresomeusefulexcelarticlesrelatedtoVBA– IFERRORFunctioninVBAVBADateDiffFunctionExcel WeeklyPlannerTemplateWhatisVBADATEVALUEFunction?VBAPasteValues VBATraining(3Courses,12+Projects) 3Courses 12Hands-onProjects 43+Hours FullLifetimeAccess CertificateofCompletion LEARNMORE>> PrimarySidebar GetFREEAccessto500+VideoTutorials SubscribetoYoutubeNow! FooterCompany Resources Courses x Let’sGetStarted EmailID* Pleaseselectthebatch Upcomingbatch* 19th/20thSeptember2020Saturday-Sunday9amISTto5pmIST 26th/27thSeptember2020Saturday-Sunday9amISTto5pmIST x DownloadVBAWeekDayFunctionExcelTemplate EnterEmailAddress x Login ForgotPassword? x FreeExcelCourse Excelfunctions,Formula,Charts,Formattingcreatingexceldashboard&others EnterEmailAddress *Pleaseprovideyourcorrectemailid.LogindetailsforthisFreecoursewillbeemailedtoyou x FreeExcelCourse Excelfunctions,Formula,Charts,Formattingcreatingexceldashboard&others EnterEmailAddress *Pleaseprovideyourcorrectemailid.LogindetailsforthisFreecoursewillbeemailedtoyou x x Cookieshelpusprovide,protectandimproveourproductsandservices.Byusingourwebsite,youagreetoouruseofcookies(PrivacyPolicy) GetFreeAccessto500+VideosinExcel,VBA&More SubscribetoWallstreetmojoYoutubeChannel 55.6Ksubscribers x
延伸文章資訊
- 1Weekday 函數(Visual Basic for Applications) | Microsoft Docs
Office VBA reference topic. ... Weekday 函數語法具有下列具名引數: ... 這個範例會使用Weekday 函數從指定的日期取得一周的第幾天。
- 2VBA - WeekDay - Tutorialspoint
VBA - WeekDay, The WeekDay function returns an integer from 1 to 7 that represents the day of the...
- 3VBA WeekDay函數- VBA教學 - 極客書
WeekDay WeekDay函數返回從1到7的整數,代表該日期指定星期的一天。 語法: Weekday ( date [, firstdayofweek ]) 參數說明Date, 必需的參數。...
- 4VBA WEEKDAY Function (Syntax + Example) - Excel Champs
The VBA WEEKDAY function is listed under the date category of VBA functions. When you use it in a...
- 5WorksheetFunction Weekday 方法(Excel) | Microsoft Docs
Visual Basic for Applications (VBA) 計算不同于Excel 的序列日期。 在VBA 中,序號1是12月31日1899,而不是1900。 支援和意見反應. 有關於...