简体   繁体   中英

VBA Excel for Mac : Copy Paste

A VBA macro, working in Mac Excel 2011, now yields strange results in Mac Excel 2016. Specifically in the macro below:

  • Step 1 - opens a "portfolio", copy a range of data and close it
  • Step 2 - activates another already opened Workbook and pastes the data there.

But now, instead of data transferred in each cell, I get pasted an image with inside it all the copied data. I don't know if this comes from the copy part or the paste part.

'Step 1'
        Set WbPort(n) = Workbooks.Open(Portfolio(n))
        Sheets(1).Select
        Range("A2:O101").Copy
        Application.CutCopyMode = False
        WbPort(n).Close SaveChanges:=False

'Step 2'
        WbTarget.Activate
        Sheets(1).Select
        Range("C" & Range("AN" & n + 2)).PasteSpecial xlPasteValues

Untested.

Since you're copy-pasting values only, it makes sense to skip the clipboard and just assign the values directly to the destination range.

Set WbPort(n) = Workbooks.Open(Portfolio(n))

Dim rangeToCopy as range
Set rangeToCopy = WbPort(n).Worksheets(1).Range("A2:O101")

With WbTarget.Worksheets(1)
.Range("C" & .Range("AN" & n + 2).value2).resize(rangeToCopy.rows.count, rangeToCopy.columns.count).value2 = rangeToCopy.value2
End with

WbPort(n).Close SaveChanges:=False

If that works for you but you still want to fix your existing code, might be worth trying:

Set WbPort(n) = Workbooks.Open(Portfolio(n))
WbPort(n).Worksheets(1).Range("A2:O101").Copy

With WbTarget.Worksheets(1)
.Range("C" & .Range("AN" & n + 2).value2).PasteSpecial xlPasteValues
End with

Application.CutCopyMode = False
WbPort(n).Close SaveChanges:=False

Or instead of xlPasteValues try another xlPasteType ( https://docs.microsoft.com/en-us/office/vba/api/excel.xlpastetype ) eg xlPasteAll just to see if you still get an image.

Also, you seem to be pasting to a row number determined by the value in Range("AN" & n + 2) of the first sheet of WbTarget -- where n is some variable (in a loop maybe?). I would assign the value in Range("AN" & n + 2).value2 to a long type variable and check that you're pasting to the correct range/cell address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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