date problem

codesmith

Active Member
Joined
Apr 23, 2008
Messages
257
hey all ...

I have a string in format:

Code:
xxxxxxxx  0007      device in xxxxxxxxxxx                    02Jul08 07:31:12

I am extracting the date portion from this string as such:

Code:
Function returnDate(statusLine As String) As String
    Dim lineDay As String
    Dim lineMonth As String
    Dim lineYear As String
    Dim rawdate As String
    
    statusLine = Trim(statusLine)
    
    rawdate = Right(statusLine, 16)
    
    lineDay = Left(rawdate, 2)
    lineMonth = Mid(rawdate, 3, 3)
    lineMonth = returnMonth(lineMonth)
    lineYear = Mid(rawdate, 6, 2)
    
    If lineMonth <= 9 Then: lineMonth = "0" & lineMonth
    
    returnDate = lineDay & "/" & lineMonth & "/20" & lineYear
    
End Function


Then writing this date to a general cell (and tried dd/mm/yyyy format cell too) as such:

Code:
    Dim lineDate As String
    wksWorking.Range("F2") = returnDate(wksNMI.Range("A4"))


This does not hold the format. It keeps changing the date to mmddyyyy (07/02/2008) ....

how would I ensure this does not happen?

Any help appreciated....
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I don't think you should have to do all that to get the date from that. Since you are using leading zeros on your single digit numbers, the length of the date time should not change:

Code:
Function returndate(r As String)
returndate = Left(Right(r, 16), 7)
End Function
 
Sub test()
Range("F2") = returndate(Range("A1"))
Range("F2").NumberFormat = "mm/dd/yy"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top