我有一个工作簿,例如我们将调用Monthly Dashboard,其中的信息完全是从名为Trial Balance的单独工作簿创建的,禁止一个单元格。这些数据将从名为Project Control的每周工作簿中收集。项目控制文件以YYYYWProjectControl.xlsx格式保存,其中YYYY表示当前年份,W表示周。
需要填写的单元格是每周表单上迄今为止所有项目的总销售额。我需要做的是以编程方式打开最后创建的项目控件,并将此销售数据输出并关闭/保存。
关闭和保存都是作为前一个项目的一部分完成的,但我需要知道如何检查当前周数是什么,并检查最后创建的文件是哪个。有没有人知道如何做到这一点?如果是这样,请记住,在接近1月份的年底将需要搜索最后创建的2013年项目控制,如果没有,那么它将需要搜索2012年。
提前致谢
答案 0 :(得分: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