How to prevent recorded vba macro from swapping my

2019-07-27 05:57发布

the context of this questions is with regards to the macro recording of the text to column function. My country uses dd/mm/yyyy date format, but a certain database in the company I'm working for uses mm/dd/yyyy. I am unable to change the date format of the database or the template prior to extracting because I am a lowly intern and the company is a MNC.

So my problem:

Manual Text to Column
Recorded macro Text to Column
Column B is the raw data from the database, Columns AL to AN is where the the end result of the text is column is, so AL is Month, AM is Day and AN is Year. Column O is where I do =DATE(LEFT(AN,4),AL,AM) to get the dates in dd/mm/yyyy.

So dates in the format of mm/dd/yyyy hh:mm PM(12 hour format) are recognised as string, while dates in the mm/dd/yyyy hh:mm (24 hour format) are recognised as dates. The macro manages to split the string dates nicely with no issues, but for the date dates, as you can see from the 2 pictures linked, the values are swapped.

Hope someone can enlighten me regarding this quirky aspect of macro recording for Text to Column, I apologise if my explanation of my problem is not sufficient/detailed enough, let me know if you need more information! Thanks in advance.

Edit: I only realised I forgot to include the recorded vba code after I turned off my computer, my bad.

Sub TextToCol()
'
' TextToCol Macro
'

'
Columns("B:B").Select
Range("B3").Activate
Selection.TextToColumns Destination:=Range("AL1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
    :="/", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
    TrailingMinusNumbers:=True
End Sub

0条回答
登录 后发表回答