有没有一种模拟系统日期的方法?

时间:2018-11-08 14:30:51

标签: excel vba excel-vba

所以我有一个只希望在工作日运行的宏。我创建了宏(希望),它将检查星期几并将其放入单元格中。这就是我所拥有的:

Private Sub dayCheck()

    If Weekday(Now) = vbMonday Or vbTuesday Or vbWednesday Or vbThursday Or vbFriday Then
        Dim BlankRow As Long
        BlankRow = Range("A65536").End(xlUp).Row + 1
        Cells(BlankRow, 1).Select
        Selection.Value = Date
        Selection.Offset(0, 1).Value = Time
        Selection.Offset(0, 2).Value = WeekdayName(Weekday(Now))
        Selection.Offset(0, 3).Value = Environ("Username")

    ElseIf Weekday(Now) = vbSaturday Or vbSunday Then
        Dim time1, time2

        Do
            time1 = Weekday(Now)
            time2 = vbMonday
            Do Until time1 = time2
                DoEvents
                time1 = Now()
            Loop
        Loop

    End If

    Application.OnTime TimeValue("12:00:00"), "dayCheck"

End Sub

我的问题是我没有管理员权限来更改系统日期。有没有办法可以通过宏模拟这一点?

2 个答案:

答案 0 :(得分:2)

您可以通过在excel中设置now()函数的常规或数字输出格式来模拟日期代码,只需加1即可递增日期。小数点右边的数字表示从午夜到第二天的时间百分比。

今天的日期时间代码是:43412.37786

明天是43413.37786

因此,有关测试代码的问题可以通过创建一个for循环来回答:

Dim Today
Today = Now
For Days = Today To Today + 7 'Tests today through next Thursday.
    If Weekday(Days) = ...
Next Days

但是达伦的答案似乎可以解决您的问题,所以我可能会同意。

答案 1 :(得分:1)

  • docker run <my_specific_parameteres> remote.docker.container.php7.2 bin/phpunit --coverage-clover -c phpunit.xml.dist 给出日期/时间,Now仅给出日期。
  • Date返回星期几,星期天为1。

  • 今天Weekday(Date)的星期四将返回False。

  • Weekday(Now)=vbMonday返回7-我不确定为什么,但是确实如此。这里最主要的是它不会返回Weekday(Now) = vbMonday Or vbTuesday Or vbWednesday Or vbThursday Or vbFridayTRUE
    • 要使该语句起作用,您必须使用
      FALSE
      一种更简单的方法是Weekday(Now) = vbMonday Or Weekday(Now) = vbTuesday Or Weekday(Now) = vbWednesday Or Weekday(Now) = vbThursday Or Weekday(Now) = vbFriday-Weekday(Date,vbMonday)<=5为一周的 Monday = 1,Sunday = 7 编号,并回答问题“工作日是星期几吗?”。

代码如下:

vbMonday

请注意,我在 Sub Test() 'DayCheck Now 'Or 'DayCheck #11/7/2018 6:55:00 PM# 'Or look at next 7 days starting now. Dim x As Long Dim StartDate As Date StartDate = Now For x = 0 To 6 DayCheck StartDate + x Next x End Sub Sub DayCheck(MyDate As Date) Dim rLastCell As Range Dim wrkSht As Worksheet Set wrkSht = ThisWorkbook.Worksheets("Sheet1") If Weekday(MyDate, vbMonday) <= 5 Then Set rLastCell = wrkSht.Cells(Rows.Count, 1).End(xlUp).Offset(1) 'Reference to the blank cell itself. rLastCell.Resize(, 4) = Array(MyDate, MyDate, MyDate, Environ("Username")) rLastCell.NumberFormat = "dd-mmm-yy" rLastCell.Offset(, 1).NumberFormat = "hh:mm AM/PM" rLastCell.Offset(, 2).NumberFormat = "dddd" Else 'No need to check if it's a weekend - we know it's not a weekday. 'Just keep running until time1 = 2 and time2 = 2? 'I guess that'll be Midnight on Monday? End If End Sub 列中输入了相同的值-只是日期和时间。
然后,我格式化每个单元格以显示您感兴趣的日期和时间部分。