View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
bdcrisp[_3_] bdcrisp[_3_] is offline
external usenet poster
 
Posts: 1
Default Looping through Ranges of Rows


this macro will generate a report for every 25 records (creates a ne
work book for each one, report1, report2, etc. and creates one for th
remainder records also) ive tested this one, so you can just copy an
paste it. look at sheet references however (thisworkbook.sheets(1
should be your data page)
good luck!

Sub MakeReports()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set datasheet = ThisWorkbook.Sheets(1)
counter = 1
headernum = 1
Do While Cells(counter + headernum, 1).Value < ""
counter = counter + 1
Loop
TotalRows = counter
fullreports = Int(TotalRows / 25)
remainder = TotalRows Mod 25
counter2 = 1
For counter = 1 To fullreports
t = "report" & counter
Set newBook = Workbooks.Add
With newBook
.Title = ""
.Subject = ""
.SaveAs Filename:="report" & counter & ".xls"
End With
Set currentreport = Workbooks("report" & counter & ".xls").Sheets(1)
For i = 1 To 25

datasheet.Activate
Cells(counter2, 1).EntireRow.Select
Selection.Copy
currentreport.Activate
currentreport.Cells(i, 1).Activate
ActiveSheet.Paste
counter2 = counter2 + 1
Next i
Next counter
Set newBook = Workbooks.Add
With newBook
.Title = ""
.Subject = ""
.SaveAs Filename:="report" & counter & ".xls"
End With
Set currentreport = Workbooks("report" & counter & ".xls").Sheets(1)
For i = 1 To remainder
datasheet.Activate
Cells(counter2, 1).EntireRow.Select
Selection.Copy
currentreport.Activate
currentreport.Cells(i, 1).Activate
ActiveSheet.Paste
counter2 = counter2 + 1
Next i
For i = 1 To fullreports
Workbooks("report" & i & ".xls").Save
Workbooks("report" & i & ".xls").Close
Next i
If remainder 0 Then
Workbooks("report" & fullreports + 1 & ".xls").Save
Workbooks("report" & fullreports + 1 & ".xls").Close
End If
If remainder 0 Then c = 1 Else c = 0
MsgBox "Reports completed - total: " & fullreports + c
End Su

-----------------------------------------------
~~ Message posted from http://www.ExcelTip.com
~~View and post usenet messages directly from http://www.ExcelForum.com

~~Now Available: Financial Statements.xls, a step by step guide to creating financial statements