将数据从一个工作簿传输到另一个工作簿

时间:2016-08-17 17:25:33

标签: excel vba excel-vba loops

以下脚本当前循环遍历数据列表,并根据单元格中的值将一个范围从一个工作簿复制到另一个工作簿。

For i = 3 To LastRow

If Cells(i, 1) <> "" And Cells(i, 13) = "ü" Then
Range(Cells(i, 1), Cells(i, 12)).Select
Selection.Copy

workbooks.Open Filename:=iForm
Worksheets("Statistics").Select
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

Worksheets("Statistics").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
ActiveWorkbook.Save
ActiveWorkbook.Close
Application.CutCopyMode = False
End If
Next i

而是从A:L复制范围我想复制A&amp; A列。 L,我已经尝试过更新,但是我一直在收到错误,请帮忙。 我提前谢谢你。

1 个答案:

答案 0 :(得分:0)

我想我知道你在尝试什么。我建议您为工作表添加变量,然后可以通过Range()选择整个列:

Sub t()
Dim mainWS As Worksheet
Set mainWS = ActiveWorkbook.Worksheets("Sheet1") ' Tweak as necessary, this sheet is where your main data is (the non-blank cell, and "ü") 

lastRow = mainWS.Cells(mainWS.Rows.Count,1).End(xlUp).Row 'or whatever your formula is. Make sure to reference the worksheet

For i = 3 To lastRow
    If mainWS.Cells(i, 1) <> "" And mainWS.Cells(i, 13) = "ü" Then
        mainWS.Range("A:A,L:L").Select
        Selection.Copy

        Workbooks.Open Filename:=iForm
        'Worksheets("Statistics").Select
        Set statWS = ActiveWorkbook.Worksheets("Statistics")

        erow = statWS.Cells(statWS.Rows.Count, 1).End(xlUp).Offset(1, 0).Row

        statWS.Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
        ActiveWorkbook.Save
        ActiveWorkbook.Close
        Application.CutCopyMode = False
    End If
Next i

End Sub

请注意,我离开了.Select,只是为了让它与你的OP相似。虽然你一定要关注avoid using .Select