Excel VBA - Function optional parameter DATE type Fails -
i have function finds following monday date. when no optional date parameter passed goes default of zero. think i've included comments explain problem
function nextmondayfromadateortoday(optional startdate date) date ' objective: if date param not supplied should set startdate today ' ??? problem: no arg provided startdate 0 = date value of 1/1/1900 ' ??? how add default optional paramater of date type? ' ??? or, how check if arg provided if parameter date type? if not (isdate(startdate)) startdate = date select case weekday(startdate) case 1: nextmondayfromadateortoday = startdate + 1 case 2: nextmondayfromadateortoday = startdate + 0 case 3: nextmondayfromadateortoday = startdate + 6 case 4: nextmondayfromadateortoday = startdate + 5 case 5: nextmondayfromadateortoday = startdate + 4 case 6: nextmondayfromadateortoday = startdate + 3 case 7: nextmondayfromadateortoday = startdate + 2 end select end function
vba variables have default value, , optional parameters automatically set default, behavior expected. since know optional parameter default 0 (1/1/1900) if not passed, why not test value instead of testing see if date passed in?
if, on other hand, think might need pass in 1/1/1900, should set optional parameter variant
type. variants not initialized when passed in optionally, not have default value (unlike dates).
Comments
Post a Comment