停止 VBA 在 copy/paste 时将文本更改为日期

Stop VBA from changing text to date when copy/paste

我想将一些文本从 sheet 复制到另一个。例如:01/02/2021。 但是 VBA 会自动将其转换为 2020/01/02。我怎样才能阻止它? 以下代码没有工作。

示例 1:

sheet_1.Range("A1:A" & sheet1.Cells(1, 1).CurrentRegion.End(xlDown).row).Copy
ws.Range("start").PasteSpecial xlPasteValues 
ws.Range("start").PasteSpecial xlPasteFormats

示例 2:

sheet_1.Range("A1:A" & sheet1.Cells(1, 1).CurrentRegion.End(xlDown).row).Copy
    ws.Range("start").PasteSpecial xlPasteFormulasAndNumberFormats

示例 3:

sheet_1.Range("A1:A" & sheet1.Cells(1, 1).CurrentRegion.End(xlDown).row).Copy
    ws.Range("start").Paste xlPaste Format:="Text" 'This causes an error

请尝试下一个代码。它将从(伪)xls 文件中提取日期并将其放在活动 sheet 的第一列中。正确格式为日期:

Sub openXLSAsTextExtractDate()
   Dim sh As Worksheet, arrTXT, arrLine, arrD, arrDate, fileToOpen As String, i As Long, k As Long
   
   Set sh = ActiveSheet 'use here the sheet you need
   fileToOpen = "xls file full name" 'use here the full name of the saved xls file
   'put the file content in an array splitting the read text by end of line (vbCrLf):
   arrTXT = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fileToOpen, 1).ReadAll, vbCrLf)
   ReDim arrDate(UBound(arrTXT))          'redim the array where the date will be kept, to have enough space for all the date values
   For i = 39 To UBound(arrTXT) - 1       'iterate between the array elements, starting from the row where date data starts
        arrLine = Split(arrTXT(i), vbTab) 'split the line by vbTab
        arrD = Split(arrLine(0), "/")     'split the first line element (the date) by "/"
        arrDate(k) = DateSerial(arrD(2), arrD(1), arrD(0)): k = k + 1 'properely format as date and fill the arrDate elements
    Next i
    ReDim Preserve arrDate(k - 1)         'keep only the array elements keeping data
    With sh.Range("A1").Resize(UBound(arrDate) + 1, 1)
        .value = Application.Transpose(arrDate)  'drop the array content
        .NumberFormat = "dd/mm/yyyy"             'format the column where the date have been dropped
    End With
End Sub

已编辑:

你什么都没说...

因此,我编写了一个返回整个 table 的代码(在活动 sheet 中)。请测试一下。只需几秒钟:

Sub openXLSAsText()
   Dim sh As Worksheet, arrTXT, arrLine, arrD, arrData, fileToOpen As String, i As Long, j As Long, k As Long
   
   Set sh = ActiveSheet 'use here the sheet you need
   fileToOpen =  "xls file full name" 'use here the full name of the saved xls file
   'put the file content in an array splitting the read text by end of line (vbCrLf):
   arrTXT = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(fileToOpen, 1).ReadAll, vbCrLf)

   ReDim arrData(1 To 10, 1 To UBound(arrTXT))  'redim the array where the date will be kept, to have enough space for all the date values
   For i = 38 To UBound(arrTXT) - 1             'iterate between the array elements, starting from the row where table header starts
        arrLine = Split(arrTXT(i), vbTab)       'split the line by vbTab
        k = k + 1                               'increment the k variable (which will become the table row)
        For j = 0 To 9
            If j = 0 And k > 1 Then
                arrD = Split(arrLine(j), "/")   'split the first line element (the date) by "/"
                arrData(j + 1, k) = DateSerial(arrD(2), arrD(1), arrD(0)) 'propperely format as date and fill the arrDate elements
            ElseIf j = 2 Or j = 3 Then
                arrData(j + 1, k) = Replace(arrLine(j), ",", ".")  'correct the format for columns 3 and four (replace comma with dot)
            Else
                 arrData(j + 1, k) = arrLine(j)                    'put the rest of the column, not processed...
            End If
        Next j
    Next i
    ReDim Preserve arrData(1 To 10, 1 To k)      'keep only the array elements with data
    With sh.Range("A1").Resize(UBound(arrData, 2), UBound(arrData))
        .value = Application.Transpose(arrData)  'drop the array content
        .EntireColumn.AutoFit                    'autofit columns
        .Columns(1).NumberFormat = "dd/mm/yyyy"  'format the first column
    End With
    MsgBox "Ready..."
End Sub