导出为CSV时更改格式

时间:2019-01-02 20:09:45

标签: excel vba

我有此宏可以将Excel导出为CSV:

Sub To_CSV()
Dim tmpWS As Worksheet
Application.DisplayAlerts = False

For Each WS In ThisWorkbook.Worksheets

If WS.Range("B1").Value <> "" Then
     If WS.Range("c1").Value <> "" Then
         filePath = Environ("USERPROFILE") & "\Desktop\" & "Pozo de Bombeo " & WS.Range("B1").Value & ".csv"
     Else
         filePath = Environ("USERPROFILE") & "\Desktop\" & "Pozo de Observacion " & WS.Range("B1").Value & ".csv"
     End If
     WS.Copy
     Set tmpWS = ActiveSheet
     tmpWS.SaveAs Filename:=filePath, FileFormat:=xlCSV
     tmpWS.Parent.Close False

End If
Next


Application.DisplayAlerts = True
End Sub

但是当我打开CSV“ POZO DE BOMBEO”时,某些日期的格式显然是随机的。

1 个答案:

答案 0 :(得分:0)

这是我的输出示例:

11/27/2018 11:19 11/27/2018 11:20 11/27/2018 11:25 11/27/2018 11:30 11/27/2018 11:35 11/27/2018 11:40 11/27/2018 11:45 11/27/2018 13:00 11/27/2018 16:00 11/27/2018 17:15 27-11-2018 22:10 27-11-2018 23:40 28-11-2018 1:10 28-11-2018 2:30 28-11-2018 3:40 28-11-2018 4:10 28-11-2018 5:10

原始输入为DD-MM-YYYY HH:MM,但是由于某些原因,您看到了它的变化。幸运的是,我能够找到解决方案:

tmpWS.SaveAs Filename:=filePath, FileFormat:=xlCSV, local:=True 

只需在Workbook.SaveAs方法中添加:“ local:= True”。