ActiveSheet.Range()。PasteSpecial不起作用

时间:2018-11-29 19:48:06

标签: excel vba excel-vba

我正在尝试使用活动表粘贴值。该代码不断抛出以下错误:

  

“为此,所有合并的单元格必须具有相同的大小”

但是我要复制的所有单元格都没有合并。错误来自以下行: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

1 个答案:

答案 0 :(得分:0)

使用ActiveSheet可能会导致问题,并且正确设置变量将确保消除错误。尝试合并下面的简单代码。

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