我试图根据一列中的现有日期来表示星期几。例如,示例2019/03/26将显示在一列中,而在相邻的列中将显示/实例化“星期二”一词/日期
Sub InputDate()
Dim StartDate As Date
Dim DayOfWeek As Date
Dim i As Integer
mbox = InputBox("Enter Start Date", "Enter Start Date")
If IsDate(mbox) Then
StartDate = CDate(mbox)
Range("d2") = StartDate
For i = 1 To 14
Cells(i + 1, 4).Value = StartDate + i
Next i
Range("d2").CurrentRegion.Offset(, -1).Value = "Eg Tuesday"
Else
MsgBox "Please Enter a Date"
End If
End Sub
所以最终结果看起来像
2019年3月26日星期二
2019年3月27日星期三
我想我可能面临两个问题。
首先,我无法从以03/26/2019样式表示的日期中提取星期几
第二件事是我不确定将C列(我希望显示星期几的位置)与D列(日期格式为03/26/2019)匹配的方式是否正确。
答案 0 :(得分:1)
假设每个日期都需要工作日和日期(格式为YYYY/MM/DD
),其中包括:
所以对于今天(2019-03-26),您想要以下结果:
+-----------+------------+
| Weekday | Date |
+-----------+------------+
| Tuesday | 2019/03/26 |
| Wednesday | 2019/03/27 |
| Thursday | 2019/03/28 |
| Friday | 2019/03/29 |
| Saturday | 2019/03/30 |
| Sunday | 2019/03/31 |
| Monday | 2019/04/01 |
| Tuesday | 2019/04/02 |
| Wednesday | 2019/04/03 |
| Thursday | 2019/04/04 |
| Friday | 2019/04/05 |
| Saturday | 2019/04/06 |
| Sunday | 2019/04/07 |
| Monday | 2019/04/08 |
| Tuesday | 2019/04/09 |
+-----------+------------+
我认为您最好的选择是创建一个既可以保存格式化日期和工作日的数组,然后又可以一次将该数组放在所需的范围内,
Option Explicit
' declare a constant for the number of dates, to avoid hard-coding it later on.
Private Const NUMBER_OF_DATES As Integer = 15
Public Sub InputDate()
' this is your array that will hold the values you need.
' note that we'll initialize is later with the ReDim command.
Dim datesWithWeekdays() As Variant
' this will hold the user i
Dim startDate As Date
' this is your initial user input (a String value).
' you may want to change the variable name to something
' that more closely matches your StartDate (e.g. startDateAsString)
Dim mbox As String
' This is just your counter variable; same as you had before.
Dim i As Integer
' this is a placeholder, to store the date each iteration.
Dim dt As Date
' this is used at the end to put the contents of your array into Excel.
Dim dest As Excel.Range
' here we actually initialize the array.
' the number of rows = the number of dates,
' and the number of columns is 2.
ReDim datesWithWeekdays(1 To NUMBER_OF_DATES, 1 To 2)
mbox = InputBox("Enter Start Date")
If IsDate(mbox) Then
startDate = CDate(mbox)
' note that we don't do any processing outside the loop.
' for the total # of dates (15), calculate the date,
' and then assign the appropriate values to the array.
' note that I'm using the DateAdd function, which is clearer
' than just adding a number to a date.
' Also, I'm adding a "'" character in front of the YYYY/MM/DD,
' so Excel doesn't try to parse it as a date.
' If you want to keep it as a date, and have the format match,
' you can instead change the column formatting in Excel.
For i = 1 To NUMBER_OF_DATES
dt = DateAdd("d", i - 1, startDate)
datesWithWeekdays(i, 1) = Format(dt, "dddd")
datesWithWeekdays(i, 2) = "'" & Format(dt, "YYYY/MM/DD")
Next i
' here, we set up the destination range, and set its value
' to the array.
Set dest = Excel.Range("$D$2")
Set dest = dest.Resize(rowsize:=NUMBER_OF_DATES, columnsize:=2)
dest.Value = datesWithWeekdays
Else
MsgBox "Invalid date"
End If
End Sub
答案 1 :(得分:0)
如果您使用的是VBA,则需要功能Format
:
Dim StartDate As Date
Dim DayOfWeek As Date
Dim i As Integer
mbox = InputBox("Enter Start Date", "Enter Start Date")
If IsDate(mbox) Then
StartDate = CDate(mbox)
For i = 0 To 13
Cells(i + 2, 3) = Format(StartDate + i, "dddd")
Cells(i + 2, 4).Value = StartDate + i
Next i
Else
MsgBox "Please Enter a Date"
End If
如果您希望例程显示14天(包括输入的日期),请使用以下循环:
For i = 0 To 13
Cells(i + 2, 3) = Format(StartDate + i, "dddd")
Cells(i + 2, 4).Value = StartDate + i
Next i
答案 2 :(得分:0)
如果您无缘无故地想花一些时间,我喜欢这样。
if range("h6").value = monday then
range("i6").value = 15
range("j6").value = 03
range("k6").value = 2019
else
if range("h7").value = tuesday then
range("i7").value = range("i6").value + 1
else
(so on through the rest of the days)
end if