John,
As I looked at the code above it is not quite what I am looking for. The code below is actually from the Mr.Excel book VBA and Macros for Microsoft Excel. The code is creating a report for each customer in a data set. I would like to create two reports or more for each customer using the same source data but reporting on it differently in different worksheets. How can this code be modified to do so? Do I have to set up different For Each... loops or include all the advanced filter methods within one loop? I can usually find code out that serves my basic purpose and modify to fit my needs but I have not found any postings concerning this issue. Thanks, Phil
Sub RunReportForEachCustomer()
' Page 231 through 233
Dim IRange As Range
Dim ORange As Range
Dim CRange As Range
Dim WBN As Workbook
Dim WSN As Worksheet
Dim WSO As Worksheet
' Since this is called from a button on Menu,
' first select the sample data sheet
Worksheets("SalesReport").Select
' Clear out results of previous macros
Range("J1:AZ1").EntireColumn.Delete
Set WSO = ActiveSheet
' Find the size of today's dataset
FinalRow = Cells(65536, 1).End(xlUp).Row
NextCol = Cells(1, 255).End(xlToLeft).Column + 2
' First - get a unique list of customers in J
' Set up output range. Copy heading from D1 there
Range("D1").Copy Destination:=Cells(1, NextCol)
Set ORange = Cells(1, NextCol)
' Define the Input Range
Set IRange = Range("A1").Resize(FinalRow, NextCol - 2)
' Do the Advanced Filter to get unique list of customers
IRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:="", CopyToRange:=ORange, Unique:=True
FinalCust = Cells(65536, NextCol).End(xlUp).Row
' Loop through each customer
For Each cell In Cells(2, NextCol).Resize(FinalCust - 1, 1)
ThisCust = cell.Value
' Set up the Criteria Range with one customer
Cells(1, NextCol + 2).Value = Range("D1").Value
Cells(2, NextCol + 2).Value = ThisCust
Set CRange = Cells(1, NextCol + 2).Resize(2, 1)
' Set up output range. We want Date, Quantity, Product, Revenue
' These columns are in C, E, B, and F
Cells(1, NextCol + 4).Resize(1, 4).Value = Array(Cells(1, 3), Cells(1, 5), Cells(1, 2), Cells(1, 6))
Set ORange = Cells(1, NextCol + 4).Resize(1, 4)
' Do the Advanced Filter to get unique list of customers & product
IRange.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=CRange, CopyToRange:=ORange
' Create a new workbook with one blank sheet to hold the output
Set WBN = Workbooks.Add(xlWBATWorksheet)
Set WSN = WBN.Worksheets(1)
' Set up a title on WSN
WSN.Cells(1, 1).Value = "Report of Sales to " & ThisCust
' Copy data from WSO to WSN
WSO.Cells(1, NextCol + 4).CurrentRegion.Copy Destination:=WSN.Cells(3, 1)
TotalRow = WSN.Cells(65536, 1).End(xlUp).Row + 1
WSN.Cells(TotalRow, 1).Value = "Total"
WSN.Cells(TotalRow, 2).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
WSN.Cells(TotalRow, 4).FormulaR1C1 = "=SUM(R2C:R[-1]C)"
' Format the new report with bold
WSN.Cells(3, 1).Resize(1, 4).Font.Bold = True
WSN.Cells(TotalRow, 1).Resize(1, 4).Font.Bold = True
WSN.Cells(1, 1).Font.Size = 18
WBN.SaveAs "C:\" & ThisCust & ".xls"
WBN.Close SaveChanges:=False
WSO.Select
Set WSN = Nothing
Set WBN = Nothing
' clear the output range, etc.
Cells(1, NextCol + 2).Resize(1, 10).EntireColumn.Clear
Next cell
Cells(1, NextCol).EntireColumn.Clear
MsgBox FinalCust - 1 & " Reports have been created!"
End Sub