VBA Text-To-Columns Reading Date Format As US

左心房为你撑大大i 提交于 2020-02-28 06:51:07

问题


When running text-to-columns in vba, it puls my dates as american dates, instead of uk dates, as long as it is possible for it to do so. So for example, 31-Aug stays as 31-Aug, but 01-Sep changes to 09-Jan!

Is there any way to specify that all dates are in UK format BEFORE vba runs? As even just reformatting to US dates to show as US dates won't work because we have code that runs if date < Today, which of course will cause issues with the changing date format.

The data feeds in the format dd/mm/yyyy, but excel reads it as mm/dd/yyyy!


回答1:


pnuts comment is the easiest method to solve this problem.

Explanation:

Run the 'Record Macro', select your column and initiate text-to-columns wizard.

On the 3rd page of the text-to-columns wizard, under 'Column data format', the 'Date' option is automatically set to 'MDY'. Change this to 'DMY'.

Stop the macro and you will find this code

Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A:A"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 4), TrailingMinusNumbers:=True

The "4" in =Array(1,4) is the part that determines DMY format.




回答2:


Maybe you can set the language first with Application.International?

[http://msdn.microsoft.com/en-us/library/office/ff840213(v=office.15).aspx][1]




回答3:


I've got a better option. Define the column as text so:

Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A:A"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 2), TrailingMinusNumbers:=True

Using the 2 format creates a text field so there will be no formating.




回答4:


There is a workarround for this case. First get the date part from the text before doing TexttoColumn. This date will be in text. Use Left, Mid and right functions to get the date, month and year and then use the function dateserial in vba with these arguments. Now you will get 2 type of dates. Date in text format which will be in UK format which you can store in the cells in excel. Other is in UK format which will be used on for if date < Today condition. U can now use text to columns and ignore the date that comes from texttocolumns and use date as text instead. The code for same in mentioned below for eg - "31/08/2014 part1 part2"

Sub t() 

Dim myarr() As String

Dim cell As Range

  For Each cell In Selection ' selection contains cells which text you need to convert text to columns
      i = i + 1
      myarr = Split(cell.Value, " ")
      mydate = Val(Left(myarr(0), 2))
      mymonth = Val(Mid(myarr(0), InStr(1, myarr(0), "/") + 1, 2))
      myyear = Val(Right(myarr(0), 4))
      date_american = DateSerial(myyear, mymonth, mydate)
      date_uk = myarr(0)
  Next cell

End Sub



回答5:


I was experiencing this same issue today, I too used the DMY in text to columns which worked when when doing it manually but in VBA as described above always changes to US data format where possible.

I got around this (which may not be suitable for you) by inserting a new column to the right and using =TEXT(Cell, "DD/MM/YYYY") and then copying and pasting the values.

I deleted the original column afterwards, I also ensure this always works regardless of the amount of rows by working out my last row with data in each time.

VBA segments below..

Sub Dateformat()

'sets i as last current row with data in

    Dim i As Integer

    Range("A2").Select

    i = ActiveCell.End(xlDown).Row

    Columns("D:D").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("D2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""DD/MM/YYYY"")"
Selection.Copy
Range("D2:D" & i).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft

End Sub

Hope this helps.

Thanks

Tom




回答6:


Maybe this observation is helpful (Excel365 64bit):

Conclusion: Be careful to apply TextToColumn ONLY to cells that actually contain text formatted dates and NOT to cells are already recognized as dates by Excel.

Observation: I used VBA to fill cells with data from an array containing dates using "range.value =". The result will be cells filled with the data in text format (e.g. 01.09.2019 is in the cell, but Excel doesn't know it is a date for 1. Sept. 2019 but only sees text). I then used VBA TextToColumns to turn those cells into actual dates specifying "FieldInfo:=Array(1, 4)". The format was indeed DMY.

This worked well BUT all things got messed up when some cells in the range were already recognized as dates by Excel.

Scenario in my case: Data was added using "value = (dates from an arry variable)" to the bottom of a column resulting in text formatted cells. I wanted to use TextToColumn to reformat the data into dates. But TextToColumns was every time applied to the entire column containng cells that had already been reformatted previously instead of only the newly added data.

Here is what happened: For some reason VBA read the already date format cell correctly but then switched around month and day where possible to write them back as MDY. To see what I mean you can try applying TextToColumns to date format cells using "FieldInfo:=Array(1, 2)". 01.09.2019 for 1. Sept. 2019 will be turned into a text cell with "09/01/2019" for 9. January 2019.

We therefore have to be careful to apply TextToColumn ONLY to really text formatted data when writing back in date format. A possible work around may be to somehow first read the entire data into an array and write it back to ensure that all data is of the same format to start with.



来源:https://stackoverflow.com/questions/26119089/vba-text-to-columns-reading-date-format-as-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!