VBA Text to column code clean up

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
Hi, could someone please help me clean this code up, i am using delimiter to change Date Text to number

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
    Range("G6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("G6"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
End Sub
 
Why do you need to convert those? They look like UK-style dates (dd/mm/yyyy) to me.

That actual format is correct but some of those dates are Text, i cant replicate the text dates in the screen shot.

When i do text to columns manually it works but if i record the same steps it does not work
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Why do you need to convert those? They look like UK-style dates (dd/mm/yyyy) to me.

Vog, when i record macro via Fixed width it works but using delimiter does not work, why is this the case? What would you use? to convert Text dates to number?

Code:
Sub Macro3()
'
' Macro3 Macro
'

'
    Range("G6").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.TextToColumns Destination:=Range("G6"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(0, 4), TrailingMinusNumbers:=True
End Sub
 
Upvote 0
Hi Mark, this is the original data, this data came from MS Access

Excel Workbook
G
5Start Date
629/04/2009
706/04/2009
808/09/2010
922/03/2011
1022/03/2011
1104/03/2011
1207/07/2011
1308/03/2011
1408/03/2011
1522/03/2011
Office_Address_List

Hi again,

Its probably just me, but I am not understanding this: If the data is coming over like this, why are we using Text2Columns? We're not splitting anything, are we?

Sorry if I'm being thick-headed, but thought to ask.

Mark
 
Upvote 0
Hi Mark.

TTC converts dates stored as text to Excel dates. To try it enter a date preceded by a ' then TTC, click Next twice, check date and select MDY then click Finish.
 
Upvote 0
Hi again,

Its probably just me, but I am not understanding this: If the data is coming over like this, why are we using Text2Columns? We're not splitting anything, are we?

Sorry if I'm being thick-headed, but thought to ask.

Mark

Mark some of those dates is in Text format, i cant replicate this on the screen shot, therefore i used to Text to columns to convert to number, do you have any other suggestions?
 
Upvote 0
Hi Mark.

TTC converts dates stored as text to Excel dates. To try it enter a date preceded by a ' then TTC, click Next twice, check date and select MDY then click Finish.

Hi Peter,

Thank you for that, I did indeed forget this. When I try this with the OP's supplied data (preface: American date default here, and to prevent it from converting the dates is recognizes, I changed numberformat to text before typing in the values...), I get:
Excel Workbook
J
129/04/2009
26/4/2009
38/9/2010
422/03/2011
522/03/2011
64/3/2011
77/7/2011
88/3/2011
922/03/2011
Sheet3
Excel 2003

Mark some of those dates is in Text format, i cant replicate this on the screen shot, therefore i used to Text to columns to convert to number, do you have any other suggestions?

Okay, it sounds as though you may have solved it, but if not, I was thinking a quick stab might be: In a junk copy of your workbook, select the offending text/dates and try -
Rich (BB code):
Sub exa()
Dim myRange As Range, Cell As Range
Dim aryTemp
    
    Set myRange = Selection
    
    For Each Cell In myRange
        If Cell.NumberFormat = "@" Or Cell.NumberFormat = "General" Then
            If Cell.Text Like "##/##/####" Then
                aryTemp = Split(Cell.Text, "/")
                Cell.NumberFormat = "dd/mm/yyyy"
                Cell.Value = DateSerial(aryTemp(2), aryTemp(1), aryTemp(0))
                
            End If
        End If
    Next
End Sub
Does that do anything (preferably anything good...) ?

Mark
 
Upvote 0
Nope that did not work, i ran your code but nothing happened.

But this code definately works, i'm just gobsmaked that when recording steps for Delimiter the code does not work but recording steps via fixed width it works, i have no idead why delimiter would not work.

Code:
Private Sub Text()

Dim LR As Long

LR = Range("G" & Rows.Count).End(xlUp).Row
Range("G6:G" & LR).TextToColumns Destination:=Range("G6"), DataType:=xlFixedWidth, _
        FieldInfo:=Array(0, 4), TrailingMinusNumbers:=True
End Sub



Hi Peter,

Thank you for that, I did indeed forget this. When I try this with the OP's supplied data (preface: American date default here, and to prevent it from converting the dates is recognizes, I changed numberformat to text before typing in the values...), I get:
Excel Workbook
J
129/04/2009
26/4/2009
38/9/2010
422/03/2011
522/03/2011
64/3/2011
77/7/2011
88/3/2011
922/03/2011
Sheet3
Excel 2003





Okay, it sounds as though you may have solved it, but if not, I was thinking a quick stab might be: In a junk copy of your workbook, select the offending text/dates and try -
Rich (BB code):
Sub exa()
Dim myRange As Range, Cell As Range
Dim aryTemp
    
    Set myRange = Selection
    
    For Each Cell In myRange
        If Cell.NumberFormat = "@" Or Cell.NumberFormat = "General" Then
            If Cell.Text Like "##/##/####" Then
                aryTemp = Split(Cell.Text, "/")
                Cell.NumberFormat = "dd/mm/yyyy"
                Cell.Value = DateSerial(aryTemp(2), aryTemp(1), aryTemp(0))
                
            End If
        End If
    Next
End Sub
Does that do anything (preferably anything good...) ?

Mark
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,286
Members
449,218
Latest member
Excel Master

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