.NumberFormat sometimes returns the wrong value with dates and times

前端 未结 3 1053
深忆病人
深忆病人 2020-12-11 23:07

It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the prob

3条回答
  •  借酒劲吻你
    2020-12-12 00:11

    I hacked this: I rewrite the original data in a known format. it relies on DateSerial and TimeSerial:

    'Google spreadsheet stores dates in USA format (MM/DD/YYYY).  We're in Australia, using DD/MM/YYYY, so we need to swap them.
                        '
                            With dc 'the cell who contains a date in USA format.
                                 d = .Value      'capture value in USA format
                                 t = TimeValue(d)
                                 .NumberFormat = "dd/mm/yyyy" 'set to OZ format, so Excel knows the values were swapped in its internal math.
                                 .Value = DateSerial(Year(d), Month(d), Day(d)) 'DateSerial takes y,d,m. We swap Month and Day components, to get OZ format dates
                                 .Value = .Value + TimeSerial(Hour(t), Minute(t), Second(t))
                                 dc.Font.Bold = True                         ' We bold the cells that are swapped, for debugging
                             End With
                        End If
    

提交回复
热议问题