繁体   English   中英

在Excel 2013 VBA中粘贴错误

[英]Pasting errors in Excel 2013 VBA

我有一个VBA脚本,该脚本结合了许多Excel文件中的数据并巧妙地呈现了结果。 通过打开输入文件,复制所需范围的数据并将其重复粘贴到结果文件中来完成此操作。

我们刚刚升级到Office 2013,并且某些粘贴到了错误的位置,例如:

  Workbooks(currentBook).Sheets("InputList").Range("E1:F1000").Copy
  ThisWorkbook.Sheets("Results").Range("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

不是粘贴到单元格B2,而是粘贴到J1。

另一个复制粘贴操作应提供:

Location    Date    Value
Location    Date    Value
Location    Date    Value

通过执行以下代码:

Workbooks(currentBook).Sheets("Pay").Range("B1:B2").Copy
ThisWorkbook.Sheets("Problem Sheets").Range("E1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
ThisWorkbook.Sheets("Problem Sheets").Range("E1:E2").Copy
ThisWorkbook.Sheets("Problem Sheets").Range("A" & problemCell).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=True
ThisWorkbook.Sheets("Problem Sheets").Range("E1:E2").ClearContents
problemCell = problemCell + 1

而是以:

Location    Location   Value
Location    Location   Value
Blank       Blank      Value

我非常感谢您对理解和处理此行为的任何帮助-我需要能够信任此文件中的结果,在Office 2010中,我可以!

更换:

ThisWorkbook.Sheets("Results").Cells("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

ThisWorkbook.Sheets("Results").Range("B2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats

等等

可以通过遍历所需范围,将每个目标单元格的值分配为等于原始单元格的值来解决此问题,如下所示:

For copyCount = 1 To 1000
    ThisWorkbook.Sheets("Pay").Cells(copyCount + 1, 2).Value = _
        Workbooks(currentBook).Sheets("Employee List").Cells(copyCount, 5).Value
    ThisWorkbook.Sheets("Pay").Cells(copyCount + 1, 3).Value = _
        Workbooks(currentBook).Sheets("Employee List").Cells(copyCount, 6).Value
    ThisWorkbook.Sheets("Pay").Cells(copyCount + 1, 1).Value = _
        Workbooks(currentBook).Sheets("Employee List").Cells(copyCount, 7).Value
Next

尽管这可以解决复制和粘贴功能中遇到的问题,但它不能解释错误的根源,因此我仍然对与此有关的任何答案都感兴趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM