简体   繁体   English

通过Excel VBA打开CSV文件-日期格式更改

[英]Opening CSV files via Excel vba - date format change

There are multiple questions concerning this problem already however none of the answers seem to solve the problem for me. 关于这个问题已经有多个问题,但是似乎没有答案可以为我解决问题。 The problem is this: I am having to edit a large number of .csv files so am doing this via Excel VBA. 问题是这样的:我必须编辑大量的.csv文件,所以要通过Excel VBA进行此操作。 The first three columns are dates in format dd/mm/yyyy hh:mm:ss and these dates are converted to the US format MDY when opening via 前三列是以dd / mm / yyyy hh:mm:ss格式显示的日期,通过以下方式打开时,这些日期将转换为美国格式MDY。

    set wb1 = Workbooks.Open (Filename:=savefolder & filename, Local:=True)

The most common answer for resolving this issue surrounds the known 'feature' that Excel will open CSV with the US dateformat unless the regional settings are used via the Local:=True but I have done this and it is still not working. 解决此问题的最常见答案围绕着已知的“功能”,即除非通过Local:= True使用区域设置,否则Excel将使用美国日期格式打开CSV,但我已经做到了,但仍然无法正常工作。 Can anyone advise of a setting that might need changing to get the following code to work? 谁能建议一个可能需要更改才能使以下代码正常工作的设置? thanks. 谢谢。

    Sub cleandata()
    dim wb1 as workbook
    dim ws1 as worksheet
    dim savefolder, filename as string

    savefolder = "C:\Users\ME\Desktop\Clean Up Folder\"
    filename = dir(savefolder & "*")
    Do while len(filename) >0
           set wb1 = Workbooks.Open (Filename:=savefolder & filename, Local:=True)
           set ws1 = wb1.worksheets(1)
           'code related to what I am wanting to do to the data to 'clean it up'
           wb1.close true
           filename = dir()
    Loop
    End Sub

Open your file using the Data >> From Text tool: in Step 3 of the process you can specify the date format of the incoming data. 使用Data >> From Text工具打开文件:在流程的第3步中,您可以指定传入数据的日期格式。

Recording a macro will give you a start on the required code. 记录宏将使您从所需的代码开始。

在此处输入图片说明

If you're using the TextToColumns function, you can use FieldInfo:=Array(1, 4) parameter. 如果使用的是TextToColumns函数,则可以使用FieldInfo:=Array(1, 4)参数。

I was having a similar issue and this worked for me (besides using Local:=True ) 我有一个类似的问题,这对我有用(除了使用Local:=True

It only worked using both "solutions". 它只能使用两个“解决方案”。

Good luck! 祝好运!

@SMLBW @SMLBW

change your system short time and long time to the "dd/mm/yyyy" format. 将您的系统短时间和长时间更改为“ dd / mm / yyyy”格式。

Im hoping this should do the trick: 我希望这可以解决问题:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM