我正在编写一个处理大量数据的Excel宏。其中一行包含日期范围,例如wkstartdate-wkenddate,我想使用dateadd函数每周打印下一个日期范围(如以下情况,例如“ 27-01-14-02-02-14”),但无法执行所以。'06 -01-14-12-01-14''13 -01-14-19-01-14''20 -01-14-26-01-14'我在以下摘录中使用了失败的内容:
Range("E" & Lastrow).Select
prwk = Split(ActiveCell.Value, "-")
'curr_wkstart = DateAdd("d", 7, prwk(1)) 'error as maybe prwk(1) isnt correct format
'curr_wkend = DateAdd("d", 7, prwk(2)) 'error
Range("E" & Lastrow + 1).Value = curr_wkstart & curr_wkend 'no result
为了进行测试,我在diff单元格中打印prwk(1)(在上述情况下为20/01/14)并添加7天,这给了我1/21/2020而不是'27 / 01/14 '。我也尝试使用Cdate函数,但仍然出错
你能给些建议么??
我认为您要在此处使用Format
和DateSerial
功能。这是我的想法:
Function GetNextWeek(TheStartWeek)
a = Split(TheStartWeek, " - ")
b = Split(a(1), "-")
c = DateSerial(b(2), b(1), b(0)) + 1
d = c + 6
GetNextWeek = Format(c, "dd-mm-yy") & " - " & Format(d, "dd-mm-yy")
End Function
Sub Test()
Debug.Print GetNextWeek("13-01-14 - 19-01-14") 'Givs you "20-01-14 - 26-01-14"
End Sub
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句