Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 84
Default Date conversion

I have a report that I download into Excel. The report has dates for many
months. The current date format is ymmdd (ie 60530) and I'd like to convert
it to an easier to read format like 05/30/06. Is there code that I can use
in an existing page setup macro to do this for all rows in the speadsheet?
I tried using the Data\Text to Columns\and changed the text to a long date,
but it did not do it properly, it read the 60530 as 5/30/1960! Oh is there
code to automatically add 200 before the 60530. I added that and ran the Data
Text to Column and it converted properly.
Thanks


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Date conversion


This will format dates in column A from row 2 down.

Sub FormatDates()
Dim RowNum As Integer
Dim NextValue As String
Dim NextDate As Date

For RowNum = 2 To Range("A1").CurrentRegion.Rows.Count
NextValue = Range("A" & RowNum).Value
NextDate = DateSerial(2000 + Mid(NextValue, 1, 1),
Mid(NextValue, 2, 2), Mid(NextValue, 4, 2))
Range("A" & RowNum).NumberFormat = "dd/mm/yyyy"
Range("A" & RowNum).Value = NextDate
Next RowNum
End Sub

Personally I find excel's handling of dates to be eratic at best. Even
when I force the dates into a format I want, if I copy the sheet to
another workbook the formats are totally screwed up. Then if you try to
format the column post-copy it just doesn't work.


--
fazstp
------------------------------------------------------------------------
fazstp's Profile: http://www.excelforum.com/member.php...o&userid=30574
View this thread: http://www.excelforum.com/showthread...hreadid=548400

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Date conversion

Hi SITCFanTN,

Try:

'=============
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim rng As Range
Dim rCell As Range
Dim iLastRow As Long
Const dateCol As String = "A" '<<=== CHANGE

Set WB = Workbooks("YourBook.xls") '<<==== CHANGE
Set SH = WB.Sheets("Sheet1") '<<==== CHANGE

iLastRow = SH.Cells(Rows.Count, dateCol).End(xlUp).Row

Set rng = SH.Range(dateCol & "2:" & dateCol & iLastRow)

On Error GoTo XIT
Application.ScreenUpdating = False

For Each rCell In rng.Cells
With rCell
If IsNumeric(.Value) And .Value < "" Then
.Value = 20 & .Value
End If
End With
Next rCell

rng.TextToColumns Destination:=rng(1), _
DataType:=xlDelimited, _
FieldInfo:=Array(1, 5)
XIT:
Application.ScreenUpdating = False
End Sub
'<<=============


---
Regards,
Norman

"SITCFanTN" wrote in message
...
I have a report that I download into Excel. The report has dates for many
months. The current date format is ymmdd (ie 60530) and I'd like to
convert
it to an easier to read format like 05/30/06. Is there code that I can
use
in an existing page setup macro to do this for all rows in the speadsheet?
I tried using the Data\Text to Columns\and changed the text to a long
date,
but it did not do it properly, it read the 60530 as 5/30/1960! Oh is
there
code to automatically add 200 before the 60530. I added that and ran the
Data
Text to Column and it converted properly.
Thanks




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Date conversion

Hi SITCFanTN,

.Value = 20 & .Value


Should read:

.Value = 200 & .Value

---
Regards,
Norman


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Date conversion

With the weird date in the first column -

You could use a text function without VBA -

=VALUE(CONCATENATE(MID(A1,2,2),"/",RIGHT(A1,2),"/",LEFT(A1,1)))

You'll get the value for the date you want. You can format before or
after.

Or you could enter the function and format with VBA -

Sub convertDate()
With ThisWorkbook.Sheets("Sheet1").Range("a1") _
..CurrentRegion.Offset(1, 0)
..Columns(2).FormulaR1C1 = _
"=VALUE(CONCATENATE(MID(RC[-1],2,2),""/"",RIGHT(RC[-1],2),""/"",LEFT(RC[-1],1)))"
..Columns(2).NumberFormat = "mm/dd/yy;@"
..Columns(1).SpecialCells(xlCellTypeBlanks).Entire Row.Delete
End With
End Sub

What kind of source do you have? A lot of times you can do some
manipulation (auto)before export that will clear this kind of pain up.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Date conversion

This will convert the functions to values -

Sub convertDate()
With ThisWorkbook.Sheets("Sheet1").Range("a1") _
..CurrentRegion.Offset(1, 0)
..Columns(2).FormulaR1C1 = _
"=VALUE(CONCATENATE(MID(RC[-1],2,2),""/"",RIGHT(RC[-1],2),""/"",LEFT(RC[-1],1)))"
..Columns(2).NumberFormat = "mm/dd/yy;@"
..Columns(1).SpecialCells(xlCellTypeBlanks).Entire Row.Delete
..CurrentRegion.Offset(1, 0).Columns(2) = .CurrentRegion.Offset(1,
0).Columns(2).Value
End With
End Sub

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 72
Default Date conversion

