2012-06-15 181 views
0

這是一個微不足道的錯誤,但我無法弄清楚。我有一個vba腳本複製並粘貼了一些項目。它適用於所有,但最後。錯誤是VBA複製並粘貼錯誤

信息無法粘貼,因爲複製區域和粘貼區域的大小和形狀不一樣。請嘗試執行以下操作之一:

  1. 單擊單個單元格,然後粘貼。 •
  2. 選擇尺寸和形狀相同的矩形,然後粘貼。

下面是代碼:

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 

回答

3

你缺少一個Set這裏:

dest = sheetToEdit.Range("AL500")