0

I have a VBA script that combines data from a bunch of Excel files and presents the results neatly. 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:

  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.

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!

2 Answers 2

1

Replace:

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

with

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

etc.

1
  • Ah, not sure how that crept in. You are right about this being wrong, but the problem persists after this change has been made.
    – Guy F-W
    Jan 10, 2014 at 13:16
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.