I have several documents that need sorting to separate different documents based on certain criteria (like Location or Name).
I did find script already, but I have hard time to make it work independently on different documents. Although I kinda understand what the code does, I dont know how to successfully modify it for my needs. I also have hard time to understand from where different values or variables come from or how to set my own variables and place them inside the code...
The script only works in that example file, but I cannot make it stand-alone. And what I mean by that, is I'd like to copy+paste the code into new Macro and then run it. Not to include it inside the document itself. Buttons etc, like in the example file had.
In my case, I receive Excel-files, that contain information about different offices and their monthly work orders and so on. I'd like to have every shop to be sorted and automaticly copied to their own files so every shops own information would be send to them only.
What I did manage to do is force output folder and set proper Cell from which the unique information is gathered. But "Range("Data[#Headers]"), 0)" gives error "Method range of object - 'Global' failed"
I also tried to make simple variables at the beginning of the code, so it would be simpler to set output folders and criteria cell, in case the Excel format might change someday
'Declare variables Dim ArrayItem As Long Dim ws As Worksheet Dim ArrayOfUniqueValues As Variant Dim SavePath As String Dim ColumnHeadingInt As Long Dim ColumnHeadingStr As String Dim rng As Range
'Set the worksheet to Set ws = Sheets("Data")
'Set the save path for the files created SavePath = "D:\Home\Projects\excel\putput2\"
'Set variables for the column we want to separate data based on ColumnHeadingInt = WorksheetFunction.Match(Range("B1").Value, Range("Data[#Headers]"), 0) ColumnHeadingStr = "Data[[#All],[" & Range("B1").Value & "]]"
'Turn off screen updating to save runtime Application.ScreenUpdating = False
'Create a temporary list of unique values from the column we want to 'separate our data based on Range(ColumnHeadingStr & "").AdvancedFilter Action:=xlFilterCopy, _ CopyToRange:=Range("UniqueValues"), Unique:=True
'Sort our temporary list of unique values ws.Range("UniqueValues").EntireColumn.Sort Key1:=ws.Range("UniqueValues").Offset(1, 0), _ Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, _ Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
'Add unique field values into an array 'ArrayOfUniqueValues = Application.WorksheetFunction.Transpose(ws.Range("IV2:IV" & Rows.Count).SpecialCells(xlCellTypeConstants)) ArrayOfUniqueValues = Application.WorksheetFunction.Transpose(ws.Range("UniqueValues").EntireColumn.SpecialCells(xlCellTypeConstants))
'Delete the temporary values ws.Range("UniqueValues").EntireColumn.Clear
'Loop through our array of unique field values, copy paste into new workbooks and save For ArrayItem = 1 To UBound(ArrayOfUniqueValues) ws.ListObjects("Data").Range.AutoFilter Field:=ColumnHeadingInt, Criteria1:=ArrayOfUniqueValues(ArrayItem) ws.Range("Data[#All]").SpecialCells(xlCellTypeVisible).Copy Workbooks.Add Range("A1").PasteSpecial xlPasteAll ActiveWorkbook.SaveAs SavePath & ArrayOfUniqueValues(ArrayItem) & Format(Now(), " YYYY-MM-DD hhmmss") & ".xlsx", 51 ActiveWorkbook.Close False ws.ListObjects("Data").Range.AutoFilter Field:=ColumnHeadingInt Next ArrayItem