VBA CODE for converting text to a DATE :-?

NeelsBorstlap

New Member
Joined
Jul 23, 2010
Messages
25
Hi
Working with Excel 2016
I'm receiving a "csv" file from a 3rd party program with dates in column "A" but it is text and I can not format it to a date in excel.

I'm looking for a VBA code that will take the text values in column "A" like 16/09/2015 for 16-Sep-2015 and like 03/11/2015 for 3-Nov-2015 and covert it to column "S" as a date like
16-Sep- 2015 and 3-Nov-2015. (In each file the number of rows with data is different.)

In column "R" the process must be repeated but all the "days" must be change to one day (say the 1st of that month) for all the text in column "A" and the format to look like this
Nov-15 for 01/11/2015 or 1-Nov-2015.

At the end it must replace the data in column "A" and insert a new column in column "B" and the "MONTH" data must go there

I did record a code for this but it involve copy and paste and it is very slow in running - very inefficient. See code below.
I want to add sample of the data but do not how to post that or attached it.

Code:
Sub dateconvert ()


    Columns("A:A").Select
    Selection.Copy
    Range("S1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.TextToColumns Destination:=Range("S1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="/", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
        TrailingMinusNumbers:=True
    Range("V1").Select
    Selection.End(xlDown).Select
    Range("U65536").Select
    Selection.End(xlUp).Offset(0, 1).Select
    ActiveCell.FormulaR1C1 = "1"
    Selection.End(xlUp).Select
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "=+RC[-1]&""/""&RC[-2]&""/""&RC[-3]&""n"""
    Range("V2").Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Range("W2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="n", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
  Selection.NumberFormat = "[$-409]d-mmm-yy;@"
    
    
    Range("V2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("X2").Select
    Application.CutCopyMode = False
    Range("R2").Select
    Selection.Locked = False
    Selection.FormulaHidden = False
    ActiveCell.FormulaR1C1 = "1"
    Range("R3").Select
    Selection.End(xlDown).Select
    Range("S65536").Select
    Selection.End(xlUp).Offset(0, -1).Select
'    Range("R11259").Select
    ActiveCell.FormulaR1C1 = "1"
    Selection.End(xlUp).Select
    Selection.Copy
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Selection.Copy
    Range("S2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("V2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("X2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Selection.Replace What:="n", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    
    Selection.NumberFormat = "[$-409]mmm-yy;@"
    
    
    Range("W2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("B2").Select
    Selection.EntireColumn.Insert
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Month"
    Range("Y2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Range("B2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("N:Z").Select
    Selection.Clear

End Sub


Thank for you help and time :)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi. Could you provide a list of sample data of what these dates look like from the CSV.
 
Upvote 0
Are all the incoming dates in the same formuat dd/mm/yyyy, particularly can we be sure of no one digit day or month values, i.e. no 16/3/2015.
 
Upvote 0
Mikerickson - yes, they are all the same like:

[TABLE="width: 83"]
<colgroup><col></colgroup><tbody>[TR]
[TD]16/09/2015[/TD]
[/TR]
[TR]
[TD]15/09/2015[/TD]
[/TR]
[TR]
[TD]02/09/2015[/TD]
[/TR]
[TR]
[TD]29/09/2015[/TD]
[/TR]
[TR]
[TD]26/09/2015[/TD]
[/TR]
[TR]
[TD="align: right"]21/10/2015[/TD]
[/TR]
[TR]
[TD="align: right"]21/11/2015[/TD]
[/TR]
[TR]
[TD]01/09/2015[/TD]
[/TR]
[TR]
[TD]30/09/2015[/TD]
[/TR]
[TR]
[TD]25/09/2015[/TD]
[/TR]
[TR]
[TD]25/09/2015[/TD]
[/TR]
[TR]
[TD]14/09/2015[/TD]
[/TR]
[TR]
[TD]01/09/2015[/TD]
[/TR]
[TR]
[TD]17/09/2015[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
If the dates in A1 are all in dd/mm/yyyy format, code like this will convert that to excel serial date and also a first of month column.

Code:
With Sheet1.Range("A:A")
    With Range(Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))
        With .Offset(0, 1)
            .FormulaR1C1 = "=DATEVALUE(MID(RC1,4,2) & ""/""&SUBSTITUTE(Rc1, MID(RC1,3,4), ""/""))"
            .NumberFormat = "mmm dd yyyy"
            .Offset(0, 3).FormulaR1C1 = "=EOMONTH(RC" & .Column & ",-1)+1"
            With .Offset(0, 3)
                .NumberFormat = "mmm dd yyyy"
                .Value = .Value
            End With
            .Value = .Value
        End With
    End With
End With
 
Upvote 0
Hi Mikerickson

Thanks - did run the code but if give error in second row - error = Run-time error "104" , Method 'Range' of object'_Global' failed

There is a heading in row 1, and data in column A to N

here is a sample

[TABLE="width: 1130"]
<colgroup><col><col><col><col><col><col><col><col><col span="2"><col span="2"><col><col></colgroup><tbody>[TR]
[TD]Date[/TD]
[TD]File No[/TD]
[TD]Name[/TD]
[TD]Loan No[/TD]
[TD]Paypoint[/TD]
[TD]Operator[/TD]
[TD]Ref[/TD]
[TD]Description[/TD]
[TD]Total[/TD]
[TD]Capital[/TD]
[TD]Int.[/TD]
[TD]Cost[/TD]
[TD]Vat[/TD]
[TD]Ins.[/TD]
[/TR]
[TR]
[TD]16/09/2015[/TD]
[TD="align: right"]510[/TD]
[TD]ARIES MM[/TD]
[TD="align: right"]7684[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]L7684[/TD]
[TD][/TD]
[TD="align: right"]4712.25[/TD]
[TD="align: right"]4000[/TD]
[TD="align: right"]212.25[/TD]
[TD="align: right"]500[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15/09/2015[/TD]
[TD="align: right"]510[/TD]
[TD]ARIES MM[/TD]
[TD="align: right"]7533[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]R6328[/TD]
[TD]Receipt Bank TransferN[/TD]
[TD="align: right"]4704.93[/TD]
[TD="align: right"]4000[/TD]
[TD="align: right"]204.93[/TD]
[TD="align: right"]500[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]02/09/2015[/TD]
[TD="align: right"]537[/TD]
[TD]BEUKES JJ[/TD]
[TD="align: right"]7625[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]L7625[/TD]
[TD][/TD]
[TD="align: right"]766.1[/TD]
[TD="align: right"]600[/TD]
[TD="align: right"]26.1[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]29/09/2015[/TD]
[TD="align: right"]537[/TD]
[TD]BEUKES JJ[/TD]
[TD="align: right"]7751[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]L7751[/TD]
[TD][/TD]
[TD="align: right"]1702.76[/TD]
[TD="align: right"]1400[/TD]
[TD="align: right"]62.76[/TD]
[TD="align: right"]240[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]26/09/2015[/TD]
[TD="align: right"]537[/TD]
[TD]BEUKES JJ[/TD]
[TD="align: right"]7625[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]R6368[/TD]
[TD]Receipt Bank TransferN[/TD]
[TD="align: right"]766.1[/TD]
[TD="align: right"]600[/TD]
[TD="align: right"]26.1[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21/09/2015[/TD]
[TD="align: right"]418[/TD]
[TD]BOLTMAN C[/TD]
[TD="align: right"]7705[/TD]
[TD]RUAN[/TD]
[TD][/TD]
[TD]L7705[/TD]
[TD][/TD]
[TD="align: right"]4712.25[/TD]
[TD="align: right"]4000[/TD]
[TD="align: right"]212.25[/TD]
[TD="align: right"]500[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]21/09/2015[/TD]
[TD="align: right"]418[/TD]
[TD]BOLTMAN C[/TD]
[TD="align: right"]7546[/TD]
[TD]RUAN[/TD]
[TD][/TD]
[TD]R6331[/TD]
[TD][/TD]
[TD="align: right"]4697.61[/TD]
[TD="align: right"]4000[/TD]
[TD="align: right"]197.61[/TD]
[TD="align: right"]500[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]01/09/2015[/TD]
[TD="align: right"]502[/TD]
[TD]BOOYSEN H[/TD]
[TD="align: right"]7618[/TD]
[TD]EMMANUEL[/TD]
[TD][/TD]
[TD]L7618[/TD]
[TD][/TD]
[TD="align: right"]772.91[/TD]
[TD="align: right"]600[/TD]
[TD="align: right"]32.91[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
I missed a dot, it should be
Code:
With Range([COLOR="#FF0000"][/COLOR][B].[/B]Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp))

Also, the first line should be adjusted to match your sheet name.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
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