VBA复制和粘贴错误

时间:2012-06-15 23:37:48

标签: excel vba excel-vba

这是一个微不足道的错误,但我无法弄清楚。我有一个复制和粘贴几个项目的vba脚本。它适用于除了最后一个之外的所有。错误是

  

无法粘贴信息,因为复制区域和粘贴   面积大小和形状不一样。请尝试以下方法之一:

     
      
  1. 单击一个单元格,然后粘贴。 •
  2.   
  3. 选择一个大小和形状相同的矩形,然后粘贴。
  4.   

以下是代码:

Sub MakePropertyPage(propertyNum, aFile)
Dim sheetName As String
sheetName = "Prop " & propertyNum

'OPENS INDIVIDUAL WORKBOOKS TO PULL IN INPUT INFORMATION
Workbooks.Open Filename:=vpath & ThisWorkbook.Path & "\" & aFile & ".xlsb",                 UpdateLinks:=False, ReadOnly:=True

Dim sourceRangeAsString As String, dest As Range, clearArea As Range
Dim sheetToEdit As Worksheet
Set sheetToEdit = ThisWorkbook.Sheets(sheetName)

Set clearArea = sheetToEdit.Range("AL100:CC900")
clearArea.Delete

sourceRangeAsString = "RentRoll"
Set dest = sheetToEdit.Range("AL100")
Call CopyAndPaste(Workbooks(aFile & ".xlsb"), sourceRangeAsString, dest)

sourceRangeAsString = "Underwriting"
Set dest = sheetToEdit.Range("AL300")
Call CopyAndPaste(Workbooks(aFile & ".xlsb"), sourceRangeAsString, dest)

sourceRangeAsString = "Projections"
Set dest = sheetToEdit.Range("AL400")
Call CopyAndPaste(Workbooks(aFile & ".xlsb"), sourceRangeAsString, dest)

sourceRangeAsString = "RolloverCalculations"
dest = sheetToEdit.Range("AL500")
Call CopyAndPaste(Workbooks(aFile & ".xlsb"), sourceRangeAsString, dest)



'Close the workbook now that we are done with it
Workbooks(aFile & ".xlsb").Close savechanges:=False
End Sub

Sub CopyAndPaste(sourceFile, sourceRangeAsString, dest)
    Set source = sourceFile.Names(sourceRangeAsString).RefersToRange
    source.Copy
    dest.PasteSpecial (xlPasteValues)
    source.Copy
    dest.PasteSpecial (xlPasteFormats)
End Sub

1 个答案:

答案 0 :(得分:3)

你错过了Set

dest = sheetToEdit.Range("AL500")