从Excel导出到txt保存特殊的date格式

我一直在使用一个代码来将数据从excel复制到.txt中去除任何不需要的字符,input到另一个应用程序:

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的date,当我应用上面的代码时,我得到格式为dd/mm/yyyy

有什么办法可以保存从Excel到原始date格式.txt

您的帮助是高度赞赏!

这是你正在尝试?

 '~~> 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 

改变这一行

  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 

我不是很清楚哪个部分用date声明variables,但是这个解释可能是有帮助的:

 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