2013-12-18 35 views
0

我有一個子查找與一個表中的值的數組匹配的SolutionID列中的值,然後將其複製到另一個表中。PasteSpecial不工作

不過,我打一個錯誤與.PasteSpecial方法 -

對象不支持此屬性或方法

有誰知道我做錯了什麼?謝謝。你會發現,當然最後一行之後

SourceTable.DataBodyRange.Rows(j).Copy DestinationTable.DataBodyRange.Range("A" & CStr(lastRow)) 

Private Sub CopySolutions(ByRef SourceTable As ListObject, ByRef DestinationTable As ListObject, ByRef values() As String) 

    On Error Resume Next 

    Dim i, j As Integer ' Dummy for looping 

    '** Loop through all of the ID's to copy... *' 
    For i = LBound(values) To UBound(values) 

     With SourceTable.DataBodyRange 

      For j = 1 To .Rows.Count 

       If .Cells(j, 1).Value = values(i) Then 

        .Rows(j).Copy ' Copy the row in the SourceTable 

        Dim LastRow As Integer 

        LastRow = DestinationTable.Rows.Count ' Work out the number of rows in the DestinationTable 

        '** Check to see if the last row in the destination table is already empty '* 
        If DestinationTable.DataBodyRange.Cells(LastRow, 1).Value <> "" Or LastRow = 0 Then 
         DestinationTable.ListRows.Add AlwaysInsert:=True ' Insert a new row in to the DestinationTable 
         LastRow = LastRow + 1        ' Increment LastRow to take in to account the newly added row 
        End If 

        DestinationTable.DataBodyRange.Cells(LastRow, 1).Select  ' Select the last row, column 1 in the Destination Table 
        Selection.PasteSpecial Paste:=xlPasteValues, _ 
              Operation:=xlNone, _ 
              SkipBlanks:=False, _ 
              Transpose:=False   ' Paste the copied row 

        Exit For ' Exit the For, there is no need to keep checking for matches 

       End If 

      Next 

     End With 

    Next 

    If Err.Number <> 0 Then 
     Call ErrorOutput("An error occured while copying your selected solutions.") 
    End If 
    On Error GoTo 0 

    WS.Range("Solution").Select ' Reselect the Solution cell range 

End Sub 
+1

移動'.Rows(J).Copy'只是其中粘貼前行。 Excel有清除剪貼板的習慣 –

+0

當然,這似乎工作。謝謝。 –

回答

3

最好避免複製/粘貼產品總數:

Dim rngSrc as Range 
'... 
Set rngSrc = .Rows(j) 
'... 

DestinationTable.DataBodyRange.Cells(LastRow, 1). _ 
     Resize(1, rngSrc.Columns.Count).Value = rngSrc.Value 
+0

+ 1不能同意你:) –

+0

完美。非常感謝,它實現了我所需要的一切。 –

1

試試這個。你不必這樣使用.Select

+0

謝謝,這適用於複製,但它粘貼一切(公式等)。我只需要'values'。 –