I am trying to ddo the following:
Main Workbook has following dataheaders
LoanID SM Region data2
1. First I am get the unique values of each region
2. Then apply auto filter to get only rows for first unique region
3. Create a new workbook
4. Paste the rows from step 2
5. Get the unique values for each SM (now its only within that region)
6. Create worksheets within that workbook by SM
7. Once all the SM's are done save that workbook with Region name.
8. Start with workbook for the next region as in step 1
Continue this till workbooks for all region are created and saved.
I am getting error in the outermost loop.
Please find the code as follows:
=====
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CreateRegion()
<SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> ThisSheet <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> InnerSheet <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> Wk <SPAN style="color:#00007F">As</SPAN> Workbook
<SPAN style="color:#00007F">Dim</SPAN> Region <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> SM <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ThisSheet = ActiveSheet
<SPAN style="color:#00007F">With</SPAN> ThisSheet
<SPAN style="color:#007F00">'Turn off AutoFilters</SPAN>
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Filter out all unique Region _
and put them in Column E</SPAN>
.Columns(3).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:= _
.Range("E1"), Unique:=<SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Loop through and filter by all unique Regions _
then create a worbbook named after each Region.</SPAN>
<SPAN style="color:#007F00">'Then copy the data for that Region to it's sheet</SPAN>
<SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> WorksheetFunction.CountA(.Columns("E:E"))
Region = .Cells(i, 5)
<SPAN style="color:#007F00">'Turn on Autofilter if needed</SPAN>
<SPAN style="color:#00007F">If</SPAN> .AutoFilterMode = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> .Rows(1).AutoFilter
<SPAN style="color:#007F00">'Filter down by Region and copy visible cells _
of columns A:D</SPAN>
.Rows(1).AutoFilter Field:=3, Criteria1:=Region
.Columns("A:D").SpecialCells(xlVisible).Copy
<SPAN style="color:#007F00">'Adding new workbook for each region</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Wk = Workbooks.Add
Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Paste new region level data dump on sheet</SPAN>
ActiveSheet.Paste
<SPAN style="color:#007F00">'New Region level data manipulation starts in workbook</SPAN>
<SPAN style="color:#00007F">With</SPAN> ThisWorkbook.ActiveSheet
<SPAN style="color:#007F00">'Turn off AutoFilters</SPAN>
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Filter out all unique Sales Managers _
and put them in Column E</SPAN>
.Columns(2).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:= _
.Range("F1"), Unique:=<SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Loop through and filter by all unique Sales Manager _
then create a worksheet named after each Sales Manager.</SPAN>
<SPAN style="color:#007F00">'Then copy the data for that Sales Manager to it's sheet</SPAN>
<SPAN style="color:#00007F">For</SPAN> j = 2 <SPAN style="color:#00007F">To</SPAN> WorksheetFunction.CountA(.Columns("E:E"))
SM = .Cells(i, 5)
<SPAN style="color:#007F00">'Turn on Autofilter if needed</SPAN>
<SPAN style="color:#00007F">If</SPAN> .AutoFilterMode = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> .Rows(1).AutoFilter
<SPAN style="color:#007F00">'Filter down by Sales Manager and copy visible cells _
of columns A:D</SPAN>
.Rows(1).AutoFilter Field:=2, Criteria1:=SM
.Columns("A:D").SpecialCells(xlVisible).Copy
<SPAN style="color:#007F00">'Add a sheet named after SM</SPAN>
Sheets.Add().Name = SM
<SPAN style="color:#007F00">'Paste new data on sheet</SPAN>
ActiveSheet.Paste
<SPAN style="color:#00007F">Next</SPAN> j
Wk.SaveAs Filename:="C:/" & Region
ActiveWorkbook.Close <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Here is here I get error (at Next i) - Compile Error: <SPAN style="color:#00007F">Next</SPAN> without for</SPAN>
Next i
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ThisSheet = <SPAN style="color:#00007F">Nothing</SPAN>
Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>=====
I hope someone can get this working.
Thanks in advance.
- Nick
Main Workbook has following dataheaders
LoanID SM Region data2
1. First I am get the unique values of each region
2. Then apply auto filter to get only rows for first unique region
3. Create a new workbook
4. Paste the rows from step 2
5. Get the unique values for each SM (now its only within that region)
6. Create worksheets within that workbook by SM
7. Once all the SM's are done save that workbook with Region name.
8. Start with workbook for the next region as in step 1
Continue this till workbooks for all region are created and saved.
I am getting error in the outermost loop.
Please find the code as follows:
=====
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CreateRegion()
<SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> j <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> ThisSheet <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> InnerSheet <SPAN style="color:#00007F">As</SPAN> Worksheet
<SPAN style="color:#00007F">Dim</SPAN> Wk <SPAN style="color:#00007F">As</SPAN> Workbook
<SPAN style="color:#00007F">Dim</SPAN> Region <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
<SPAN style="color:#00007F">Dim</SPAN> SM <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ThisSheet = ActiveSheet
<SPAN style="color:#00007F">With</SPAN> ThisSheet
<SPAN style="color:#007F00">'Turn off AutoFilters</SPAN>
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Filter out all unique Region _
and put them in Column E</SPAN>
.Columns(3).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:= _
.Range("E1"), Unique:=<SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Loop through and filter by all unique Regions _
then create a worbbook named after each Region.</SPAN>
<SPAN style="color:#007F00">'Then copy the data for that Region to it's sheet</SPAN>
<SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> WorksheetFunction.CountA(.Columns("E:E"))
Region = .Cells(i, 5)
<SPAN style="color:#007F00">'Turn on Autofilter if needed</SPAN>
<SPAN style="color:#00007F">If</SPAN> .AutoFilterMode = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> .Rows(1).AutoFilter
<SPAN style="color:#007F00">'Filter down by Region and copy visible cells _
of columns A:D</SPAN>
.Rows(1).AutoFilter Field:=3, Criteria1:=Region
.Columns("A:D").SpecialCells(xlVisible).Copy
<SPAN style="color:#007F00">'Adding new workbook for each region</SPAN>
<SPAN style="color:#00007F">Set</SPAN> Wk = Workbooks.Add
Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Paste new region level data dump on sheet</SPAN>
ActiveSheet.Paste
<SPAN style="color:#007F00">'New Region level data manipulation starts in workbook</SPAN>
<SPAN style="color:#00007F">With</SPAN> ThisWorkbook.ActiveSheet
<SPAN style="color:#007F00">'Turn off AutoFilters</SPAN>
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#007F00">'Filter out all unique Sales Managers _
and put them in Column E</SPAN>
.Columns(2).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:= _
.Range("F1"), Unique:=<SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Loop through and filter by all unique Sales Manager _
then create a worksheet named after each Sales Manager.</SPAN>
<SPAN style="color:#007F00">'Then copy the data for that Sales Manager to it's sheet</SPAN>
<SPAN style="color:#00007F">For</SPAN> j = 2 <SPAN style="color:#00007F">To</SPAN> WorksheetFunction.CountA(.Columns("E:E"))
SM = .Cells(i, 5)
<SPAN style="color:#007F00">'Turn on Autofilter if needed</SPAN>
<SPAN style="color:#00007F">If</SPAN> .AutoFilterMode = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> .Rows(1).AutoFilter
<SPAN style="color:#007F00">'Filter down by Sales Manager and copy visible cells _
of columns A:D</SPAN>
.Rows(1).AutoFilter Field:=2, Criteria1:=SM
.Columns("A:D").SpecialCells(xlVisible).Copy
<SPAN style="color:#007F00">'Add a sheet named after SM</SPAN>
Sheets.Add().Name = SM
<SPAN style="color:#007F00">'Paste new data on sheet</SPAN>
ActiveSheet.Paste
<SPAN style="color:#00007F">Next</SPAN> j
Wk.SaveAs Filename:="C:/" & Region
ActiveWorkbook.Close <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#007F00">'Here is here I get error (at Next i) - Compile Error: <SPAN style="color:#00007F">Next</SPAN> without for</SPAN>
Next i
.AutoFilterMode = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">Set</SPAN> ThisSheet = <SPAN style="color:#00007F">Nothing</SPAN>
Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>=====
I hope someone can get this working.
Thanks in advance.
- Nick