Excel VBA将列复制到现有工作簿

时间:2012-03-29 15:54:51

标签: excel-vba vba excel

我有工作簿, source.xlsm ,工作表“test1”列A6:A20 我需要复制到位于我的C上的另一个工作簿:...已命名 dest.xlsx 工作表“资产”列“我”。我需要能够复制数据并能够添加到列而不会覆盖以前复制的数据。任何帮助都可以节省生命。

Sub Align()
    Dim TargetSh As String

    TargetSh = "Assets"

    For Each WSheet In Application.Worksheets

        If WSheet.Name <> TargetSh Then
            WSheet.Select
            Range("A6:A20").Select
            Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
            Selection.Copy
            Sheets(TargetSh).Select
            lastRow = Range("I65532").End(xlUp).Row
            Cells(lastRow + 1, 1).Select
            ActiveSheet.Paste
        End If
    Next WSheet
End Sub

1 个答案:

答案 0 :(得分:1)

这是你在尝试什么?我有没有经过测试它,但我认为它应该可行。如果您有任何错误,请告诉我。

Sub Sample_Copy()
    Dim wb As Workbook, wbTemp As Workbook
    Dim ws As Worksheet, wsTemp As Worksheet
    Dim lastRow As Long

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("test1")

    '~~> Change path as applicable
    Set wbTemp = Workbooks.Open("C:\dest.xlsx")
    Set wsTemp = wbTemp.Sheets("Assets")

    lastRow = wsTemp.Range("I" & Rows.Count).End(xlUp).Row + 1

    ws.Range("A6:A20").Copy wsTemp.Range("I" & lastRow)

    Application.CutCopyMode = False

    '~~> Cleanup
    wbTemp.Close savechanges:=True
    Set wb = Nothing: Set wbTemp = Nothing
    Set ws = Nothing: Set wsTemp = Nothing
End Sub

HTH

西特