感谢您的任何提前支持!
我在将数据从CSV文件复制和粘贴到Excel中的表时遇到问题。
我有一个宏,它打开CSV文件,读取第一列中的所有ID,并将其与主工作簿中的ID表进行比较。然后,宏将标识新的ID,并将相关数据复制到主工作簿中,并在表的底部添加行。
[我的问题是,在CSV文件中,日期列已预格式化为29/03/2020(英国格式),但是,当数据粘贴到主工作簿的表格中时,日期将以美国格式粘贴,其中可能。
有没有一种方法可以强制粘贴操作将所有数据视为文本,然后可以对其进行格式化?
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
With newTableCellRange.Resize(newRow.Rows.Count, newRow.Columns.Count)
.NumberFormat = "@"
.value = newRow.value
End With