+ Reply to Thread
Results 1 to 6 of 6

Extract multi excel file into a single file

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Extract multi excel file into a single file

    hello, I am trying to extract many profiles & product files into single file.
    on sheet "Merchant" had succeeded but for sheet "Produk" has not been successful.
    here it is my code
    Sub Button1_Click()
    Dim vaFiles As Variant
        Dim i As Long
        Dim WB As Workbook
        
        vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", _
                  Title:="Select files", MultiSelect:=True)
        
        If IsArray(vaFiles) Then
            For i = LBound(vaFiles) To UBound(vaFiles)
                Set WB = Workbooks.Open(Filename:=vaFiles(i))
    
                   
      NextFree = ThisWorkbook.Sheets("Merchant").Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Value = WB.Sheets("Merchant").Range("C4").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 1).Value = WB.Sheets("Merchant").Range("C5").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 2).Value = WB.Sheets("Merchant").Range("C6").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 3).Value = WB.Sheets("Merchant").Range("C7").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 4).Value = WB.Sheets("Merchant").Range("C8").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 5).Value = WB.Sheets("Merchant").Range("C9").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 6).Value = WB.Sheets("Merchant").Range("C10").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 7).Value = WB.Sheets("Merchant").Range("C11").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 8).Value = WB.Sheets("Merchant").Range("C12").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 9).Value = WB.Sheets("Merchant").Range("C13").Value
     
     NextFree2 = ThisWorkbook.Sheets("Produk").Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
     Dim k As Long
        For k = 3 To 251
            If Not IsEmpty(WB.Sheets("Produk").Range("C" & k)) Then _
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)) = WB.Sheets("Produk").Range("C" & k)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 1) = WB.Sheets("Produk").Range("c" & k).Offset(0, 1)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 2) = WB.Sheets("Produk").Range("c" & k).Offset(0, 2)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 3) = WB.Sheets("Produk").Range("c" & k).Offset(0, 3)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 4) = WB.Sheets("Produk").Range("c" & k).Offset(0, 4)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 5) = WB.Sheets("Produk").Range("c" & k).Offset(0, 5)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 6) = WB.Sheets("Produk").Range("c" & k).Offset(0, 6)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 7) = WB.Sheets("Produk").Range("c" & k).Offset(0, 7)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 8) = WB.Sheets("Produk").Range("c" & k).Offset(0, 8)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 9) = WB.Sheets("Produk").Range("c" & k).Offset(0, 9)
            ThisWorkbook.Sheets("Produk").Range("B" & (NextFree2 + k - 3)).Offset(0, 10) = WB.Sheets("Produk").Range("c" & k).Offset(0, 10)
        Next k
                WB.Close savechanges:=False
            Next i
        End If
    End Sub

    any help would be appreciated
    and here is my files
    Attached Files Attached Files

  2. #2
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Extract multi excel file into a single file

    Hi saputroa272
    Quote Originally Posted by saputroa272 View Post
    hello, I am trying to extract many profiles & product files into single file.
    on sheet "Merchant" had succeeded but for sheet "Produk" has not been successful.
    here it is my code………
    and here is my files……
    . You need to give us much more information in order for us to help you. It is good that you have given us the code – that is a good start point for us. And you have given us the “Before File” Extract Multifile trial.xlsm . But from the information that you have given we can have no idea what is going wrong as you have not stated clearly exactly how you want the File “Extract Multifile trial.xlsm” to look like after successful running of a code.
    . Please in future provide a second “Extract Multifile trial File” ,( the “After” File ) which you fill in by hand to show exactly what output you want finally after a successful running of a code
    ……..
    . In any case I have made a few guesses as to what you want and rewritten your code so that it consolidates profiles & product files into the File “Extract Multifile trial.xlsm” assuming that “Extract Multifile trial.xlsm” would be empty to begin with:--***
    . - I think your main problem in your original code was in determining your Nextfree2 -
    . a) ***File “Extract Multifile trial.xlsm” appeared to have some data in a long way down in sheet Produk that you may have overlooked
    . b) even when deleting this data your calculation of NextFree2 seemed to give erratic results which I do not yet understand. I had to empty cells which looked empty to get the codes too work. But I am not sure if you intended having this data there?***
    . But I have also simplified greatly your 'copy, paste, whatever stuff” as well. ( Assuming for now you have no empty rows within the data to be consolidated as per your sample data )

    Code:
    Option Explicit
    Sub Button1_Click()
    Dim vaFiles As Variant
        Dim i As Long
        Dim WB As Workbook
        
        vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", Title:="Select files", MultiSelect:=True)
        
        If IsArray(vaFiles) Then
            For i = LBound(vaFiles) To UBound(vaFiles)
                Set WB = Workbooks.Open(Filename:=vaFiles(i))
     
                'copy, paste, whatever, for merchant
                Dim NextFree As Long
                  NextFree = ThisWorkbook.Sheets("Merchant").Cells(Rows.Count, 2).End(xlUp).Row + 1 'Last cell in Column 2 has end property ( argument "going upwards" (xlup)) applied which returns a new range ( cell ) with last entry in it, to which the .Row property retuns the row number. + 1 give next free cell
                
                WB.Sheets("Merchant").Range("C4:C13").Copy 'Copy column to clipboard assuming it is always same size, same position in WB
                ThisWorkbook.Sheets("Merchant").Range("B" & NextFree & "").PasteSpecial Paste:=xlPasteAll, Transpose:=True 'Paste to next free row in This workbook Merchant sheet. ( Need to Transpose to change Column to Row )
                
                'copy, paste, whatever, for Produk
                Dim Nextfree2 As Long
                 Nextfree2 = ThisWorkbook.Sheets("Produk").Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
                Dim lastProduk As Long
                 Let lastProduk = WB.Worksheets("Produk").Cells(Rows.Count, 4).End(xlUp).Row
                
                 WB.Worksheets("produk").Range("C3:M" & lastProduk & "").Copy Destination:=ThisWorkbook.Worksheets("Produk").Range("B" & Nextfree2 & "") 'We only are interested in values so can use Copy Destination for entore range which is quicker by by-passing the clipboard
                 
                 WB.Close savechanges:=False
            Next i
        End If
    End Sub

    Alan Elston
    Bavaria
    Germany
    Last edited by Doc.AElstein; 07-15-2015 at 04:40 AM.
    '_- Google first, like this _ site:ExcelForum.com Gamut
    Use Code Tags: Highlight code; click on the # icon above,
    Post screenshots COPYABLE to a Spredsheet; NOT IMAGES PLEASE
    http://www.excelforum.com/the-water-...ml#post4109080
    https://app.box.com/s/gjpa8mk8ko4vkwcke3ig2w8z2wkfvrtv
    http://excelmatters.com/excel-forums/ ( Scrolll down to bottom )

  3. #3
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Re: Extract multi excel file into a single file

    well thanks in advance for the reply doc, it seem your code much shorter than mine I will learn how it works to get my code more efficient. the idea is to copy non blank value (products list)in profile&product workbook sheet"produk" range C3:M200 to extractmultifile workbook sheet"produk" at the first blank cell in B3:B column.It embarasing that i found my self forgotten to write ".value" on sheet"produk" so here is the worked code:
    Dim vaFiles As Variant
        Dim i As Long
        Dim WB As Workbook
        Dim NextFree As Long
        Dim NF As Long
        Dim kk As Long
        vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", _
                  Title:="Select files", MultiSelect:=True)
        
        If IsArray(vaFiles) Then
            For i = LBound(vaFiles) To UBound(vaFiles)
                Set WB = Workbooks.Open(Filename:=vaFiles(i))
    
                   
     NextFree = ThisWorkbook.Sheets("Merchant").Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
     NF = ThisWorkbook.Sheets("Produk").Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Value = WB.Sheets("Merchant").Range("C4").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 1).Value = WB.Sheets("Merchant").Range("C5").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 2).Value = WB.Sheets("Merchant").Range("C6").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 3).Value = WB.Sheets("Merchant").Range("C7").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 4).Value = WB.Sheets("Merchant").Range("C8").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 5).Value = WB.Sheets("Merchant").Range("C9").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 6).Value = WB.Sheets("Merchant").Range("C10").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 7).Value = WB.Sheets("Merchant").Range("C11").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 8).Value = WB.Sheets("Merchant").Range("C12").Value
     ThisWorkbook.Sheets("Merchant").Range("B" & NextFree).Offset(0, 9).Value = WB.Sheets("Merchant").Range("C13").Value
     
      Dim k As Long
      kk = NF - 3
          For k = 3 To 251
            If Not IsEmpty(WB.Sheets("Produk").Range("C" & k)) Then _
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Value = WB.Sheets("Produk").Range("C" & k).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 1).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 1).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 2).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 2).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 3).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 3).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 4).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 4).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 5).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 5).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 6).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 6).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 7).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 7).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 8).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 8).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 9).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 9).Value
            ThisWorkbook.Sheets("Produk").Range("B" & (kk + k)).Offset(0, 10).Value = WB.Sheets("Produk").Range("c" & k).Offset(0, 10).Value
        Next k
                WB.Close savechanges:=False
            Next i
        End If
    so it Solved. sorry for my english

  4. #4
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Extract multi excel file into a single file

    Hi saputroa272
    . Just a quick follow up here as I was working on a similar problem and had horrendous problems in checking for “Empty cells”……

    . 1) Usually .Value would be taken as default if omitted. But interestingly here it leads to your code not working, that is to say NextFree2 seemed to give erratic results which does not appear to be the case with your modified code.
    . A strange problem, but emphasizes as always not too rely on the default and always remember to include .Value….

    . 2) There could still be some problems lurking to catch you out unexpectedly to do with “Empty cells”..
    .. here is another code alternative ( 2 versions ) which my be more appropriate, or at least another alternative to have as a back- up..


    Full code with explaining comments:

    '
    Sub Alan2()
    Dim vaFiles As Variant
        Dim i As Long, k As Long 'Loop Bound variables counts'( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647) If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, For example Integer need converted internally anyways, so a Long is actually faster.
        Dim WB As Workbook 'used for files to be integrated in loop, set each time to specific WorkBook
        Dim wsProd As Worksheet, wsMerch As Worksheet 'Variables for Sheets in main Sheet
        Set wsProd = ThisWorkbook.Worksheets("Produk"): Set wsMerch = ThisWorkbook.Worksheets("Merchant") 'Give abbreviations Methods Properties, etc., of Worksheet Object
        
        With ThisWorkbook.Worksheets("Produk").Columns(2) 'Quick way to allow "one liner" code below to assign to large range
        .Value = .Value 'Important Initial step to ensure that Empty cells are Empty!!
        End With
    
        
        vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", Title:="Select files", MultiSelect:=True)
        
        If IsArray(vaFiles) Then 'Chech we have profile and productfiles to work with
            For i = LBound(vaFiles) To UBound(vaFiles)
                Set WB = Workbooks.Open(Filename:=vaFiles(i))
     
                'copy, paste, whatever, for merchant
                Dim Nextfree As Long
                  Nextfree = wsMerch.Cells(Rows.Count, 2).End(xlUp).Row + 1 'Last cell in Column 2 has end property ( argument "going upwards" (xlup)) applied which returns a new range ( cell ) with last entry in it, to which the .Row property retuns the row number. + 1 give next free cell
                
                WB.Sheets("Merchant").Range("C4:C13").Copy 'Copy column to clipboard assuming it is always same size, same position in WB
                ThisWorkbook.Sheets("Merchant").Range("B" & Nextfree & "").PasteSpecial Paste:=xlPasteAll, Transpose:=True 'Paste to next free row in This workbook Merchant sheet. ( Need to Transpose to change Column to Row )
                
                'copy, paste, whatever, for Produk
                Dim Nextfree2 As Long
                    For k = 3 To 251 'Check down a large number of rows
                            If WB.Sheets("Produk").Range("C" & k).Value <> "" Then 'This will only check for a value present, not if it is Not Empty: Overcomes the problem if no value is there but some formating which would possibly be then taken as Not Empty
                                Nextfree2 = wsProd.Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row 'Find next Empty cell in column 2 in Produk sheet in main File
                                WB.Worksheets("produk").Range("C" & k & ":M" & k & "").Copy 'Copy entire required range from Profile and Product file
                                wsProd.Range("B" & Nextfree2 & "").PasteSpecial xlPasteValues 'Pasting in the copied range in Main Sheet. ( Paste, or Paste Destination:= woulde also work here )
                            Else 'If cell in column C has no value in it, take no action: Redundant code
                            End If
                    Next k 'Check next row in profile and product File
                 WB.Close savechanges:=False 'Close profile and product , using argument to ignorebeing asked to save changes
            Next i 'go to next profile and product File
        End If
    End Sub

    Shortened / Simplified code:

    '
    Sub Alan2SHimpfGlified()
        Dim vaFiles, i As Long, k As Long
        Dim WB As Workbook
        Dim wsProd As Worksheet, wsMerch As Worksheet
        Set wsProd = ThisWorkbook.Worksheets("Produk"): Set wsMerch = ThisWorkbook.Worksheets("Merchant")
        
        With ThisWorkbook.Worksheets("Produk").Columns(2)
        .Value = .Value
        End With
      
        vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", Title:="Select files", MultiSelect:=True)
        
        If IsArray(vaFiles) Then
            For i = LBound(vaFiles) To UBound(vaFiles)
                Set WB = Workbooks.Open(Filename:=vaFiles(i))
     
    'copy, paste, whatever, for merchant
                WB.Sheets("Merchant").Range("C4:C13").Copy
                ThisWorkbook.Sheets("Merchant").Range("B" & wsMerch.Cells(Rows.Count, 2).End(xlUp).Row + 1 & "").PasteSpecial Paste:=xlPasteAll, Transpose:=True
    '----
    'copy, paste, whatever, for Produk
                    For k = 3 To 251
                            If WB.Sheets("Produk").Range("C" & k).Value <> "" Then
                                WB.Worksheets("produk").Range("C" & k & ":M" & k & "").Copy Destination:=wsProd.Range("B" & wsProd.Range("B2:B" & Rows.Count).Cells.SpecialCells(xlCellTypeBlanks).Row & "")
                            End If
                    Next k
                 WB.Close savechanges:=False
    '----
            Next i
        End If
    End Sub

    Alan

  5. #5
    Registered User
    Join Date
    07-03-2015
    Location
    Jakarta
    MS-Off Ver
    2010
    Posts
    12

    Re: Extract multi excel file into a single file

    Hi Doc.AElstein
    1. noted sir
    2. superb, both of them are worked fine. Its good for me as a back- up if i find a problem with “Empty cells”
    thanks a alot

  6. #6
    Forum Expert Doc.AElstein's Avatar
    Join Date
    05-23-2014
    Location
    '_- Germany >Outside Building things.... Mostly
    MS-Off Ver
    Office 2003 2007 2010 PC but Not mac. XP and Vista mostly, sometimes Win 7
    Posts
    3,618

    Re: Extract multi excel file into a single file

    Quote Originally Posted by saputroa272 View Post
    Hi Doc.AElstein
    1. noted sir
    2. superb, both of them are worked fine. Its good for me as a back- up if i find a problem with “Empty cells”
    thanks a alot
    . You're welcome,
    . Thanks for the feedback.
    . Alan Elston

+ 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. Excel VBA to scan a PDF for keyword and extract PDF Pages to single PDF File
    By Brawnystaff in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2014, 02:00 AM
  2. [SOLVED] Wanted to extract data from a file which is already an excel macro file
    By Raju Radhakrishnan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2013, 11:43 AM
  3. Multi Pages from MSN Money (StockScouter) to One Single Excel File
    By SimonChow in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2013, 04:19 AM
  4. [SOLVED] Extract a few values from several .csv-files and bundle them in a single output-file
    By Jeroen606 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-14-2013, 01:05 AM
  5. How to extract Information from an Excel File and entering those data in a text file?
    By bikash.nitk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2012, 12:13 PM
  6. convert multi-sheets file to single-sheet files
    By yahata in forum Excel General
    Replies: 3
    Last Post: 04-22-2007, 06:54 AM
  7. [SOLVED] Saving multi-tab excel file created from comma delimited text file
    By Marcus Aurelius in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-19-2005, 01:20 PM

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