[英]Entering date of the day of the week in excel vba
I have created a time card worksheet for my employees to submit.我创建了一个考勤卡工作表供我的员工提交。 Column A is the day name and I want column B to auto-populate the date of that day. A 列是日期名称,我希望 B 列自动填充当天的日期。 Here is the module I have created...这是我创建的模块...
Sub AutoDate()
Dim NextSun As Date
Dim NextMon As Date
Dim NextTues As Date
Dim NextWed As Date
Dim NextThur As Date
Dim NextFri As Date
Dim NextSat As Date
'*****Autopopulate the dates of the day of the week*****
NextSun = ((Weekday(Date, vbSunday)))
Range("C26") = NextSun
NextSun = ((Weekday(Date, vbMonday)))
Range("C27") = NextMon
NextSun = ((Weekday(Date, vbTuesday)))
Range("C28") = NextTues
NextSun = ((Weekday(Date, vbWednesday)))
Range("C29") = NextWed
NextSun = ((Weekday(Date, vbThursday)))
Range("C30") = NextThur
NextSun = ((Weekday(Date, vbFriday)))
Range("C31") = NextFri
NextSat = ((Weekday(Date, vbSaturday)))
Range("C32") = NextSat
End Sub
Thank you in advance for your assistance in this matter.在此先感谢您对此事的协助。
So I assume you Always start with Sunday in B26
and Monday in B27
and so on, and want to just print the dates for the next week – hence the variable name – in column C
?所以我假设你总是在B26
中从星期日开始,在B27
中从星期一开始,依此类推,并且只想在C
列中打印下一周的日期——因此是变量名称?
Weekday() will only return a number, not a date, so that alone will not likely do what you want. Weekday() 只会返回一个数字,而不是日期,因此仅凭这一点不太可能做你想做的。 However, you can add it onto the existing date.但是,您可以将其添加到现有日期。
Sub AutoDate()
Dim NextDay As Range
Dim i As Long
Set NextDay = Range("C26")
For i = 0 To 6
NextDay.Offset(i) = Date + (8 - Weekday(Date) + i)
Next i
End Sub
Change that 8 to a 1 if you want the current week.如果您想要当前一周,请将8更改为1 。
This will fill seven rows of column B and C with dates of the current week:这将用本周的日期填充 B 列和 C 的七行:
Sub AutoDate()
Dim NameRange As Range
Dim DateRange As Range
Dim WeekdayDate As Date
Dim WeekdayName As String
Dim StartDate As Date
Dim Offset As Long
Set NameRange = Range("C26")
Set DateRange = Range("B26")
StartDate = DateAdd("d", 1 - Weekday(Date, vbSunday), Date)
For Offset = 0 To 6
WeekdayDate = DateAdd("d", Offset, StartDate)
WeekdayName = Format(WeekdayDate, "dddd")
DateRange.Offset(Offset) = WeekdayDate
NameRange.Offset(Offset) = WeekdayName
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.