Frage

I am writing a macro that processes an excel with lots of data. One of the rows contains a date range like wkstartdate - wkenddate and I would like to use dateadd function to print next date range every week (like '27-01-14 - 02-02-14' in below case) but unable to do so. '06-01-14 - 12-01-14' '13-01-14 - 19-01-14' '20-01-14 - 26-01-14' I used below excerpt which fails:

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

For testing purpose I print, prwk(1) which is 20/01/14 in the above case, in a diff cell and add 7 days, which gives me 1/21/2020 instead of '27/01/14'. I also tried using Cdate function, but still error

Can you please advise??

War es hilfreich?

Lösung

I think what you want to use here are the Format and DateSerial functions. Here's how I came at it:

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

Hope this helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top