VBA,将多个工作簿中的列复制到主表,

时间:2017-07-21 18:54:58

标签: excel vba excel-vba

无法运行此代码。我无法从多个工作簿中复制多个列并将它们编译到我的工作表中。我循环的每个文件中的列将具有相同的名称或相同的顺序。当我运行此代码时,我没有得到任何结果或错误。

       Option Explicit
Dim wsMaster As Workbook, csvFiles As Workbook
Dim Filename As String
Dim File As Integer
Dim r As Long

Public Sub Consolidate()

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    With Application.FileDialog(msoFileDialogOpen)
        .AllowMultiSelect = True
        .Title = "Select files to process"
        .Show

        If .SelectedItems.Count = 0 Then Exit Sub

        Set wsMaster = ActiveWorkbook

        For File = 1 To .SelectedItems.Count

            Filename = .SelectedItems.Item(File)

           If Right(Filename, 4) = ".csv" Then
    Set csvFiles = Workbooks.Open(Filename, 0, True)
    r = wsMaster.Sheets("Sheet1").UsedRange.Rows.Count
    csvFiles.Sheets(1).Columns("col name").Copy Destination:=wsMaster.Sheets("Sheet1").Range("A" & r).Offset(1, 0)
    csvFiles.Close SaveChanges:=False 'close without saving
            End If


        Next File 'go to the next file and repeat the process

    End With

    Set wsMaster = Nothing
    Set csvFiles = Nothing

    With Application
        .ScreenUpdating = True
        .EnableEvents = True

    End With

End Sub

0 个答案:

没有答案