2017-05-11 187 views
0

我有這段代碼,但我需要它複製粘貼數據作爲值。我知道它可能很簡單,但我沒有使用.Paste函數,所以我不知道如何轉換它。我處於非常初級的水平。提前謝謝了!複製粘貼爲值Excel VBA

Sub Movetabletototal() 
    Dim Count As Integer 
    Dim copyRng As Range, pasteRng As Range 
    Dim totalWS As Worksheet, mixerWS As Worksheet 

    Set totalWS = Worksheets("TOTAL") 
    Set mixerWS = Worksheets("MIXER TOTAL") 

    Set copyRng = mixerWS.Range("P3:Q" & mixerWS.Cells(mixerWS.Rows.Count, 17).End(xlUp).Row) 

    Dim newRow As Long 
    newRow = totalWS.Cells(totalWS.Rows.Count, 1).End(xlUp).Row 
    If newRow > 1 Then newRow = newRow + 1 

    copyRng.Copy totalWS.Range(totalWS.Cells(newRow, 1), totalWS.Cells(newRow + copyRng.Rows.Count, copyRng.Columns.Count)) 
End Sub 
+1

讀'PasteSpecial'方法。 – Masoud

回答

1

你需要在你的代碼中使用PasteSpecial。最後一行改成這樣:

'. 
'. Your code will be the same till here 
'. 

copyRng.Copy 

Set pasteRng = totalWS.Range(totalWS.Cells(newRow, 1), _ 
totalWS.Cells(newRow + copyRng.Rows.Count, copyRng.Columns.Count)) 

pasteRng.PasteSpecial xlPasteValues 

End Sub 

或者你可以使用值這樣設置(設置像上面的範圍):如果你只在感興趣

pasteRng.Value = copyRng.Value 
+1

如何使用.value = .value代替複製粘貼? – Vinnie

+0

@Vinnie OP詢問使用粘貼功能。我個人更喜歡這種方法(請查看我的最後一個問題和我自己的答案)。 – Masoud

1

沒有理由使用Copy,PastePasteSpecial。相反,只是使用直接賦值:

Dim destinationRange as Range 
With totalWs 
    Set destinationRange = .Cells(newRow, 1).Resize(copyRange.Rows.Count, copyRange.Columns.Count) 
End With 

destinationRange.Value = copyRange.Value 
0

您也可以嘗試

.PASTESPECIAL(-4163)

+0

xlPasteValues = -4163 - > https://msdn.microsoft.com/en-us/library/office/ff837425.aspx – Masoud