Modify Ron´s Code - Create a new workbook for all unique values And Create PivotTable is such workbooks

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
Ron´s Code - Create a new workbook for all unique values works just fine. IT is below.
But I need to go forward, and after all workbooks were created from unique values using this Ron´s code, I need to create PivotTable in each of these workbooks. Reference to Ron´s Code:http://www.rondebruin.nl/copy5_3.htm
any tips
Code:
Sub Copy_To_Workbooks()[COLOR=#000000]'Note: This macro use the function LastRow[/COLOR]    Dim My_Range As Range    Dim FieldNum As Long    Dim FileExtStr As String    Dim FileFormatNum As Long    Dim CalcMode As Long    Dim ViewMode As Long    Dim ws2 As Worksheet    Dim MyPath As String    Dim foldername As String    Dim Lrow As Long    Dim cell As Range    Dim CCount As Long    Dim WSNew As Worksheet    Dim ErrNum As Long  [COLOR=#000000]  'Set filter range on ActiveSheet: A1 is the top left cell of your filter range    'and the header of the first column, D is the last column in the filter range.    'You can also add the sheet name to the code like this :    'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))    'No need that the sheet is active then when you run the macro when you use this.[/COLOR]    Set My_Range = Range("A1:D" & LastRow(ActiveSheet))    My_Range.Parent.Select    If ActiveWorkbook.ProtectStructure = True Or _       My_Range.Parent.ProtectContents = True Then        MsgBox "Sorry, not working when the workbook or worksheet is protected", _               vbOKOnly, "Copy to new workbook"        Exit Sub    End If    [COLOR=#000000]'This example filters on the first column in the range(change the field if needed)    'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......[/COLOR]    FieldNum = 1    [COLOR=#000000]'Turn off AutoFilter[/COLOR]    My_Range.Parent.AutoFilterMode = False    [COLOR=#000000]'Set the file extension/format[/COLOR]    If Val(Application.Version) < 12 Then        [COLOR=#000000]'You use Excel 97-2003[/COLOR]        FileExtStr = ".xls": FileFormatNum = -4143    Else        [COLOR=#000000]'You use Excel 2007-2010[/COLOR]        If ActiveWorkbook.FileFormat = 56 Then            FileExtStr = ".xls": FileFormatNum = 56        Else            FileExtStr = ".xlsx": FileFormatNum = 51        End If    End If    [COLOR=#000000]'Change ScreenUpdating, Calculation, EnableEvents, ....[/COLOR]    With Application        CalcMode = .Calculation        .Calculation = xlCalculationManual        .ScreenUpdating = False        .EnableEvents = False    End With    ViewMode = ActiveWindow.View    ActiveWindow.View = xlNormalView    ActiveSheet.DisplayPageBreaks = False    [COLOR=#000000]'Delete the sheet RDBLogSheet if it exists[/COLOR]    On Error Resume Next    Application.DisplayAlerts = False    Sheets("RDBLogSheet").Delete    Application.DisplayAlerts = True    On Error GoTo 0   [COLOR=#000000]' Add worksheet to copy/Paste the unique list[/COLOR]    Set ws2 = Worksheets.Add(After:=Sheets(Sheets.Count))    ws2.Name = "RDBLogSheet"   [COLOR=#000000]'Fill in the path\folder where you want the new folder with the files    'you can use also this "C:\Users\Ron\test"[/COLOR]    MyPath = Application.DefaultFilePath    [COLOR=#000000]'Add a slash at the end if the user forget it[/COLOR]    If Right(MyPath, 1) <> "\" Then        MyPath = MyPath & "\"    End If   [COLOR=#000000]'Create folder for the new files[/COLOR]    foldername = MyPath & Format(Now, "yyyy-mm-dd hh-mm-ss") & "\"    MkDir foldername    With ws2        [COLOR=#000000]'first we copy the Unique data from the filter field to ws2[/COLOR]        My_Range.Columns(FieldNum).AdvancedFilter _                Action:=xlFilterCopy, _                CopyToRange:=.Range("A3"), Unique:=True        [COLOR=#000000]'loop through the unique list in ws2 and filter/copy to a new sheet[/COLOR]        Lrow = .Cells(Rows.Count, "A").End(xlUp).Row        For Each cell In .Range("A4:A" & Lrow)            [COLOR=#000000]'Filter the range[/COLOR]            My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _             Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")            [COLOR=#000000]'Check if there are no more then 8192 areas(limit of areas)[/COLOR]            CCount = 0            On Error Resume Next            CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _                     .Areas(1).Cells.Count            On Error GoTo 0            If CCount = 0 Then                MsgBox "There are more than 8192 areas for the value : " & cell.Value _                     & vbNewLine & "It is not possible to copy the visible data." _                     & vbNewLine & "Tip: Sort your data before you use this macro.", _                       vbOKOnly, "Split in worksheets"            Else                [COLOR=#000000]'Add new workbook with one sheet[/COLOR]                Set WSNew = Workbooks.Add(xlWBATWorksheet).Worksheets(1)                [COLOR=#000000]'Copy/paste the visible data to the new workbook[/COLOR]                My_Range.SpecialCells(xlCellTypeVisible).Copy                With WSNew.Range("A1")                    [COLOR=#000000]' Paste:=8 will copy the columnwidth in Excel 2000 and higher                    ' Remove this line if you use Excel 97[/COLOR]                    .PasteSpecial Paste:=8                    .PasteSpecial xlPasteValues                    .PasteSpecial xlPasteFormats                    Application.CutCopyMode = False                    .Select                End With                [COLOR=#000000]'Save the file in the new folder and close it[/COLOR]                On Error Resume Next                WSNew.Parent.SaveAs foldername & _                                    cell.Value & FileExtStr, FileFormatNum                If Err.Number > 0 Then                    Err.Clear                    ErrNum = ErrNum + 1                    WSNew.Parent.SaveAs foldername & _                     "Error_" & Format(ErrNum, "0000") & FileExtStr, FileFormatNum                    .Cells(cell.Row, "B").Formula = "=Hyperlink(""" & foldername & _                      "Error_" & Format(ErrNum, "0000") & FileExtStr & """)"                    .Cells(cell.Row, "A").Interior.Color = vbRed                Else                    .Cells(cell.Row, "B").Formula = _                    "=Hyperlink(""" & foldername & cell.Value & FileExtStr & """)"                End If                WSNew.Parent.Close False                On Error GoTo 0            End If            [COLOR=#000000]'Show all the data in the range[/COLOR]            My_Range.AutoFilter Field:=FieldNum        Next cell        .Cells(1, "A").Value = "Red cell: can't use the Unique name as file name"        .Cells(1, "B").Value = "Created Files (Click on the link to open a file)"        .Cells(3, "A").Value = "Unique Values"        .Cells(3, "B").Value = "Full Path and File name"        .Cells(3, "A").Font.Bold = True        .Cells(3, "B").Font.Bold = True        .Columns("A:B").AutoFit    End With   [COLOR=#000000]'Turn off AutoFilter[/COLOR]    My_Range.Parent.AutoFilterMode = False    If ErrNum > 0 Then        MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _             & vbNewLine & "There are characters in the name that are not allowed" _             & vbNewLine & "in a sheet name or the worksheet already exist."    End If    [COLOR=#000000]'Restore ScreenUpdating, Calculation, EnableEvents, ....[/COLOR]    My_Range.Parent.Select    ActiveWindow.View = ViewMode    ws2.Select    With Application        .ScreenUpdating = True        .EnableEvents = True        .Calculation = CalcMode    End WithEnd SubFunction LastRow(sh As Worksheet)    On Error Resume Next    LastRow = sh.Cells.Find(What:="*", _                            After:=sh.Range("A1"), _                            Lookat:=xlPart, _                            LookIn:=xlValues, _                            SearchOrder:=xlByRows, _                            SearchDirection:=xlPrevious, _                            MatchCase:=False).Row    On Error GoTo 0End Function
</PRE>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
any helpfull code?
Maybe, just maybe solution would be to create PivotTable and then export to unique workbooks filtered data?
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,393
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top