excel - Date auto conversion -
i have sheet name "02-05-2015". use
dim sheetname string sheetname = activesheet.name msgbox (sheetname)
it displays 02-05-2015 in msg box. if use
range("k2").value = sheetname
its shows 05-02-2015. if cell format changed dd/mmm/yyyy, shows 05-feb-2015. why problem occurs. don't know. if use
range("k2").value = format(sheetname,"dd/mm/yyyy")
no change occurs. need 05/02/2015 or 05-02-2015 in cell date format.
this behaviour-by-design. when put value cell excel tries best figure out it. includes making number actual number (not desired in case of duns number or part number leading zeroes) , trying reconcile appears date system's regional settings of either mm/dd/yyyy or dd/mm/yyyy. trouble 02-05-2015 either 05-feb-2015 or 02-may-2015. decision either dmy or mdy made on computer's system regional setting (found in control panel).
from description, appear wish range("k2") hold date representing 02-may-2015, not 05-feb-2015. seems system set default of mm/dd/yyyy.
this 3 step routine should results looking for.
with range("k2") .value = chr(39) & .parent.name .texttocolumns destination:=range("k2"), datatype:=xlfixedwidth, fieldinfo:=array(0, 4) .numberformat = "mm-dd-yyyy" end
that takes worksheet name , stuffs k2 forcing text value prefacing single quote (e.g. '
or tick). uses text-to-columns command interpret dmy date , store date in current system regional settings. finally, applies cell formatting desired cell display value.
Comments
Post a Comment