Avatar of Norma Posy
Norma PosyFlag for United States of America

asked on 

Excel pagebreak from VB code

My app creates an Excel spreadsheet.
There are places where a page break is needed.
Code snippet:

Dim oXLApp As Object
Dim oXLBook As Object
Dim oXLSheet As Object

Set oXLApp = CreateObject("Excel.Application")
Set oXLBook = oXLApp.Workbooks.Add
Set oXLSheet = oXLBook.Worksheets(1)

If [condition is met] Then
   sRange = "A" & Trim(CStr(iRow))
   oXLSheet.HPagebreaks.Add Before:=oXLSheet.Range(sRange)
End If

Open in new window


I've stepped through the code, and [condition is met] is detected, and the if-then is executed. But nothing happens. No page break occurs.

Please help.
Visual Basic Classic

Avatar of undefined
Last Comment
Norma Posy
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Norma Posy
Norma Posy
Flag of United States of America image

ASKER

I'm working on it.
I will get back to you.
Meanwhile:
I was wondering if the sequence matters.
Maybe page breaks should be inserted AFTER the spreadsheet is populated.
I'm guessing that changing the contents of a cell might negate other attributes.
I'm going to experiment.
Avatar of Norma Posy
Norma Posy
Flag of United States of America image

ASKER

Here is a little test page break test.
Some numbers are placed in the first 6 rows of column A.
Then a page break is inserted after row 3:
Note: xl Constants are defined in a "constants" module.

Private Sub TestPageBreak_Click()
Dim iI As Integer
Dim sCellNumber As String
Dim oXLApp As Object
Dim oXLBook As Object
Dim oXLSheet As Object
Dim sWorkbookName As String
Dim sCurrentName As String
'
Set oXLApp = CreateObject("Excel.Application")
Set oXLBook = oXLApp.Workbooks.Add
Set oXLSheet = oXLBook.Worksheets(1)
sWorkbookName = "Testing.xls"
'
With oXLApp
    .Visible = True
    sCurrentName = .Workbooks.Add.Name
    With .Range("A1:F300")
        .Font.Name = "Courier New"
        .Font.Size = 10
    End With
    .Range("A1:A200").ColumnWidth = 5
    .Columns("A:A").Select
    .Selection.HorizontalAlignment = xlCenter
'
' Insert Cell Data
    For iI = 1 To 6
        sCellNumber = "A" & CStr(Trim(iI))
        .Range(sCellNumber).Value = iI
    Next iI
' Page Break
' (alternate try:)
'    oXLSheet.HPagebreaks.Add Before:=oXLSheet.Range("A4")
' using this one:
    oXLSheet.Rows(4).PageBreak = xlPageBreakManual
End With
'
Set oXLApp = Nothing
Set oXLBook = Nothing
Set oXLSheet = Nothing
End Sub

Open in new window


Two spreadsheets are created!
"Book1" Contains the page break
"Book2" Contains the data.

Please help.
Avatar of Norma Posy
Norma Posy
Flag of United States of America image

ASKER

I solved it myself.
Line 17 (code, above) is superfluous, and creates a second spreadsheet.

The link you supplied was interesting reading, though.
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo