Solved

Page Break via VBA

Posted on 2011-03-22
5
383 Views
Last Modified: 2012-05-11
I need to add a page break in my VBA code.

I need to insert it when it finds the last value of column E that begins with OV. Once it finds the last row with that beginning value then I need to add a page break. How can I do this?
0
Comment
Question by:Lawrence Salvucci
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35188756
Try this. Do remember to replace "Sheet1" with the respective sheet name.

Sub Sample()
    Dim i As Long, LastRow As Long
    
    LastRow = Sheets("Sheet1").Range("E" & Rows.Count).End(xlUp).Row
    
    For i = LastRow To 1 Step -1
        If UCase(Left(Sheets("Sheet1").Range("E" & i).Value, 2)) = "OV" Then
            Sheets("Sheet1").Range("E" & i + 1).Select
            ActiveWindow.SelectedSheets.HPageBreaks.Add before:=ActiveCell
            ActiveWindow.SelectedSheets.VPageBreaks.Add before:=ActiveCell
            Exit Sub
        End If
    Next i
End Sub

Open in new window


Sid
0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35188813
It's faster to use excel's search within vba to search up for "OV".
0
 
LVL 1

Author Comment

by:Lawrence Salvucci
ID: 35188867
Can you explain what you mean about Excel's search within VBA?
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35188874
Yes you are right and I have been a biggest advocate of .Find(). However when you need to find just one last value in the column then the above loop will also do :)

Sid
0
 
LVL 1

Author Closing Comment

by:Lawrence Salvucci
ID: 35188928
Thank you for the quick answer! Much appreciated!
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question