将 excel 列转换为文本时出现日期问题

date issue while converting a excel column to text

我有一个 excel 文件,其中一列将包含数字、字符串、日期等数据。我想使用 VBA.

将该单列转换为文本

代码:(我在这里尝试将A列转换为文本)

Sub Datatotext()
    Dim xWs As Worksheet
    For Each xWs In Application.ActiveWorkbook.Worksheets
        xWs.Activate
        Columns("A:A").Select
        Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 2), TrailingMinusNumbers:=True
        Next
    ActiveWorkbook.Save
End Sub

问题: 在 运行 上面的代码之后,A 列被转换为文本,但是对于 Date 值,月份和日期部分被交换并且零被删除。 示例:29/03/2033 ---转换为--> 3/29/2033。而其他数据(如字符串、数字等)则毫无问题地存储为文本。请帮我解决一下这个。我想要与文本相同的数据 eg:29/03/2033 --> 29/03/2033

输入文件: input file

input file data after converting to text

首先,请务必小心您的代码,它会更改工作簿中每个 sheet 的 A 列。

试试这个代码,它会将文本中列的每个单元格转换为文本,然后将列类型转换为文本。

Dim rngColumn As Range
Dim cel As Variant
Dim usdRng As Long
Dim arr() As String
Dim count As Long
Dim xWs As Worksheet
' It's looping through the worksheets

Application.Calculation = xlCalculationManual
For Each xWs In Application.ActiveWorkbook.Worksheets
    'get last used row number
    usdRng = xWs.UsedRange.Rows.count
    Set rngColumn = xWs.Range("A1:A" & CStr(usdRng))
    
    ReDim arr(1 To usdRng)
    
    xWs.Range("A:A").Columns.AutoFit

    count = 1
    For Each cel In rngColumn
        arr(count) = cel.Text
        count = count + 1
    Next
    xWs.Range("A:A").NumberFormat = "@"
    xWs.Range("A1:A" & CStr(count-1)).Value = Application.Transpose(arr)
    
Next

Application.Calculation = xlCalculationAutomatic
ActiveWorkbook.Save