简体   繁体   中英

ActiveSheet.Range().PasteSpecial not working

I'm trying to paste values using the activesheet. The code keeps throwing out the following error:

"To do this, all the merged cells need to be the same size"

But none of the cells I'm copying are merged. The error comes from the line: ActiveSheet.Range("C23").PasteSpecial...

Dim FM As Worksheet: Set FM = ThisWorkbook.Sheets("Closings Template")

fmPath = "G:\Finance Department\Banking Dashboard\"
FmFile = "Testing_Testing.xlsm"
fmRef = fmPath & FmFile
ToPath = "G:\Budgets and Financial\CLT Budget Templates\"
ToFile = "Belle Grove Manor.xlsx"
ToRef = ToPath & ToFile
CIWPath = "H:\02-CHARLOTTE\Land\zLand Worksheets\"
CIWFile = "Community Information Workbook_CLT.xlsm"
CIWRef = CIWPath & CIWFile

FM.Range("O2").Copy
Workbooks.Open(ToRef).Worksheets("Sheet1").Activate
ActiveSheet.Range("C11").PasteSpecial Paste:=xlPasteValues
FM.Range("P2").Copy
ActiveSheet.Range("C17").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("C15").Value = "14"
Application.CutCopyMode = False
Workbooks.Open(CIWRef).Worksheets("BLSRG.PS").Range("N23").Copy
ActiveSheet.Range("C23").PasteSpecial Paste:=xlPasteValues
ActiveSheet.Range("C19").Copy
FM.Range("N2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

Using ActiveSheet can cause problems and setting up your variable properly will ensure errors are eliminated. Try to incorporate the simple code below.

Dim wbSrc As Workbook
Set wbSrc = Workbooks.Open(CIWRef) 'the source workbook

Dim wsSrc As Worksheet
wsSrc = wbSrc.Sheets("BLSRG.PS") 'the source worksheet

Dim wbDest As Workbook
Set wbDest = Workbooks.Open(ToRef) 'the destination workbook

Dim wsDest As Worksheet
wsDest = wbDest.Sheets("Sheet1") 'the destination worksheet

wsDest.Range("C23").Value = wsSrc.Range("N23").Value 'writes the values from the source worksheet to the destination worksheet

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