Excel VBA – 使用RESIZE“粘贴” – 需要粘贴特殊(科学记数法)

我正在使用下面的代码,这是基于SO上的其他人的post。 它打开另一个工作簿,复制工作表并将其粘贴回主工作簿。 它正在使用resize函数“粘贴”这些值,所以pastespecial不可用。 问题是源表单的S列中有20位以上的数字(跟踪数字),当它们被粘贴回主工作簿时,它们被截断成科学记数法并丢失。

Set SourceFile = Workbooks.Open("C:\users\user\desktop\shipping\tempshipping.xls") Set DestFile = ThisWorkbook With SourceFile.Sheets("Sheet1").UsedRange 'I tried using this to set the format of the cells in the source workbook 'to "Text" to fix the problem but I stepped through the code and actually 'checked the file when it's open and it's not doing anything LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Range("S2:" & "S" & LastRow).NumberFormat = Text 'you can see here the "paste" method is taking the range value and resizing 'it to the source data, so pastespecial isn't available. I'm not even sure 'doing pastespecial would fix the problem. 'Now, paste to y worksheet: DestFile.Sheets("DestSheet").Range("A1").Resize( _ .Rows.Count, .Columns.Count) = .Value End With 'Close TEMP File SourceFile.Close 

文本的Range.NumberFormat属性是@ ,而不是文本

  Dim lastRow As Long, cpyrng As Range Dim SourceFile As Workbook, DestFile As Workbook Set SourceFile = Workbooks.Open("C:\users\user\desktop\shipping\tempshipping.xls") Set DestFile = ThisWorkbook With SourceFile.Sheets("Sheet1").UsedRange lastRow = .Cells.Find(What:="*", After:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Set cpyrng = .Range("S2:S" & lastRow) cpyrng.NumberFormat = "@" '<~~ Use @; Not Text End With 'transfer values to destination worksheet With DestFile.Sheets("MobStub").Range("A1").Resize(cpyrng.Rows.Count, cpyrng.Columns.Count) .NumberFormat = "@" '<~~ set the receiving number format first .Value = cpyrng.Value End With 'Close TEMP File SourceFile.Close 

你错过了一些. With … End With语句中的引用。 的. in .Cells显示With … End With中引用的父工作表。