VBA从工作簿复制到主工作簿 - 错误9更新

时间:2016-12-11 22:24:46

标签: excel vba excel-vba

我正在尝试将数据从一个工作簿复制到另一个工作簿。但我得到错误9 - 下标超出范围。

public class A {
    private final ClassB b;

    public A(ClassB b) {
        this.b = b;
    }

    public void abc(){
        b.getData();
    }

    public void xyz( boolean callabc){
        if (callabc) {
            abc();
        }
    }
}

使用此代码,(错误是错误的工作表名称(它有名称Sheet1111(Baza)),现在它只复制目录中最后一个文件的数据,我想复制所有文件中的数据。

1 个答案:

答案 0 :(得分:0)

以下代码适用于我:

Option Explicit
Sub Test()
    Application.ScreenUpdating = False ' Reduce screen "flickering"
    Dim FolderPath As String, Filepath As String, Filename As String
    Dim lastrow As Long, lastcolumn As Long
    Dim wb As Workbook
    Dim ws As Worksheet

    Set ws = Worksheets("Baza")

    FolderPath = "F:\Test\"
    Filepath = FolderPath & "*.xlsm"        
    Filename = Dir(Filepath)        
    Do While Filename <> ""
        'Provide status message showing where we are up to
        Application.StatusBar = "Processing " & Filename
        DoEvents

        Set wb = Workbooks.Open(FolderPath & Filename)
        With ActiveSheet ' this isn't strictly necessary, but ensures that we can
                         ' qualify all sheet references to a consistent sheet without
                         ' any surprises
            lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
            lastcolumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
            .Range(.Cells(2, 1), .Cells(lastrow, lastcolumn)).Copy _
                      ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0)
        End With
        Application.DisplayAlerts = False
        wb.Close
        Application.DisplayAlerts = True

        Filename = Dir
    Loop        
    Application.ScreenUpdating = True
    Application.StatusBar = False
End Sub