Converting imported text as dd/mm/yyyy to date as yyyymmdd

exceltime

New Member
Joined
Aug 19, 2014
Messages
29
I have an Excel macro which imports a csv file with all the fields specified as text. Among these fields is a field whith a date. This fields appears as dd/mm/yyyy. When it comes to copy this field to another sheet converting it to date format yyyymmdd and later sort on the fields it does not work. Help is highly appreciated. Here is the code:
Code:
 = J + 1                        'is a acounter
    Dim tdate As Date
    Dim sDate As String
    tdate = Sheets(SSource).Cells(I, ColE).Value     'from original imported file
    sDate = Format(Tdate, "yyyymmdd")
    Sheets(tName).Cells(J, ColA).Value = Tdate
J
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
First question...

Why is it
Code:
Sheets(tName).Cells(J, ColA).Value = Tdate
and not
Code:
Sheets(tName).Cells(J, ColA).Value = [B][COLOR="#FF0000"]sDate[/COLOR][/B]

Second question....
Are your Windows regional date settings dd/mm/yyyy or mm/dd/yyyy?
 
Last edited:
Upvote 0
First question...

Why is it
Code:
Sheets(tName).Cells(J, ColA).Value = Tdate
and not
Code:
Sheets(tName).Cells(J, ColA).Value = [B][COLOR=#FF0000]sDate[/COLOR][/B]

Second question....
Are your Windows regional date settings dd/mm/yyyy or mm/dd/yyyy?

I am using Tdate to simplify the format function. The result formatting then sDate to format the result. However I have used your proposal also but it gives the same error.

My regional settings are dd/mm/yyyy
 
Upvote 0
However I have used your proposal also but it gives the same error

It wasn't a proposal, it was a question as sDate was doing nothing to change Tdate.
Try the code below.

Code:
tdate = CDate(Sheets(SSource).Cells(I, ColE).Value)     'from original imported file
sDate = Format(tdate, "yyyymmdd")
Sheets(tName).Cells(J, ColA).Value = sDate
 
Last edited:
Upvote 0
It wasn't a proposal, it was a question as sDate was doing nothing to change Tdate.
Try the code below.

Code:
tdate = CDate(Sheets(SSource).Cells(I, ColE).Value)     'from original imported file
sDate = Format(tdate, "yyyymmdd")
Sheets(tName).Cells(J, ColA).Value = sDate
I tried same error "Type mismatch"
 
Upvote 0
Post your full code so we can check that your variables are correct, including the variables for SSource and tName as the code doesn't err for me when they are correct.

Btw, the last post is the first post where you have mentioned that you received a "Type mismatch" error (all you stated before was "it does not work").
If you did then I would have asked for the full code in post #2.
 
Last edited:
Upvote 0
Post your full code so we can check that your variables are correct, including the variables for SSource and tName as the code doesn't err for me when they are correct.

Btw, the last post is the first post where you have mentioned that you received a "Type mismatch" error (all you stated before was "it does not work").
If you did then I would have asked for the full code in post #2.
Code:
Set SSource = ActiveWorkbook.ActiveSheet
Code:
tName = InputBox("Results sheet is " & CelVal, "INGSearch", CelVal) 
         If Not sheetExists(tName) Then
              Sheets.Add().Name = tName
         End If
Code:
J = J + 1
    Dim tDate As Date
    Dim sDate As String
    tDate = CDate(Sheets(SSource).Cells(I, ColE).Value)     'from original imported file
    sDate = Format(tDate, "yyyymmdd")
    Sheets(tName).Cells(J, ColA).Value = sDate
THe counter J may go 1 to 500. J is inner loop. There is a counter I in the outer loop and I may go from 1 to 4000. The statement beginning with tDate fails with error 13.
 
Upvote 0
SSource is a worksheet variable not a string variable so...

Code:
tDate = CDate(SSource.Cells(I, ColE).Value)

and I still very much doubt that is the full code as you aren't showing the variables for I, J, ColA and ColE, so if you have anymore queries I need the full code as already requested.
 
Last edited:
Upvote 0
SSource is a worksheet variable not a string variable so...

Code:
tDate = CDate(SSource.Cells(I, ColE).Value)

and I still very much doubt that is the full code as you aren't showing the variables for I, J, ColA and ColE, so if you have anymore queries I need the full code as already requested.

It worked like charm. Thanks a lot. I have another problem with sorting, but I'll open a separate thread for that.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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