+ Reply to Thread
Results 1 to 6 of 6

Vba code to compile specific details from different excel file to one file from folder

Hybrid View

UPA Vba code to compile specific... 05-24-2017, 05:51 AM
Olly Re: Vba code to compile... 05-24-2017, 08:16 AM
UPA Re: Vba code to compile... 05-25-2017, 12:15 AM
Olly Re: Vba code to compile... 05-25-2017, 05:27 AM
UPA Re: Vba code to compile... 05-25-2017, 06:55 AM
Olly Re: Vba code to compile... 05-25-2017, 07:38 AM
  1. #1
    Registered User
    Join Date
    09-25-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    88

    Vba code to compile specific details from different excel file to one file from folder

    Hello experts,

    There are around 300 excel files and kept on my desktop on specific folder all 300 excel file having Proforma Invoice sheet, from these Proforma Invoice sheet i want to pull specific information to this excel file in sheet1.In sheet1 in column A i want Cell A10 value and In column B i want Cell A18 value and in Column C
    I want Cell E12 value and in column D i want Cell G46 value from all 300 excel files.

    Please find the attached file where i need data from different files

    Regards,
    upa
    Attached Files Attached Files

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Vba code to compile specific details from different excel file to one file from folder

    You can do this really quickly and easily with Power Query.

    The following query returns all files in your folder, as a table, and applies a function query for each column returned:
    let
        Source = Folder.Files("C:\Temp\Combine"),
        #"Get A" = Table.AddColumn(Source, "Agency", each fnColumnA([Content])),
        #"Get B" = Table.AddColumn(#"Get A", "Advertiser", each fnColumnB([Content])),
        #"Get C" = Table.AddColumn(#"Get B", "Start / End", each fnColumnC([Content])),
        #"Get D" = Table.AddColumn(#"Get C", "Net Amount", each fnColumnD([Content])),
        #"Removed Other Columns" = Table.SelectColumns(#"Get D",{"Agency", "Advertiser", "Start / End", "Net Amount"})
    in
        #"Removed Other Columns"
    The four function queries, to return your four columns are identical, apart from the cell referenced. Name them consistently with the function names used in the CombineFromFolder query.

    let GetColumnA = (wb as binary) =>
        let
            Source = Excel.Workbook(wb, null, true),
            Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
            Value = Sheet{9}[Column1]
        in
            Value
    in
        GetColumnA
    let GetColumnB = (wb as binary) =>
        let
            Source = Excel.Workbook(wb, null, true),
            Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
            Value = Sheet{17}[Column1]
        in
            Value
    in
        GetColumnB
    let GetColumnC = (wb as binary) =>
        let
            Source = Excel.Workbook(wb, null, true),
            Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
            Value = Sheet{11}[Column5]
        in
            Value
    in
        GetColumnC
    let GetColumnD = (wb as binary) =>
        let
            Source = Excel.Workbook(wb, null, true),
            Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
            Value = Sheet{45}[Column7]
        in
            Value
    in
        GetColumnD
    Example workbook attached - just change the folder path in the CombineFromFolder query, then refresh. To update, simply refresh the query.
    Attached Files Attached Files
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    09-25-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    88

    Re: Vba code to compile specific details from different excel file to one file from folder

    Hi Olly,

    Thanks for the reply!

    Actually i do not have power query install on my system.

    Is it possible by macro.

    I am using below code but it is not working.

    Sub Example()
    Dim Mypath As String
    Dim wbk As Workbook
    Dim Targetfile As Worksheet
    'Where all files are located
    Mypath = "C:\Users\r.shi\Desktop\New folder"
    'In this workbook I want to compile details (Unsaved)
    Set Targetfile = ThisWorkbook.Sheets("Sheet1")
    Do While Mypath <> ""
    Set wbk = Mypath.Workbooks.Worksheets("Sheet1")
    Range("A10:D10").Copy
    Targetfile.Activate Range("A2").PasteSpecial
    Range("A18:B18").Copy
    Targetfile.Activate
    ActiveCell.Offset(0, 1).PasteSpecial
    Range("E12").Copy
    Targetfile.Activate
    ActiveCell.Offset(0, 1).PasteSpecial
    Range("G46").Copy
    Targetfile.Activate
    ActiveCell.Offset(0, 1).PasteSpecial
    ActiveCell.Offset(1, 0).Activate
    Loop
    End Sub
    Regards,

    upa
    Last edited by UPA; 05-25-2017 at 01:54 AM. Reason: edit

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Vba code to compile specific details from different excel file to one file from folder

    I'd recommend installing Power Query, and learning the basics. It's SO MUCH better than VBA for doing this kind of data processing.

    However - here's a VBA solution. It will be much slower than the Power Query version:

    Sub CombineFromFolder()
        
        Dim stDirectory As String
        Dim stFile As String
        Dim wbSrc As Workbook
        Dim wsSrc As Worksheet
        Dim wsTgt As Worksheet
        Dim tbTgt As ListObject
        Dim lWB As Long
        
        Const sTableName As String = "tbCombine"
        Set wsTgt = Sheet1  'change to your output sheet, if needed
        
        On Error GoTo Terminate
        
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
        End With
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then
                stDirectory = .SelectedItems(1) & "\"
            Else
                Err.Raise -1001, , "No directory"
            End If
        End With
        
        If Not TableExists(wsTgt, sTableName) Then
            If Not MakeTable(wsTgt.Range("A1"), sTableName) Then
                Err.Raise -1002, , "No output table"
            End If
        End If
        
        Set tbTgt = wsTgt.ListObjects(sTableName)
        With tbTgt
            If Not .DataBodyRange Is Nothing Then .DataBodyRange.Rows.Delete
        End With
        
        stFile = Dir(stDirectory)
    
        While (stFile <> "")
            Application.StatusBar = "Processing file: " & stFile
            Set wbSrc = Workbooks.Open(stDirectory & stFile)
            lWB = lWB + 1
            
            Set wsSrc = wbSrc.Worksheets(1) ' change source sheet if needed - currently looks at first sheet
    
            With tbTgt.ListRows.Add
                .Range(1, 1).Value = wsSrc.Range("A10").Value
                .Range(1, 2).Value = wsSrc.Range("A18").Value
                .Range(1, 3).Value = wsSrc.Range("E12").Value
                .Range(1, 4).Value = wsSrc.Range("G46").Value
            End With
            
            wbSrc.Close SaveChanges:=False
            stFile = Dir
            DoEvents
        Wend
    
        Application.ScreenUpdating = True
        MsgBox "Updated data from " & lWB & " source files", vbInformation + vbOKOnly
    
    Terminate:
        If Err Then
            Debug.Print "Error", Err.Number, Err.Description
            Err.Clear
        End If
        With Application
            .StatusBar = False
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
        End With
    
    End Sub
    
    Function TableExists(ByRef ws As Worksheet, ByVal sTableName As String) As Boolean
        
        Dim lo As ListObject
        
        On Error GoTo Terminate
        
        For Each lo In ws.ListObjects
            If lo.Name = sTableName Then TableExists = True
        Next lo
        
    Terminate:
        If Err Then
            TableExists = False
            Err.Clear
        End If
        
    End Function
    
    Function MakeTable(ByRef cTableRange As Range, ByVal sTableName As String)
        
        Dim vHeaders() As Variant
        Dim i As Integer
        
        On Error GoTo Terminate
        
        vHeaders = Array("Agency", "Advertise", "Start", "End")
        Set cTableRange = cTableRange.Resize(1, UBound(vHeaders) + 1)
        cTableRange.Value = vHeaders
        Sheet1.ListObjects.Add(SourceType:=xlSrcRange, Source:=cTableRange, xllistobjecthasheaders:=xlYes).Name = sTableName
        
    Terminate:
        If Err Then
            MakeTable = False
            Err.Clear
        Else
            MakeTable = True
        End If
    End Function
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    09-25-2015
    Location
    India
    MS-Off Ver
    2013
    Posts
    88

    Re: Vba code to compile specific details from different excel file to one file from folder

    Hi Olly,

    Thank you for your reply - much appreciated.

    thanks so much for all your hard work and efforts in producing this VBA code - it's amazing and it works very well.

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Vba code to compile specific details from different excel file to one file from folder

    Happy to help. If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. You can also Add Reputation to those who helped you. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Saving file to folder using cell details
    By deefield in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-30-2016, 05:18 PM
  2. Replies: 2
    Last Post: 12-29-2015, 04:19 AM
  3. extracting folder & file details in excel
    By Navin Agrawal in forum Excel General
    Replies: 1
    Last Post: 01-16-2015, 11:51 AM
  4. [SOLVED] Auto hyperlink file from specific folder after enter file name in cell
    By satputenandkumar0 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-12-2014, 06:11 AM
  5. Create folder, excel file, pdf file, clear details macro
    By cemartinho in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-16-2013, 08:52 AM
  6. List of file names & details in a folder
    By Navin Agrawal in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-04-2012, 04:02 AM
  7. a macro to show all file details existing in a folder.
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-03-2010, 09:59 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1