简体   繁体   English

由于某些原因,带有井号的Excel日期格式

[英]Excel Date formatting with hash marks for some reason

The Problem 问题

I've got a VBA program that exports data from one Excel file into a CSV. 我有一个VBA程序,可以将数据从一个Excel文件导出到CSV。 When it comes across a date, it formats it like #2016-06-14# . 遇到日期时,其格式类似于#2016-06-14# I'm assuming the hash marks (or octothorpe or pound sign or hashtag) are meant to indicate that the field is a date field. 我假设井号(或八字形或井号或井号)是用来表示该字段是日期字段。 But, when I'm importing the CSV back into a different Workbook, the date will not come in no matter how I format the field. 但是,当我将CSV导入回另一个工作簿时,无论我如何格式化该字段,日期都不会出现。 It still contains the # characters. 它仍然包含#字符。

The Question 问题

How can I get the date column to import as a YMD format date? 如何获取要导入为YMD格式日期的日期列?

Appendix 附录

Here's some code I'm using to export and import, for reference. 这是我用来导出和导入的一些代码,以供参考。

Export 出口

Sub WriteCSV(writeRange As Range, fileName As String)
    Dim myFile As String, rng As Range, cellValue As Variant, i As Integer, j As Integer

    myFile = ActiveWorkbook.Path & "\Results\" & fileName & ".csv"
    Debug.Print myFile
    Open myFile For Output As #1
    For i = 1 To writeRange.Rows.Count
        For j = 1 To writeRange.Columns.Count
            cellValue = writeRange.Cells(i, j).value
            If j = writeRange.Columns.Count Then
                Write #1, cellValue
            Else
                Write #1, cellValue,
            End If
        Next
    Next
    Close #1
End Sub

Import 进口

Sub ReadCSV(targetCell As Range, filePath As String)

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & filePath, Destination:=targetCell)
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
        .Delete
    End With

End Sub

Try outputing .text instead of .value. 尝试输出.text而不是.value。

Change this: 更改此:

cellValue = writeRange.Cells(i, j).value

to this: 对此:

cellValue = writeRange.Cells(i, j).Text

Discard those # . 丢弃那些#
When I manually Save as a file as CSV, Excel does NOT put those # around the dates. 当我手动Save as文件Save as CSV时,Excel不会将这些#放在日期周围。 Instead it writes the dates as they are defined in my regional settings (that is - for me - dd/mm/yyyy ). 而是按照我在区域设置中定义的日期(即-对我来说dd/mm/yyyy )写入日期。
And when importing back, the recorder used array(...4...) for the date column, and it was imported correctly. 并且在导入回来时,记录器将array(... 4 ...)用于日期列,并且已正确导入。
Anyway CSV really sucks if your are working in an international environment since it behaves differently according the the local machine settings. 无论如何,如果您在国际环境中工作,CSV 确实很烂,因为它的行为会因本地计算机设置而异。 Being poorly defined, it's the last format I would use. 定义不明确,这是我要使用的最后一种格式。

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

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