【问题标题】:insert day of the week based on YYYY/MM/DD format根据 YYYY/MM/DD 格式插入星期几
【发布时间】:2019-03-26 16:13:29
【问题描述】:

我正在尝试根据列中的现有日期来表示星期几。例如,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)匹配的方式是否正确。

【问题讨论】:

  • 您可以使用自定义数字格式执行此操作 - dddd 为您提供一周中的哪一天,因此您需要 dddd yyyy/mm/dd
  • 我一直在尝试,但没有运气。例如,如果我不使用 VBA,我可以使用 =TEXT(D2,"dddd") 获得我想要的结果,但是当我尝试在 VBA 中使用它时,它会产生错误 1004。这让我想到了我的方式我使用 VBA 自动填充单元格不正确,这是我的第二个问题。
  • 不,这不是公式,而是属性。只是 Range("d2").currentregion.numberformat = "dddd yyyy/mm/dd"

标签: excel vba


【解决方案1】:

假设您想要每个日期的工作日和日期(格式为 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

【讨论】:

    【解决方案2】:

    如果您使用 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
    

    【讨论】:

      【解决方案3】:

      如果你想无缘无故地花一些额外的时间,我喜欢这样做。

      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
      

      【讨论】:

        猜你喜欢
        • 2022-07-21
        • 1970-01-01
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多