Excel 由于某种原因,日期格式带有井号
Excel Date formatting with hash marks for some reason
问题
我有一个 VBA 程序可以将数据从一个 Excel 文件导出到 CSV 文件中。当遇到日期时,它会将其格式化为 #2016-06-14#
。我假设井号(或 octothorpe 或井号或井号)是为了表明该字段是一个日期字段。但是,当我将 CSV 导入回另一个工作簿时,无论我如何格式化该字段,日期都不会出现。它仍然包含 #
个字符。
问题
如何获取要导入为 YMD 格式日期的日期列?
附录
这是我用来导出和导入的一些代码,供参考。
导出
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
导入
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
丢弃那些 #
。
当我手动 Save as
一个 CSV 文件时,Excel 不会将那些 #
放在日期周围。相反,它会按照我的区域设置中定义的日期来写日期(对我来说是 dd/mm/yyyy
)。
并且在导入回来时,记录器使用 array(...4...) 作为日期列,并且导入正确。
无论如何,如果您在国际环境中工作,CSV 真的 很糟糕,因为它根据本地机器设置表现不同。由于定义不明确,这是我最后使用的格式。
尝试输出 .text 而不是 .value。
改变这个:
cellValue = writeRange.Cells(i, j).value
对此:
cellValue = writeRange.Cells(i, j).Text
问题
我有一个 VBA 程序可以将数据从一个 Excel 文件导出到 CSV 文件中。当遇到日期时,它会将其格式化为 #2016-06-14#
。我假设井号(或 octothorpe 或井号或井号)是为了表明该字段是一个日期字段。但是,当我将 CSV 导入回另一个工作簿时,无论我如何格式化该字段,日期都不会出现。它仍然包含 #
个字符。
问题
如何获取要导入为 YMD 格式日期的日期列?
附录
这是我用来导出和导入的一些代码,供参考。
导出
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
导入
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
丢弃那些 #
。
当我手动 Save as
一个 CSV 文件时,Excel 不会将那些 #
放在日期周围。相反,它会按照我的区域设置中定义的日期来写日期(对我来说是 dd/mm/yyyy
)。
并且在导入回来时,记录器使用 array(...4...) 作为日期列,并且导入正确。
无论如何,如果您在国际环境中工作,CSV 真的 很糟糕,因为它根据本地机器设置表现不同。由于定义不明确,这是我最后使用的格式。
尝试输出 .text 而不是 .value。
改变这个:
cellValue = writeRange.Cells(i, j).value
对此:
cellValue = writeRange.Cells(i, j).Text