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....
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,335
Messages
5,595,573
Members
413,996
Latest member
mabelO

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
Top