如何从Sheet 1复制所有值并粘贴到Sheet 2的其余部分

时间:2013-12-21 11:04:28

标签: vba excel-vba excel

有人可以告诉我如何从sheet1复制值并将其粘贴到剩余的现有值中吗? 我有两张名为“DTMGIS”和“DTMFinal”的表,它们具有完全相同的结构,但我需要将“DTMGIS”中的值添加到(我的意思是在Last Row之后)“DTMFinal”?

我已经从这个Post的Soren获得了这个代码,这对我来说是一个空白的复制品,但是为了将数据附加到现有的我认为我需要更多的东西

Sub CopyPasteValues()
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set ws1 = ThisWorkbook.Sheets("DTMGIS")
    Set ws2 = ThisWorkbook.Sheets("DTMEdit")

    ws1.Range(ws1.UsedRange.Address).Copy
    ws2.Range("a1").PasteSpecial xlPasteValues
End Sub

3 个答案:

答案 0 :(得分:0)

试试这个

Sub CopyPasteValues()
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rng1 As Range, rng2 As Range

    Set ws1 = ThisWorkbook.Sheets("DTMGIS")
    Set ws2 = ThisWorkbook.Sheets("DTMEdit")

    With ws1
        Set rng1 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
        Range(.Cells(1, 1), rng1).EntireRow.Copy
    End With
    With ws2
        Set rng2 = .Cells.Find("*", .Cells(1, 1), xlValues, xlWhole, xlByRows, xlPrevious)
        If rng2 Is Nothing Then
            .Cells(1, 1).PasteSpecial xlPasteValues
        Else
            rng2.EntireRow.Cells(2, 1).PasteSpecial xlPasteValues
        End If
    End With
End Sub

已更新以避免UsedRange

答案 1 :(得分:0)

这是您的问题的一般解决方案,如果您可以提供一些要复制粘贴的样本数据,Soluton可以更加具体化

一般解决方案如下:

    Sub CopyPaste()

'Copying Data
Sheets("DTMGIS").Activate
Range("A1").Select
'    DataStart = Selection.Address
'    Selection.End(xlToRight).Select
'    Selection.End(xlDown).Select
'    DataEnd = Selection.Address
'    Range(DataStart, DataEnd).Select
'    Uncomment The Above 5 lines if u have Multiple columns of data
'    and Comment the Below line.
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy


' Pasting data
Sheets("DTMFinal").Activate
If WorksheetFunction.CountA(Cells) = 0 Then     'Checking If Sheet has no data
    Range("A1").Select
    ActiveCell.PasteSpecial
Else
    Range("A1").Select
    Selection.End(xlDown).Select
    ActiveCell.Offset(1, 0).Select
    ActiveCell.PasteSpecial
End If


End Sub

随意询问代码中的任何修改。由于没有样本数据我做了一些假设..

答案 2 :(得分:-1)

希望这有帮助。

Sub CopyPasteValues()
    Dim ws1 As Worksheet

    Set ws1 = ThisWorkbook.Sheets(1)

    ws1.Range(ws1.UsedRange.Address).Copy
    ws1.UsedRange.Cells.Offset(ws1.UsedRange.Rows.Count, 0).Select
    Selection.PasteSpecial xlPasteValues
End Sub