excel - Difference between "MM" standing for Months and "mm" standing for minutes when using Format function -
excel - Difference between "MM" standing for Months and "mm" standing for minutes when using Format function -
my issue trying set rolling count since our lastly accident @ work. code below works in fashion. rolling count works fine, there problem removing "s" words (hours, minutes, seconds ect) month , minute.
here's code:
sub lti_sub() static etime 'exit sub ' uncomment stop etime = now() + timeserial(0, 0, 1) sheets("lti").range("time") = lti(sheets("lti").range("last")) application.ontime etime, "lti_sub", , true end sub function lti(lastlti date) x = now() - lastlti yx = format(x, "yy") if yx = 1 ys = "" else ys = "s" end if mx = format(x, "mm") if mx = 1 ms = "" else ms = "s" end if dx = format(x, "dd") if dx = 1 ds = "" else ds = "s" end if hx = format(x, "hh") if hx = 1 hs = "" else hs = "s" end if mmx = format(x, "mm") if mmx = 1 mms = "" else mms = "s" end if sx = format(x, "ss") if sx = 1 ss = "" else ss = "s" end if lti = format(x, "yy \y\e\a\r\" & ys & ", mm \m\o\n\t\h\" & ms & ", dd \d\a\y\" & ds & "," & vbnewline & "hh \h\o\u\r\" & hs & ", mm \m\i\n\u\t\e\" & mms & ", \a\n\d ss \s\e\c\o\n\d\" & ss) end function
now i'm not sure how vba knows difference between mm
, mm
when comes formatting time, on lines mx
, mmx
determined if "s" needed, treats month value. how tell minutes?
there's weird "fault" line x = now() - lastlti
(where lastlti
date of lastly accident). when returned in vba comes month , day on it, when done in excel returns right value. example, if it's been 1 day since lat accident (down second), vba returns next string: "00 years, 01 month, 02 days, 00 hours, 00 min , 00 seconds" <-- notice minutes has dropped s because "month" equal 1.
i hope explains i'm trying achieve!
thanks in advance
i utilize few different date functions including datediff returns difference between 2 dates given specified interval, dateadd
inverse of allowing add specified intervals date value. utilize timevalue
function returns time portion of date.
i think gets want, or @ to the lowest degree should very close.
function lti(lastlti date) dim yx long dim mx long dim dx long dim hx long dim mmx long dim sx long dim ys string dim ms string dim ds string dim hs string dim mms string dim ss string dim dtnow date dtnow = now() yx = datediff("yyyy", dtnow, lastlti) ys = iif(yx = 1, "", "s") mx = datediff("m", dateadd("yyyy", yx, dtnow), lastlti) ms = iif(mx = 1, "", "s") dx = format(dtnow - lastlti, "dd") ds = iif(dx = 1, "", "s") hx = datediff("h", timevalue(dtnow), timevalue(lastlti)) hs = iif(hx = 1, "", "s") 'compute remaining minutes not allocated whole hour, above: mmx = format(timevalue(dtnow), "n") - format(timevalue(lastlti), "n") mms = iif(mmx = 1, "", "s") ' compute remaining seconds not allocated whole minute, above: sx = format(timevalue(dtnow), "ss") - format(timevalue(lastlti), "ss") ss = iif(sx = 1, "", "s") lti = yx & "\y\e\a\r\" & ys & ", " & _ mx & "\m\o\n\t\h\" & ms & ", " & _ dx & "\d\a\y\" & ds & "," & vbnewline & _ hx & "\h\o\u\r\" & hs & ", " & _ mmx & "\m\i\n\u\t\e\" & mms & ", \a\n\d " & _ sx & "\s\e\c\o\n\d\" & ss end function
excel vba date excel-vba formatting
Comments
Post a Comment