MM-DD-YYYY到星期几,分析连续工作日

时间:2009-12-29 06:02:57

标签: excel datetime vba

我正在基于x个连续工作日,M-F(以及稍后跳过选定的假期)分析数据。我的计划是拿MM-DD-YYYY并确定星期几。

如何在VBA中执行此操作?

我发现这种使用.NET的简单方法,但是如何从Office 2007 VBA调用,以及需要添加哪些参考?

  

DateTime.Parse( “2009-10-02”)。的ToString( “d”,   CultureInfo.CreateSpecificCulture( “EN”)   .NET Day of week from YYYY-MM-DD

为了完整性并帮助他人,以下是我的代码最终结果: 如何在x天期间干净地概括这一点

        myDateStd = myDate
        If Right(Left(myDate, 2), 1) = "/" Then myDateStd = "0" & myDate   'IF M/*/YYYY
        If Len(myDateStd) = 9 Then myDateStd = Left(myDateStd, 3) & "0" & Right(myDateStd, 6) ' If MM/D/YYYY' fix
        dayNumber = Weekday(DateSerial(Right(myDateStd, 4), Left(myDateStd, 2), Mid(myDateStd, 4, 2)), vbMonday)

        Select Case dayNumber
            Case 1 To 3
                workingDaysDelta = 3
            Case 4 To 5      'i.e. its a Friday (or thur) so need to count Fri,S,S,M,T
                workingDaysDelta = 5
            Case 6 To 7     ' its sat/sun
                workingDaysDelta = 4
            Case Else
                MsgBox "ERROR date seams invalid, Date = " & myDateStd
        End Select

'Loop with If testing conditions including:
CDate(wbAn.Cells(j, "E")) < (CDate(myDate) + dayNumber)

1 个答案:

答案 0 :(得分:3)

以下任何一项都应该有效

数字,例如2

Weekday(DateSerial(Right(s, 4), Left(s, 2), Mid(s, 4, 2)), vbMonday)

短字符串,例如“星期二”

Format(DateSerial(Right(s, 4), Left(s, 2), Mid(s, 4, 2)), "ddd")

长字符串,例如“星期二”

Format(DateSerial(Right(s, 4), Left(s, 2), Mid(s, 4, 2)), "dddd")

其中s是包含'MM-DD-YYYY'格式日期的字符串。

无需添加任何引用。