+ Reply to Thread
Results 1 to 8 of 8

split column into multiple files into multiple columns based on another file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    split column into multiple files into multiple columns based on another file

    hi
    I would split column B into files PUR1,PUR2 into multiple columns into column B,C,D,E based on splited data file SEARCH into columns B,C,D . the result should be in first sheet into files PUR1,2 and should replace the data every time run the macro . I put the expected result in both the files in sheet result . I have about five files in the same folder whit the same structure as in PUR1,2

    thanks in advance
    Attached Files Attached Files
    Last edited by Hasson; 01-31-2022 at 05:22 AM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,388

    Re: split column into multiple files into multiple columns based on another file

    Is there any reason why you have separate workbooks rather than putting all the data in one workbook with 2 sheets per "PURx"??

    See attached

    Named range "Key" based on formula in H of "RP"

    =D2& " " &E2& " " &F2

    in c2 of "PUR1B"

    =IFERROR(INDEX(RP!D$2:D$1000,MATCH(PUR1A!$B2,Key,0)),"")

    copy across to E

    in F2

    =IFERROR(INDEX(PUR1A!E$2:E$1000,MATCH(PUR1A!$B2,PUR1A!$B$2:$B$1000,0)),"")
    Attached Files Attached Files
    Last edited by JohnTopley; 01-16-2022 at 12:33 PM.
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,388

    Re: split column into multiple files into multiple columns based on another file

    VBA version

    Sub Reports(wb As Workbook)
    
    Dim i As Long, j As Long, n As Long, r As Long, c As Long
    Dim lastrow As Long, Lastcol As Long
    Dim inarr, outarr, BP_Key, Key_Arr
    Dim res  As Integer
    
    Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    
    'MsgBox wb.Name
    
    Set ws1 = ThisWorkbook.Worksheets("RP")
     
    With ws1  ' Sheet "RP"
        
            lastrow = .Cells(Rows.Count, 3).End(xlUp).Row
            Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
            
            inarr1 = .Range(.Cells(2, 3), .Cells(lastrow, Lastcol))  ' "RP" data
            
            BP_Key = .Range("key")  ' list of "keys"
        
    End With
    
    With wb
    
        Set ws2 = Worksheets("Report")
        Set ws3 = Worksheets("Result")
        
        With ws2  ' Sheet "Report"
        
            lastrow = .Cells(Rows.Count, 2).End(xlUp).Row
            Lastcol = .Cells(1, Columns.Count).End(xlToLeft).Column
            
            Key_Arr = .Range(.Cells(2, 2), .Cells(lastrow, 2))
            inarr2 = .Range(.Cells(2, 1), .Cells(lastrow, Lastcol))
            
        End With
        
        ReDim outarr(1 To UBound(inarr2, 1), 1 To UBound(inarr2, 2))
        
        ws3.Activate ' sheet "Result"
        
        With ws3
            
            For r = 1 To UBound(inarr2, 1)
                res = Application.Match(inarr2(r, 2), BP_Key, 0) ' Match "ID" vs "key"
                If Not IsError(res) Then
                    outarr(r, 1) = inarr2(r, 1)
                    outarr(r, 2) = inarr1(res, 2)
                    outarr(r, 3) = inarr1(res, 3)
                    outarr(r, 4) = inarr1(res, 4)
                    res = Application.Match(inarr2(r, 2), Key_Arr, 0)
                    If Not IsError(res) Then
                        outarr(r, 5) = inarr2(res, 5)  ' Qty
                    End If
                End If
                
            Next r
        
        End With
        
        ws3.Range("B2").Resize(UBound(outarr, 1), UBound(outarr, 2)) = outarr
        
    
    End With
    
    End Sub
    Sub Loop_Folder()
    
    Dim wb As Workbook
    Dim myPath As String
    Dim myFile As String
    Dim myExtension As String
    Dim FldrPicker As FileDialog
    
    'Optimize Macro Speed
      Application.ScreenUpdating = False
      Application.EnableEvents = False
      Application.Calculation = xlCalculationManual
    
    'Retrieve Target Folder Path From User
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With
    
    'In Case of Cancel
    NextCode:
      myPath = myPath
      If myPath = "" Then GoTo ResetSettings
    
    'Target File Extension (must include wildcard "*")
      myExtension = "*.xls*"
    
    'Target Path with Ending Extention
      myFile = Dir(myPath & myExtension)
    
    'Loop through each Excel file in folder
      Do While myFile <> ""
        'Set variable equal to opened workbook
          Set wb = Workbooks.Open(fileName:=myPath & myFile)
        
        'Ensure Workbook has opened before moving on to next line of code
          DoEvents
        
         Call Reports(wb)  ' ****** Produce Reports  *******
        
        'Save and Close Workbook
          wb.Close SaveChanges:=True
          
        'Ensure Workbook has closed before moving on to next line of code
          DoEvents
    
        'Get next file name
          myFile = Dir
      Loop
    
    'Message Box when tasks are completed
      MsgBox "Task Complete!"
    
    ResetSettings:
      'Reset Macro Optimization Settings
        Application.EnableEvents = True
        Application.Calculation = xlCalculationAutomatic
        Application.ScreenUpdating = True
    
    End Sub
    Assumes ALL files in the selected folder are to be processed.

    Ensure TAB names are consistent (currently "Report" and "Result")
    Attached Files Attached Files
    Last edited by JohnTopley; 01-17-2022 at 03:39 AM.

  4. #4
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    Re: split column into multiple files into multiple columns based on another file

    Is there any reason why you have separate workbooks rather than putting all the data in one workbook with 2 sheets per "PURx"??
    yes , theses files issued by another company and sent by email throught each month and differnt date .
    the result are put into files just are simple to undersatnd what I want, but my real data for each file are about 500 rows and it will take more time copy & paste in one file . so that's why I look for macro achieve that.
    as to post#3 I prefer VBA I will test it and inform you how works .

  5. #5
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    Re: split column into multiple files into multiple columns based on another file

    thanks . I would as I siad in OP
    the rsult should be in first sheet into files PUR1,2
    the sheet RESULT just to see how could be . the result should just be in the first sheet for all files are existed in the folder.
    last thing I don't want selected folder way by browser . I would do that automatically without select folder .

  6. #6
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    Re: split column into multiple files into multiple columns based on another file

    another choice experts?

  7. #7
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    Re: split column into multiple files into multiple columns based on another file

    cross posted https://www.mrexcel.com/board/thread...-file.1193454/
    no answer so far and edited the file

  8. #8
    Forum Contributor
    Join Date
    05-08-2021
    Location
    ALQUDS
    MS-Off Ver
    2019
    Posts
    218

    Re: split column into multiple files into multiple columns based on another file

    I would add note , if the items in column B into files PUR1,PUR2 are not matched with columns B,C,D, based on splited data file SEARCH then should not split and show message " there items are not matched you should correct them before split "

+ 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. [SOLVED] split column to multiple columns based on extension files
    By ABDELFATTA in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-11-2021, 12:31 PM
  2. Split Excel file into multiple Excel files based on specific column value
    By qiyusi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-04-2018, 10:08 PM
  3. Split a File to Multiple files with Selected columns into CSV format
    By suchetherrah in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-28-2017, 11:46 AM
  4. Replies: 4
    Last Post: 10-04-2015, 08:21 AM
  5. Split one Excel file into multiple files based on values in column A?
    By MetroBOS in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-06-2015, 10:59 AM
  6. Split Excel file into multiple files based on number of rows
    By kingtut86 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-24-2013, 03:38 PM
  7. Replies: 3
    Last Post: 08-02-2006, 12:35 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