简体   繁体   English

在Excel 2013 VBA中粘贴错误

[英]Pasting errors in Excel 2013 VBA

I have a VBA script that combines data from a bunch of Excel files and presents the results neatly. 我有一个VBA脚本,该脚本结合了许多Excel文件中的数据并巧妙地呈现了结果。 It does this by opening an input file, copying the required range of data, and pasting it into the results file, repeatedly. 通过打开输入文件,复制所需范围的数据并将其重复粘贴到结果文件中来完成此操作。

We just upgraded to Office 2013, and some of the pastes are going to the wrong locations, for example: 我们刚刚升级到Office 2013,并且某些粘贴到了错误的位置,例如:

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

is not pasting to cell B2 but rather to J1. 不是粘贴到单元格B2,而是粘贴到J1。

Another copy-paste operation which should give: 另一个复制粘贴操作应提供:

Location    Date    Value
Location    Date    Value
Location    Date    Value

by executing this code: 通过执行以下代码:

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

is instead ending up with: 而是以:

Location    Location   Value
Location    Location   Value
Blank       Blank      Value

I would really appreciate any assistance with understanding and dealing with this behaviour - I need to be able to trust the results in this file, and in Office 2010 I could! 我非常感谢您对理解和处理此行为的任何帮助-我需要能够信任此文件中的结果,在Office 2010中,我可以!

Replace: 更换:

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

with

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

etc. 等等

This issue can be corrected by looping through the required range, assigning the value of each destination cell to be equal to the value of the origin cell, like so: 可以通过遍历所需范围,将每个目标单元格的值分配为等于原始单元格的值来解决此问题,如下所示:

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

While this corrects the encountered issue with copying and pasting functionality, it does not explain the origin of the error, so I am still interested in any answers relating to that. 尽管这可以解决复制和粘贴功能中遇到的问题,但它不能解释错误的根源,因此我仍然对与此有关的任何答案都感兴趣。

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

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