VBA Range.PasteSpecial xlPasteValues 有问题

Trouble with VBA Range.PasteSpecial xlPasteValues

提前感谢您的支持!

我在将数据从 CSV 文件复制和粘贴到 excel 中的 table 时遇到问题。

我有一个打开 CSV 文件的宏,读取第一列中的所有 ID,并将其与主工作簿中的 ID table 进行比较。然后宏识别新 ID 并将相关数据复制到主工作簿中,将行添加到 table.

的底部

我的问题是,在 CSV 文件中,日期列已预先格式化为 29/03/2020(英国格式),但是当数据粘贴到主工作簿中的 table 时,日期尽可能以美国格式粘贴。

有没有一种方法可以强制粘贴操作将所有数据视为文本,然后我可以对其进行格式化?

Sub LookupIDandPaste(rg As Range, rgCompare As Range, dict As Dictionary, resultType As eResultType, OpenBook As Workbook)
'lookup row number in rgCompare of transaction ID's not already listed in rgBase (identified in sub MainCompare)

    Dim key As Variant
    Dim rowNum As Long
    Dim newRow As Range
    Dim newTableRowNum As Long
    Dim newTableCellRange As Range
    'Dim monzoTransFirstCol As String
    'Set monzoTransFirstCol

        For Each key In dict

        rowNum = Application.Match(key, rgCompare, 0) + 1 'row number in rgCompare of new item (plus 1 to account for header row)
        Set newRow = OpenBook.Sheets(1).Range("A" & rowNum & ":P" & rowNum)

        ThisWorkbook.Sheets("Transactions (Monzo)").ListObjects("MonzoTransactions").ListRows.Add AlwaysInsert:=True

        newTableRowNum = ThisWorkbook.Sheets("Transactions (Monzo)").ListObjects("MonzoTransactions").ListRows.Count + 2
        Set newTableCellRange = ThisWorkbook.Sheets("Transactions (Monzo)").Range("Q" & newTableRowNum) 'cell in table

        newRow.Copy
        newTableCellRange.PasteSpecial xlPasteValues 'paste operation

    Next key

End Sub

尝试,请更换:

newRow.Copy
newTableCellRange.PasteSpecial xlPasteValues

With newTableCellRange.Resize(newRow.Rows.Count, newRow.Columns.Count)
      .NumberFormat = "@"
      .value = newRow.value
End With