1

我有一个工作簿,例如,我们将称之为 Monthly Dashboard,其中的信息完全由一个名为 Trial Balance 的单独工作簿创建,除了一个单元格。这些数据将从名为 Project Control 的每周工作簿中收集。项目控制文件以 YYYYWProjectControl.xlsx 格式保存,其中 YYYY 指的是当年,W 指的是周。

需要填写的单元格是每周表格上迄今为止所有项目的总销售额。我需要做的是以编程方式打开最后创建的项目控件并计算出这个销售数字并关闭/保存。

关闭和保存都作为以前项目的一部分完成,但我需要知道如何检查当前周数是什么以及检查最后创建的文件是哪个。有谁知道如何做到这一点?如果是这样,还请记住,随着年底临近 1 月,您需要搜索最后创建的 2013 年项目控制,如果没有,则需要搜索 2012 年。

提前致谢

4

1 回答 1

3

根据您是指实际周还是财政(财务)周,您可以使用这两个函数中的任何一个来为您计算:

财政周:

Function FiscalWeek(dtDate As Variant)

Dim dtFiscal    As Date
Dim dtFstMon    As Date
Dim i           As Integer

    'Ensure we are working with a valid date
    If Not IsDate(dtDate) Then
        FiscalWeek = "Invalid Date"
        Exit Function
    End If

    'Strip out any timestamps
    dtFiscal = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))

    'Determine first Monday of April for given year
    i = 1
    Do Until Weekday(DateSerial(Year(dtFiscal), 4, i), 2) = 1
        i = i + 1
    Loop

    dtFstMon = DateSerial(Year(dtFiscal), 4, i)

    'Work out the difference in weeks from April for the relevant year
    Select Case dtFiscal
        Case Is < dtFstMon
            FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal) - 1, 4, 1), dtFiscal, 2)
        Case Else
            FiscalWeek = DateDiff("WW", DateSerial(Year(dtFiscal), 4, 1), dtFiscal, 2) + IIf(i = 1, 1, 0)
    End Select

End Function

或实际周:

Function ActualWeek(dtDate As Variant)

Dim dtActual    As Date
Dim dtFstMon    As Date
Dim i           As Integer

    'Ensure we are working with a valid date
    If Not IsDate(dtDate) Then
        ActualWeek = "Invalid Date"
        Exit Function
    End If

    'Strip out any timestamps
    dtActual = DateSerial(Year(dtDate), Month(dtDate), Day(dtDate))

    'Determine first Monday of January for given year
    i = 1
    Do Until Weekday(DateSerial(Year(dtActual), 1, i), 2) = 1
        i = i + 1
    Loop

    dtFstMon = DateSerial(Year(dtActual), 1, i)

    'Work out the difference in weeks from January for the relevant year
    Select Case dtActual
        Case Is < dtFstMon
            ActualWeek = DateDiff("WW", DateSerial(Year(dtActual) - 1, 1, 1), dtActual, 2)
        Case Else
            ActualWeek = DateDiff("WW", DateSerial(Year(dtActual), 1, 1), dtActual, 2) + IIf(i = 1, 1, 0)
    End Select

End Function
于 2012-11-01T10:48:15.187 回答