Greetings AlphaFrog,
The code below is only parts of a bigger program that pulls apart the different sections of the monthly report, which is thousands of rows long. Most of my code for that part came from this site, and works great, except the print area and page breaking. Before switching to excel 2007 from 2003 I had no problem with the print area and page breaks as I would fix the main report before splitting it out and the print area and page breaks would carry over to the separated sections.
All my code is in it's own workbook with a button that activates the process. (ie: Opening the monthly report which becomes the active sheet.)
When changing that piece of code I get the runtime error.
Highlighted line for runtime error is:
Code:
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
Test code:
Code:
Option Explicit
Sub mReportOpen()
'User must find MonthlyReport.xlsx
Dim fileToOpen As Variant
fileToOpen = Application _
.GetOpenFilename("Text Files (*.*), *.*")
If fileToOpen <> False Then
Workbooks.OpenText Filename:=fileToOpen
' MsgBox "Open " & fileToOpen
End If
Call mReportSetup
End Sub
Sub SaveWk()
'Save
ActiveWorkbook.Save
End Sub
Sub CloseWk()
'Close
ActiveWorkbook.Close
End Sub
Sub mReportSetup()
Rows("1:1").Select
Selection.AutoFilter
Columns("C:C").Select
Selection.ColumnWidth = 50
With ActiveSheet.PageSetup
.LeftHeader = ""
.CenterHeader = ""
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = ""
.RightFooter = ""
.LeftMargin = Application.InchesToPoints(0.5)
.RightMargin = Application.InchesToPoints(0)
.TopMargin = Application.InchesToPoints(0)
.BottomMargin = Application.InchesToPoints(0)
.HeaderMargin = Application.InchesToPoints(0)
.FooterMargin = Application.InchesToPoints(0)
.PrintHeadings = False
.PrintGridlines = False
.PrintComments = xlPrintNoComments
.PrintQuality = 600
.CenterHorizontally = False
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperLetter
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = 70
.PrintErrors = xlPrintErrorsDisplayed
End With
ActiveSheet.PageSetup.PrintArea = "C2:N" & Cells.Find("*", SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Call mReportPBreak
End Sub
Sub mReportPBreak()
Dim myStr1 As String
Dim c1 As Range
Dim firstAddress As Variant
Dim Answer1 As Integer
myStr1 = "TopOfPage"
With ActiveSheet.Range("C:C")
Set c1 = .Find(myStr1, LookIn:=xlValues)
If Not c1 Is Nothing Then
firstAddress = c1.Address
Do
Cells.FindNext(After:=ActiveCell).Activate
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
Set c1 = .FindNext(c1)
On Error GoTo ErrHand1
Loop While Not c1 Is Nothing And c1.Address <> firstAddress
End If
End With
ErrHand1:
Answer1 = MsgBox("Setup Complete!", _
vbOKCancel + vbQuestion, "Monthly Report")
If Answer1 = vbCancel Then
Call CloseWk
Exit Sub
Else
Range("A1").Select
ActiveWindow.SmallScroll ToRight:=0
Call SaveWk
Call CloseWk
End If
End Sub
The below screenshot is just a sample of the monthly report. The issues with it are:
-Report to print has blank rows
-Report changes in length each month, but not width
-Report is all one column and cannot be edited without losing formatting
-I manually adjust the numbers along the side each month (Another issues I'd like to work on later)
note: To to the information contained in this monthly report I can not edit the report to fit multiple columns.
The numbers along the side represent the fund and center numbers for the monthly report. These are not real numbers just something to show what I have to get done.
fund 22222
center 1000
center 1010
fund 33333
center 1000
fund 44444
center 1000
center 1001
fund 55555
center 1000
center 90000
Sorry I haven't been able to get the spreadsheet maker working, wish I could just attach the sample file I have.
Please let me know if you need any more explanation.
Thanks for any help you can provide.
-malvdh