Excel 根据YYYY/MM/DD格式插入星期几
我试图根据列中的现有日期来表示一周中的某一天。例如,2019/03/26将显示在一列中,在相邻列中,单词/日期星期二将出现/实例化Excel 根据YYYY/MM/DD格式插入星期几,excel,vba,Excel,Vba,我试图根据列中的现有日期来表示一周中的某一天。例如,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) Ran
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日星期三
我想我可能面临两个问题
首先,我无法从2019年3月26日样式中表示的日期提取一周中的某一天
第二件事是,我不确定我将C列(我希望显示一周中的某一天)与D列(日期样式为2019年3月26日)匹配的方式是否正确 如果使用VBA,则需要函数格式: 如果希望例程显示包括输入日期在内的14天,请改用此循环:
For i = 0 To 13
Cells(i + 2, 3) = Format(StartDate + i, "dddd")
Cells(i + 2, 4).Value = StartDate + i
Next i
假设您希望每个日期的工作日和日期格式为YYYY/MM/DD,其中包括: 开始日期 接下来的14天 因此,对于今天的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
如果你想无缘无故地多花点时间,我喜欢这样做
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
您可以使用自定义数字格式来实现这一点—dddd提供了一周中的某一天,因此您需要dddd yyyy/mm/ddI。我一直在尝试,但运气不佳。如果我没有使用VBA,我可以使用=TEXTD2,dddd得到我想要的结果,但是当我尝试在VBA中使用它时,它会产生一个错误1004。这让我觉得我使用VBA自动填充单元格的方式是不正确的,这是我的第二个问题。不,这不是公式,而是属性。仅范围为2.currentregion.numberformat=dddd yyy/mm/dd
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