Create file xls based on colums & populate data with criteria

Solved/Closed
Steveryde Posts 3 Registration date Tuesday May 13, 2014 Status Member Last seen May 14, 2014 - May 13, 2014 at 05:42 AM
Steveryde Posts 3 Registration date Tuesday May 13, 2014 Status Member Last seen May 14, 2014 - May 14, 2014 at 09:05 AM
Hi,

I need a macro that create some new file (txt or xls it doesen't matter) starting from an existing xls.

The existing xls named "Arretrati" has data from colum A to column AA, I have to create some new files using the data in the column "F-Intermediary Code" (named with the content of column F), and I have to copy into the new file all the data that have the same code.

In the original file arretrati I can have many rows for the same intermediary code, I need to create only one new file with the code as name, then I need to copy there all the data, from colum A to AA for this code.

Thanks all for any help.
Related:

4 responses

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
May 13, 2014 at 10:50 AM
Hi Steveryde,

Do you have anything against applying autofilter?

Filter on "F-Intermediary Code", copy entire sheet, open new workbook, paste data and save workbook as "F-Intermediary Code".

Best regards,
Trowa
0
Steveryde Posts 3 Registration date Tuesday May 13, 2014 Status Member Last seen May 14, 2014
May 13, 2014 at 11:23 AM
Sorry, I'ven't understand.

I gave you an example, in the "original" file arretrati.xlsx I have the intermediary code in the column F (same code many rows). In the other colums I have many other information (column A - Name, Column B Address, Column C policy, etc).
Ando so for the intermediary 0001 in the arretrati.xlsx I have 10 rows, then i need to create the file 0001.xlsx with 10 rows, for the intermediary 0002 35 rows, i need to create the file 0002.xlsx with the 35 rows....and so on.

I can do this with the autofilter?
0
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 555
May 13, 2014 at 12:18 PM
Hi Steveryde,

Yeah, I thought maybe you wanted to do this with one criteria.

To know which criteria to look for I created a sheet "Filterlist" (create this sheet in your original file "arretrati.xlsx"). On this sheet I used column A to put the criteria's. So A1 = 0001, A2 = 0002 and so on.

Then I run the code below to:
- filter data based on the criteria's found in the "Filterlist" Sheet.
- copy/paste filter result in new workbook.
- save workbook as criteria's name and close workbook.

Try it and see if it yields the desired result.

Sub RunMe()
Dim x As Integer
Dim sValue As String

x = 1

Do
    sValue = Sheets("Filterlist").Cells(x, 1)

    Cells.AutoFilter Field:=6, Criteria1:=sValue
    Cells.Copy
    
    Workbooks.Add
    ActiveWorkbook.Sheets(1).Range("A1").PasteSpecial
    ActiveWorkbook.SaveAs ("C:\MyDocuments\" & sValue & ".xls") 'Make sure both filepath and extention are correct
    ActiveWorkbook.Close
    
    x = x + 1

Loop Until Sheets("Filterlist").Cells(x, 1) = vbNullString

Cells.AutoFilter

End Sub


Best regards,
Trowa

0
Steveryde Posts 3 Registration date Tuesday May 13, 2014 Status Member Last seen May 14, 2014
May 14, 2014 at 09:05 AM
Hi Trowa,

Thanks a lot for your precious help.

I've changed the code you wrote for me because it doesn't run under my excel.

Here the code modified:

---------------------------------------------------------------------
Sub CreaFileIntermediario()

Dim x As Integer
Dim sValue, sPath As String
Dim My_Range As Range
Dim LastRow As Long

x = 2

sPath = ActiveWorkbook.Path

Sheets("Arretrati").Select
Range("A2").Select
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Set My_Range = Range("A1:Q" & LastRow)
My_Range.Parent.Select

Do



sValue = Sheets("Filterlist").Cells(x, 1)

My_Range.Parent.AutoFilterMode = False
My_Range.AutoFilter Field:=2, Criteria1:=sValue
My_Range.Copy

Workbooks.Add
ActiveWorkbook.Sheets(1).Range("A1").PasteSpecial
ActiveWorkbook.SaveAs (sPath & "\" & sValue & ".xlsx")
ActiveWorkbook.Close

x = x + 1

Loop Until Sheets("Filterlist").Cells(x, 1) = vbNullString

Cells.AutoFilter

End Sub
---------------------------------------------------------------------

I hope that this code could help somebody else.

Regards
0