Forum Discussion

Pavol_Goralka's avatar
Pavol_Goralka
Copper Contributor
Apr 28, 2021
Solved

Taking one column from many excel files and putting them into one next to each other?

I have many excels (like more than 100), which all have different data but are all the same excel (same structure). Is it possible, if I want one particular column of data from each of the excels, an...
  • HansVogelaar's avatar
    Apr 28, 2021

    Pavol_Goralka 

    I'll assume that the workbooks are all in the same folder, and that this folder does not contain other workbooks that you do not want to process. Run the following macro:

    Sub CombineColumns()
        Const Col = "M" ' column to copy
        Dim strPath As String
        Dim strFile As String
        Dim c As Long
        Dim wbkS As Workbook
        Dim wshS As Worksheet
        Dim wbkT As Workbook
        Dim wshT As Worksheet
        With Application.FileDialog(4) ' msoFileDialogFolderPicker
            .Title = "Please select the folder with the workbooks"
            If .Show Then
                strPath = .SelectedItems(1)
                If Right(strPath, 1) <> "\" Then
                    strPath = strPath & "\"
                End If
            Else
                Beep
                Exit Sub
            End If
        End With
        Application.ScreenUpdating = False
        Set wbkT = Workbooks.Add(xlWBATWorksheet)
        Set wshT = wbkT.Worksheets(1)
        strFile = Dir(strPath & "*.xls*")
        Do While strFile <> ""
            Set wbkS = Workbooks.Open(Filename:=strPath & strFile)
            Set wshS = wbkS.Worksheets(1)
            c = c + 1
            wshS.Columns(Col).Copy Destination:=wshT.Columns(c)
            wbkS.Close SaveChanges:=False
            strFile = Dir
        Loop
        Application.CutCopyMode = False
        Application.ScreenUpdating = True
    End Sub

Resources