在Excel中,我有工作日后命名的工作表。所以,我没有周末或特殊日子(节假日等)。我创建了一个函数:previous(x),x是工作天数。我希望我的函数给我n-x天的日期(mm-dd格式),n是当前的工作日 修改:对不起day1和month1,因为VBA使用了日期和月份
Function Previous(x As Integer) As String
currentSheet= ActiveSheet.Name
day1 = Right(currentSheet, 2) 'sheets' name convention is mm-dd
month1 = Left(currentSheet, 2)
nbLoop = 0
dayFound= 0
While dayFound < x
day1= Format(day1 + 1, "dd") 'Call to 2 other functions that correctly decrement'
day1= DecrementJ(day1) 'day and month'
month1 = DecrementM(day1, month1)
fPrec = month1 & "-" & day1
On Error Resume Next
Set wsSheet= Sheets(fPrec)
If wsSheet Is Nothing Then
Else 'dont ask me why If Not wsS Is Nothing doesnt work'
dayFound= dayFound+ 1
End If
nbLoop = nbLoop +1
if nbLoop > 30 then goto Err: 'avoid infinite loop'
Wend
Previous= fPrec
Err:
End Function
此功能正常工作一次(它在周末前一天找到)但未能跳过两个洞(如周末+假期)
感谢您的帮助
答案 0 :(得分:1)
为什么这么努力?怎么样:
calcdate = CDate(Year(Date) & "-" & ActiveSheet.Name) - x
calcname = Format(calcdate, "mm-dd")
For Each ws In Worksheets
If ws.Name = calcname Then
Previous = calcname
End If
Next