Txt yyyy-mm-dd

时间:2019-03-18 20:48:31

标签: excel vba

我正在尝试将一个脚本拼凑在一起,以便处理许多格式化特定格式的Excel电子表格。脚本的第二部分工作正常,但首先需要帮助我将特定的列(将来的位置和位置可能会更改,因此我想在代码中明确命名列)为基于txt的yyyy-mm-dd因为代码输出了一个csv文件,但是csv文件使用了区域日期格式。还有一列,即第7列,即日期时间,我想调整时间。最后,我需要使用不包含日期列标题的单元格区域。当前,第1列会强制所有单元格读取为“ YYYY-MM-DD”,所有其他日期列在excel中均会正确读取,但在导出时会转换回区域日期。非常感谢。

    Sub CSVFile()
    Columns(1).NumberFormat = ”@”
    Columns(1).NumberFormat = "YYYY-MM-DD"
    Columns(7).NumberFormat = "YYYY-MM-DD"
    Columns(8).NumberFormat = "YYYY-MM-DD"
    Columns(9).NumberFormat = "YYYY-MM-DD"
    Columns(11).NumberFormat = "YYYY-MM-DD"
    Columns(16).NumberFormat = "YYYY-MM-DD"
    Columns(28).NumberFormat = "YYYY-MM-DD"
    Columns(29).NumberFormat = "YYYY-MM-DD"
    Columns(31).NumberFormat = "YYYY-MM-DD"
    Columns(33).NumberFormat = "YYYY-MM-DD"
    Columns(36).NumberFormat = "YYYY-MM-DD"
    Columns(39).NumberFormat = "YYYY-MM-DD"
    Columns(40).NumberFormat = "YYYY-MM-DD"
    Columns(44).NumberFormat = "YYYY-MM-DD"
    Columns(45).NumberFormat = "YYYY-MM-DD"
    Columns(48).NumberFormat = "YYYY-MM-DD"
    Columns(49).NumberFormat = "YYYY-MM-DD"
    Columns(50).NumberFormat = "YYYY-MM-DD"
Dim SrcRg As Range
Dim CurrRow As Range
Dim CurrCell As Range
Dim CurrTextStr As String
Dim ListSep As String
Dim FName As Variant
FName = Application.GetSaveAsFilename("", "CSV File (*.csv), *.csv")
ListSep = Application.International(xlListSeparator)
  If Selection.Cells.Count > 1 Then
  Set SrcRg = Selection
  Else
  Set SrcRg = ActiveSheet.UsedRange
  End If
Open FName For Output As #1
For Each CurrRow In SrcRg.Rows
  CurrTextStr = ìî
For Each CurrCell In CurrRow.Cells
  If (CurrCell.Value = “NULL” Or Len(CurrCell.Value) < 1) Then
  CurrTextStr = CurrTextStr & ListSep
  Else
  CurrTextStr = CurrTextStr & “”“” & Replace(CurrCell.Value, “”“”, “”“”“”) & “”“” & ListSep 
  End If
Next
While Right(CurrTextStr, 1) = ListSep
  CurrTextStr = Left(CurrTextStr, Len(CurrTextStr) - 1)
Wend
Print #1, CurrTextStr
Next
Close #1
End Sub

0 个答案:

没有答案