【问题标题】:Stop VBA from changing text to date when copy/paste复制/粘贴时停止 VBA 将文本更改为日期
【发布时间】:2021-10-08 00:57:16
【问题描述】:

我想将一些文本从一张纸复制到另一张纸上。例如: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

【问题讨论】:

  • "一些文本从一个工作表到另一个工作表。例如:01/02/2021"。它看起来像日期,而不是文本。如果是文本,它看起来像'01/02/2021(公式字符串中第一个字符之前的撇号)
  • 当我查看原始单元格时,它的格式是“常规”,而不是日期。
  • VBA 不会将文本更改为最新!您看到的是 Excel “试图提供帮助”:) 使用单元格格式。
  • @АлексейР 当我查看原始单元格时,它的格式是“常规”,而不是日期。当我将Vartype 应用于该单元格时,它也显示为 8。
  • @Gene 感谢您的评论。我看到的是它确实将文本更改为日期。原始数据的格式不是很好,01/02/2021 是 2 月 1 日,而不是 1 月 2 日。这就是为什么我需要将它放在字符串中一次并手动将其转换为日期。

标签: vba date text copy paste


【解决方案1】:

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

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

已编辑

你什么都没说……

所以,我编写了一个返回整个表格的代码(在活动工作表中)。请测试一下。只需几秒钟:

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

【讨论】:

    猜你喜欢
    • 2021-07-28
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多