Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jay Jay is offline
external usenet poster
 
Posts: 671
Default Change dates from dd/mm/yy to 6 digits being ddmmyy

Hi Stuart

The procedure below converts either serial dates or string dates to serial
dates (and optionally to strings) in ddmmyy format. Note the following:

1. If your source dates are text strings, you have to tell the procedure how
the date is constructed by modifying some variables in the declarations
section. Otherwise, date functions (datevalue or dateserial) cant
consistently interpret year, month, or day.

2. The procedure replaces the dates in-place so test it on dummy data.
Or, modify the column OffSet to put the results in an empty column.

3. You mentioned that your system accepts only numeric values. Dates are
numeric, so I assumed you mean numeric digits. Therefore, the procedure
optionally converts dates to strings that contain only numeric digits.

Sub convertDatesInPlace()
'----------------------------------------------------------------
'If dates are text strings, set the values in next 3 statements.
'If dates are true excel serial dates, no modification is needed.
' Enter the within-string order for (m)onth, (d)ay, and (y)ear.
' Enter the date delimiter used.
' Enter number of digits in the year portion (2 or 4).
m = 1: d = 2: y = 3
delimiter = "-"
yeardigits = 2
Set dateRange = Range("A1:A100") '<====adjust input range to suit.
'-----------------------------------------------------------------
'1 is subtracted for split array index
m = m - 1: d = d - 1: y = y - 1
For Each dt In dateRange
On Error GoTo invalidDate
With dt.Offset(0, 0) '<==adjust column offset for results
If IsDate(dt) Then
.NumberFormat = "ddmmyy"
If WorksheetFunction.IsNumber(dt) Then
'Cell contains serial date
.Value = DateValue(dt)
Else
'cell contains valid textual date string, but must be
'converted to true excel serial date before formatting
'because the DateValue function may misinterpret date.
parts = Split(dt, delimiter)
mnth = Val(parts(m))
dy = Val(parts(d))
yr = Val(parts(y)) + IIf(yeardigits = 2, 2000, 0)
.Value = DateSerial(yr, mnth, dy)
End If
End If
End With
invalidDate:
On Error GoTo 0
Next 'dt

'Optional conversion of serial dates to text
If MsgBox("Output dates as text?", vbYesNo) = vbYes Then
For Each dt In dateRange
If IsDate(dt) Then
With dt.Offset(0, 0)
.NumberFormat = "@"
.Value = Format(Day(dt), "0#") & _
Format(Month(dt), "0#") & _
Right(Year(dt), 2)
End With
End If
Next 'dt
End If
End Sub

-----
Jay
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you change single digits to recognized double digits? Evil with a K[_2_] Excel Worksheet Functions 5 April 3rd 23 02:29 PM
VBA write macro change column with 3 number digits to 4 digits the James C[_2_] Excel Discussion (Misc queries) 3 January 25th 10 03:12 PM
how do i format date to DDMMYY Maz Excel Discussion (Misc queries) 2 August 23rd 07 11:07 AM
Converting Dates to 8 digits jermsalerms Excel Worksheet Functions 17 January 20th 06 02:00 AM
Entering dates in vba inputbox as ddmmyy. Excel returns mmddyy pkeegs Excel Programming 3 August 30th 05 08:57 AM


All times are GMT +1. The time now is 04:30 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright 2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"