excel vba - How to get the Date from Time Stamp -
am having 1 excel file contains data multiple sheets. in 1 column having time stamp 21/6/12 10:33:07:am. system date format "m/d/yyyy"
while converting date 21/06/2012. able date showing in wrong. 06-12-2021 should 21-06-2012. using below code.
ex : 26/6/12 11:15:07:am should 26/06/2012
21/6/12 10:33:07:am should 21/06/2012
public sub convtdate() dim parsedatetime date application.screenupdating = false each datcol in ws_raw2.range("i2:i65536") x = instr(1, datcol, " ", vbtextcompare) - 1 if x > 0 parsedatetime = datevalue(left(datcol, x)) datcol.value = parsedatetime end if next application.screenupdating = true end sub
please how date.
in advance.
datevalue
expects date string in format set system.
excel help:
if date string includes numbers separated valid date separators, datevalue recognizes order month, day, , year according short date format specified system.
since data in day month year
, system month day year
need reconstruct parameter pass datevalue
.
here's refactor of coed, dealing few other issues:
dim
all variables (useoption explicit
force this)- only process rows need to
- loop variant array rather range, much faster
public sub convtdate() dim parsedatetime date dim long dim x long dim a() string dim dat variant dim rng range application.screenupdating = false set rng = range(cells(2, 9), cells(rows.count, 9).end(xlup)) dat = rng.value = 1 ubound(dat, 1) x = instr(1, dat(i, 1), " ", vbtextcompare) - 1 if x > 0 = split(left(dat(i, 1), x), "/") parsedatetime = datevalue(a(1) & "/" & a(0) & "/" & a(2)) ' or if dont know system data format use ' parsedatetime = dateserial(a(2), a(1), a(0)) dat(i, 1) = parsedatetime end if next rng = dat application.screenupdating = true end sub
Comments
Post a Comment