【问题标题】:Export from Excel to txt preserving especial date format从 Excel 导出到保留特殊日期格式的 txt
【发布时间】:2013-01-05 20:18:45
【问题描述】:

我一直在使用代码将数据从 excel 复制到 .txt,删除任何不需要的字符,以便输入到另一个应用程序:

Public DirRoot As String
Public CasNam As String

Public Sub Export()

    Dim intUnit As Integer
    Dim rngRow As Range
    Dim rngCell As Range
    Dim strText As String

    intUnit = FreeFile

    DirRoot = "C:\temp\"
    CasNam = "Test"

        Open DirRoot & CasNam & ".txt" For Output As intUnit

    'End If

    With Worksheets("Export").UsedRange
        For Each rngRow In .Rows
            strText = ""
            For Each rngCell In rngRow.Cells
                If Len(rngCell.Value) = 0 Then Exit For
                strText = strText & " " & rngCell.Value
            Next
            Print #intUnit, Trim(strText)
        Next
    End With

    Close intUnit

End Sub

现在我需要在一组不同的数据下使用它,其中一列是格式为:yyyymmdd 的日期,当我应用上述代码时,我得到格式为 dd/mm/yyyy 的列。

有什么方法可以将原始日期格式从 excel 保留为 .txt

非常感谢您的帮助!

【问题讨论】:

    标签: excel file date text format


    【解决方案1】:

    这是你正在尝试的吗?

    '~~> Change this to the column which has dates
    Col = 2
    
    For Each rngCell In rngRow.Cells
        If Len(rngCell.Value) = 0 Then Exit For
    
        If rngCell.Column = Col Then
            strText = strText & " " & Format(rngCell.Value, "yyyymmdd")
        Else
            strText = strText & " " & rngCell.Value
        End If
    Next
    

    【讨论】:

      【解决方案2】:

      改变这一行

            strText = strText & " " & rngCell.Value
      

            strText = strText & " " &  Format(rngCell.Value,"yyyymmdd")
      

      祝你好运

      编辑

      请检查一下。 colCounter是标识你的格式请求列。

      Dim colCounter as Integer
      colCounter = 1
      With Worksheets("Export").UsedRange
         For Each rngRow In .Rows
            strText = ""
            colCounter = 1
            For Each rngCell In rngRow.Cells
              If Len(rngCell.Value) = 0 Then Exit For
              If colCounter = 2  Then ' change 2 to whatever column you want to format
                 strText = strText & " " &  Format(rngCell.Value,"yyyymmdd")
              Else
                 strText = strText & " " & rngCell.Value
              End if
              colCounter = colCounter + 1
            Next
            Print #intUnit, Trim(strText)
         Next
      End With
      

      【讨论】:

      • 它可以工作,但现在它将该格式(yyyymmdd)应用于所有单元格,我只需要以某种方式将它应用于一个特定的列。我会尝试弄清楚,但检查列号的 IF 条件可能会起作用!
      【解决方案3】:

      我不太清楚哪一部分声明了带有日期的变量,但这种解释可能会有所帮助:

      Sub Date_Convert 
      
      dim dtDate_Orig as date
      dim dtDate_Convert as date
      dim sDate as String
      
      
      dtDate_Orig = "22/01/2012"
      dtDate_Convert = format(dtDate_Orig, "yyyymmdd")
      sDate = Cstr(dtDate_Convert)
      
      'Now you can use the string for a filename 
      
      End Sub
      

      【讨论】:

      • 男士们,感谢您提供快速而坚定的答案。最后,我添加了一个 n IF 子句来测试当前单元格在第 2 列(带有日期的那个)中的位置。谢谢大家!
      猜你喜欢
      • 1970-01-01
      • 2022-12-20
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多