Results 1 to 6 of 6

Thread: split data into multiple workbooks with 3 conditions.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi,

    Try this.

    Code:
    Sub kTest()
        
        Dim k, Data As Range, x, Hdr, i As Long, Crits, s As String
        Dim wbkActive   As Workbook, wbkNew As Workbook, j As Long
        Dim FN  As String, Cols
        
        Set wbkActive = ThisWorkbook
        
        Hdr = Array("Gateway", "Supplier Code", "Formulated Column", "Supplier Code", _
                    "Reference", "Amount (£)", "Doc Currency", "Country", "Invoice Number")
        Cols = Array(3, 6, , 7, 5, 8, 9, 11, 4)
        
        Application.ScreenUpdating = False
        
        With wbkActive.Sheets("workings")
            If .AutoFilterMode Then .AutoFilterMode = False
            Set Data = .Range("a1:l" & .Range("a" & .Rows.Count).End(xlUp).Row)
            k = Data.Value2
        End With
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 2 To UBound(k, 1)
                If Len(k(i, 2)) Then
                    s = k(i, 2) & "|" & k(i, 9) & "|" & k(i, 11)
                    If Not .exists(s) Then
                        .Add s, Nothing
                    End If
                End If
            Next
            k = .keys
        End With
        
        With Data
            For i = 0 To UBound(k)
                x = Split(k(i), "|")
                .AutoFilter 2, x(0), 1
                .AutoFilter 9, x(1), 1
                .AutoFilter 11, x(2)
                FN = .Offset(1).Cells(1, .Columns.Count).Value
                Set wbkNew = Workbooks.Add
                With wbkNew.Sheets(1)
                    .Range("a2:a4") = Application.Transpose([{"File Number","Payment/Recovery","Currency"}])
                    .Range("a7").Resize(, UBound(Hdr) + 1) = Hdr
                    .Range("b2") = FN
                    .Range("b3") = x(0)
                    .Range("b4") = x(1)
                End With
                On Error Resume Next
                For j = 0 To UBound(Cols)
                    .Columns(Cols(j)).Copy wbkNew.Sheets(1).Cells(8, j + 1)
                Next
                On Error GoTo 0
                wbkNew.Sheets(1).Rows(8).Delete
                wbkNew.SaveAs wbkActive.Path & "\" & FN & "-" & x(0) & "-" & x(1) & "-" & x(2), 51
                wbkNew.Close
                Set wbkNew = Nothing
            Next
            .Parent.AutoFilterMode = False
        End With
        Application.ScreenUpdating = True
        
    End Sub
    HTH
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    split data into multiple workbooks with 3 conditions

    Hi Admin,

    Thank you for your coding.

    I already have a excel workbook in which I paste the output of the segregated line items as the sheet 2 has a data for few formulas that I use in sheet 1 along with the output. This coding creates a new file for each output however, Is it possible to put the output data on the template file which I have.

    This means I have a file in which there is 2 sheets. I need the output data in sheet 1 and the sheet 2 should remain constant with the data whatever it has. because I need to send the output files to someone who need to work along with sheet2. Each output file should be in this format only.

    First sheet of output data which we have segregated and second sheet with default data. I guess we need to create a macro file keeping the sheet1 as template and then need to paste the data to it and then we need to save as it in the required file name.

    Is this possible to do?

    Regards,
    Malai



    Quote Originally Posted by Admin View Post
    Hi,

    Try this.

    Code:
    Sub kTest()
        
        Dim k, Data As Range, x, Hdr, i As Long, Crits, s As String
        Dim wbkActive   As Workbook, wbkNew As Workbook, j As Long
        Dim FN  As String, Cols
        
        Set wbkActive = ThisWorkbook
        
        Hdr = Array("Gateway", "Supplier Code", "Formulated Column", "Supplier Code", _
                    "Reference", "Amount (£)", "Doc Currency", "Country", "Invoice Number")
        Cols = Array(3, 6, , 7, 5, 8, 9, 11, 4)
        
        Application.ScreenUpdating = False
        
        With wbkActive.Sheets("workings")
            If .AutoFilterMode Then .AutoFilterMode = False
            Set Data = .Range("a1:l" & .Range("a" & .Rows.Count).End(xlUp).Row)
            k = Data.Value2
        End With
        With CreateObject("scripting.dictionary")
            .comparemode = 1
            For i = 2 To UBound(k, 1)
                If Len(k(i, 2)) Then
                    s = k(i, 2) & "|" & k(i, 9) & "|" & k(i, 11)
                    If Not .exists(s) Then
                        .Add s, Nothing
                    End If
                End If
            Next
            k = .keys
        End With
        
        With Data
            For i = 0 To UBound(k)
                x = Split(k(i), "|")
                .AutoFilter 2, x(0), 1
                .AutoFilter 9, x(1), 1
                .AutoFilter 11, x(2)
                FN = .Offset(1).Cells(1, .Columns.Count).Value
                Set wbkNew = Workbooks.Add
                With wbkNew.Sheets(1)
                    .Range("a2:a4") = Application.Transpose([{"File Number","Payment/Recovery","Currency"}])
                    .Range("a7").Resize(, UBound(Hdr) + 1) = Hdr
                    .Range("b2") = FN
                    .Range("b3") = x(0)
                    .Range("b4") = x(1)
                End With
                On Error Resume Next
                For j = 0 To UBound(Cols)
                    .Columns(Cols(j)).Copy wbkNew.Sheets(1).Cells(8, j + 1)
                Next
                On Error GoTo 0
                wbkNew.Sheets(1).Rows(8).Delete
                wbkNew.SaveAs wbkActive.Path & "\" & FN & "-" & x(0) & "-" & x(1) & "-" & x(2), 51
                wbkNew.Close
                Set wbkNew = Nothing
            Next
            .Parent.AutoFilterMode = False
        End With
        Application.ScreenUpdating = True
        
    End Sub
    HTH

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2013, 05:32 PM
  2. Import Data From Multiple Workbooks To Another
    By Jorrg1 in forum Excel Help
    Replies: 2
    Last Post: 05-13-2013, 05:00 PM
  3. Replies: 2
    Last Post: 03-05-2013, 07:34 AM
  4. copying data from multiple workbooks into another
    By rahulcoolz99 in forum Excel Help
    Replies: 1
    Last Post: 08-22-2012, 09:19 PM
  5. Split Closed Workbook into Multiple Workbooks Using ADO
    By ramakrishnan in forum Excel Help
    Replies: 4
    Last Post: 10-02-2011, 08:34 PM

Tags for this Thread

Posting Permissions

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