HI Norman, How would I edit this code if I was just using it in the active
sheet? Thanks a bunch

"Norman Jones" wrote:

Hi SITCFanTN,

.Value = 20 & .Value


Should read:

.Value = 200 & .Value

---
Regards,
Norman



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 84
Default Date conversion

HI Fazstp,

Thanks so much for the code, but for some reason I'm getting a compile sytax
error on the 2 lines shown with the blank rows around it. Any other help you
can give me is appreciated. Thanks

Sub FormatDates()
Dim RowNum As Integer
Dim NextValue As String
Dim NextDate As Date
For RowNum = 2 To Range("A1").CurrentRegion.Rows.Count
NextValue = Range("A" & RowNum).Value


NextDate = DateSerial(2000 + Mid(NextValue, 1, 1),
Mid(NextValue, 2, 2), Mid(NextValue, 4, 2))


Range("A" & RowNum).NumberFormat = "dd/mm/yyyy"
Range("A" & RowNum).Value = NextDate
Next RowNum
End Sub



"fazstp" wrote:


This will format dates in column A from row 2 down.

Sub FormatDates()
Dim RowNum As Integer
Dim NextValue As String
Dim NextDate As Date

For RowNum = 2 To Range("A1").CurrentRegion.Rows.Count
NextValue = Range("A" & RowNum).Value
NextDate = DateSerial(2000 + Mid(NextValue, 1, 1),
Mid(NextValue, 2, 2), Mid(NextValue, 4, 2))
Range("A" & RowNum).NumberFormat = "dd/mm/yyyy"
Range("A" & RowNum).Value = NextDate
Next RowNum
End Sub

Personally I find excel's handling of dates to be eratic at best. Even
when I force the dates into a format I want, if I copy the sheet to
another workbook the formats are totally screwed up. Then if you try to
format the column post-copy it just doesn't work.


--
fazstp
------------------------------------------------------------------------
fazstp's Profile: http://www.excelforum.com/member.php...o&userid=30574
View this thread: http://www.excelforum.com/showthread...hreadid=548400


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 84
Default Date conversion


HI Ward,

I'm getting a Compile Syntax error on the first two lines of code. Any
assistnace you can give me to get it to run is appreciated. I'm putting this
code in an existing module for the active document. Thanks

"ward376" wrote:

With the weird date in the first column -

You could use a text function without VBA -

=VALUE(CONCATENATE(MID(A1,2,2),"/",RIGHT(A1,2),"/",LEFT(A1,1)))

You'll get the value for the date you want. You can format before or
after.

Or you could enter the function and format with VBA -

Sub convertDate()
With ThisWorkbook.Sheets("Sheet1").Range("a1") _
..CurrentRegion.Offset(1, 0)
..Columns(2).FormulaR1C1 = _
"=VALUE(CONCATENATE(MID(RC[-1],2,2),""/"",RIGHT(RC[-1],2),""/"",LEFT(RC[-1],1)))"
..Columns(2).NumberFormat = "mm/dd/yy;@"
..Columns(1).SpecialCells(xlCellTypeBlanks).Entire Row.Delete
End With
End Sub

What kind of source do you have? A lot of times you can do some
manipulation (auto)before export that will clear this kind of pain up.


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Date conversion


It should be one line; either just put it on one line or use line
continuation, a space and an underscore.



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default Date conversion

Thats for zap's code -
ward376 wrote:
It should be one line; either just put it on one line or use line
continuation, a space and an underscore.


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Date conversion

Hi JOUIOUI,

HI Norman, How would I edit this code if I was just using it in the active
sheet? Thanks a bunch


Try changing:

Set WB = Workbooks("YourBook.xls") '<<==== CHANGE
Set SH = WB.Sheets("Sheet1") '<<==== CHANGE


to

Set SH = ActiveSheet


---
Regards,
Norman


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
Date conversion pete Excel Worksheet Functions 2 November 20th 09 04:25 PM
Date Conversion mecca Excel Worksheet Functions 7 February 2nd 09 08:00 PM
Date conversion Charlie7805 Excel Worksheet Functions 4 March 4th 08 06:50 PM
Date Conversion Port Man Excel Discussion (Misc queries) 6 March 2nd 07 12:55 AM
Date Conversion JD McLeod Excel Worksheet Functions 1 June 17th 05 01:07 PM


All times are GMT +1. The time now is 09:46 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"