简体   繁体   English

从日历日期读取正确的星期数

[英]Read the correct week number from a calendar date

I have below dates in an Excel column, as you can see. 如您所见,我在Excel栏中有以下日期。

Sprint 1 takes from 10.04 to 21.04 this means 2 weeks and between brackets they are specified week 15 and 16 which is correct but for Sprint 2, who also starts in 10.04 but takes until 05.05 it means 7 weeks, but are displayed also the weeks from the Sprint1. Sprint 1的时间是从10.0421.04这意味着2周,在方括号之间指定了第15周和16周,这是正确的,但对于Sprint 2,它也从10.04开始,但直到05.05都意味着7周,但也显示了从Sprint 1。

"Sprint1 (CW15-16/2017)
[10.04.2017 - 21.04.2017]
Sprint2 (CW15-16/2017)
[10.04.2017 - 05.05.2017]"

What I have until now is: 到目前为止,我所拥有的是:

'reading the first CW of the sprint based on the date
SprintFristCW = Left(planning_wb.Worksheets(SprintPlanningTable).Cells(2, i + 1).Value, 9)

'reading the last CW of the Sprint based on the date
SprintEndCW = Right(planning_wb.Worksheets(SprintPlanningTable).Cells(2, i + Sprintlength).Value, 9)

SprintCW = Left(SprintFirstCW, 4) & "-" & Right(SprintEndCW, 7)

But SprintEndCW is not reading correct the week number. 但是SprintEndCW无法读取正确的星期数。

So I need to read the correct week number in which each sprint ends and print it. 因此,我需要读取每个冲刺结束的正确星期数并进行打印。

Don't create huge procedures. 不要创建庞大的程序。 Small is beautiful. 小就是美丽。 Create functions that feed into your Main procedure. 创建提供给您的Main过程的函数。 Here is an example. 这是一个例子。 The procedure TestExtraction calls the function ExtractWeeks . 过程TestExtraction调用函数ExtractWeeks Therefore ExtractWeeks needs not be part of the procedure that calls it, making the code easier to understand and maintain. 因此, ExtractWeeks不必是调用它的过程的一部分,这使代码更易于理解和维护。

Private Sub TestExtraction()

    Dim Fun As Long
    Dim DateString As String
    Dim StartDate As Date, EndDate As Date

    DateString = ActiveCell.Value

    ' the DateString is re-defined here for testing purposes
    DateString = "[10.04.2017 - 05.05.2017]"


    Fun = ExtractWeeks(DateString, StartDate, EndDate)
    If Fun < 0 Then
        Debug.Print "Invalid date"
    Else
        With Application
            DateString = "(CW" & .WeekNum(StartDate)
            If Year(StartDate) <> Year(EndDate) Then _
                DateString = DateString & "/" & Year(StartDate)
            DateString = DateString & " - " & .WeekNum(EndDate) & "/" & Year(EndDate) & ")"
        End With
        Debug.Print DateString
        Debug.Print Fun & " weeks"
    End If
End Sub

Private Function ExtractWeeks(ByVal DateString As String, _
                              StartDate As Date, _
                              EndDate As Date) As Long
    ' 24 Oct 2017
    ' return the number of weeks between dates (rounded up)
    ' return -1 if one of the dates is unreadable

    Dim Dates() As String
    Dim i As Integer

    Dates = Split(Mid(DateString, 2, Len(DateString) - 2), "-")
    On Error Resume Next
    For i = 0 To 1
        Dates(i) = Replace(Trim(Dates(i)), ".", Application.International(xlDateSeparator))
    Next i
    StartDate = DateValue(Dates(0))
    EndDate = DateValue(Dates(1))
    If Err Then
        ExtractWeeks = -1
    Else
        ExtractWeeks = Int((StartDate - EndDate) / 7) * -1
    End If
End Function

The point is that not everything that looks like a date is a date Excel can understand. 关键是,并非所有看起来像日期的东西都是Excel可以理解的日期。 The Function ExtractWeeks converts the "dates' from your worksheet into real dates and returns these dates to the calling procedure. It also returns -1 in case of error which you can use to trap such errors. In my example, the function returns the number of weeks (or -1). You might let it return the CW string my calling procedure constructs. You will find it easy to move the process of constructing that string to the function and let the function return "" in case of error instead of -1. Perhaps you can exclude the possibility of errors in the dates. This is a question of how you integrate the function into your Main . 函数ExtractWeeks将工作表中的“日期”转换为实际日期,并将这些日期返回给调用过程。如果发生错误,它也会返回-1,可以用来捕获此类错误。在我的示例中,该函数返回数字个星期(或-1)。您可以让它返回我的调用过程构造的CW字符串。您会发现很容易将构造该字符串的过程移至该函数,并在出现错误的情况下让该函数返回“”,而不是返回-1。也许您可以排除日期中出现错误的可能性,这是如何将函数集成到Main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM