Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA

Posted on 2015-01-07
8
Medium Priority
?
81 Views
Last Modified: 2015-01-20
I have written a VBA that extract records from database, ranged from column A to H with few hunderds of rows.

How to configure the page break with VBA such that
- column A - H always fix in a A4 width
- a single A4 size will have 40 rows only , and page break to a new page.

Tks
0
Comment
Question by:AXISHK
[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
  • 4
  • 4
8 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40537393
Hi,

pls try


Sub Macro()
Set sh = ActiveSheet

sh.PageSetup.Zoom = False
sh.ResetAllPageBreaks
sh.PageSetup.Orientation = xlLandscape
Rounding = 40
LastRowRounded = WorksheetFunction.MRound(Range("A" & Cells.Rows.Count).End(xlUp).Row, Rounding)
sh.PageSetup.PrintArea = "A1:H" & LastRowRounded
sh.PageSetup.FitToPagesWide = 1
sh.PageSetup.FitToPagesTall = LastRowRounded / Rounding
 
sh.PrintPreview
Set sh = Nothing
End Sub

Open in new window

Regards
0
 

Author Comment

by:AXISHK
ID: 40557597
Tks. It can't really fit into the row that I need, say every 1-80, 81-160,161- 240 and 241-320 into separate page. Any idea ?

Tks
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40557641
change line 7 to

Rounding = 80
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:AXISHK
ID: 40557652
Doesn't help... Tks
0
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 40557675
Could you be more precise?
0
 

Author Comment

by:AXISHK
ID: 40559154
I have extracted a coding from my marco. Base on the calculation, page break should occur every 80 rows but it doesn't. Any idea ?

Tks



Rounding = iNumberOfRow * lblHeight
   80 =    4   x 20


LastRowRounded = iTotalRecord / iNumberOfCol * lblHeight
      300 =     60 / 4 *20

stPrintArea = Cells(1, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False) & ":" & Cells(LastRowRounded, iNumberOfCol * lblWidth).Address(RowAbsolute:=False, ColumnAbsolute:=False)
                                                                                                               (4 * 3)
wksheet.PageSetup.FitToPagesWide = 1
wksheet.PageSetup.FitToPagesTall = LastRowRounded / Rounding
                                       300 / 80
'wksheet.PrintPreview
Set wksheet = Nothing
Test.xlsx
0
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 40559211
pls try

Sub Macro()
Set sh = ActiveSheet

sh.ResetAllPageBreaks
sh.PageSetup.Zoom = 25

sh.PageSetup.Orientation = xlLanscape
Rounding = 40
LastRowRounded = WorksheetFunction.RoundUp(Range("A" & Cells.Rows.Count).End(xlUp).Row / Rounding, 0) * Rounding
sh.PageSetup.PrintArea = "A1:L" & LastRowRounded
For Idx = 1 To LastRowRounded / Rounding - 1
    myRow = Rounding * Idx + 1
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Range("A" & myRow)
Next
    sh.PageSetup.Zoom = False
    sh.PageSetup.FitToPagesWide = 1
    sh.PageSetup.FitToPagesTall = False
 
sh.PrintPreview
Set sh = Nothing
End Sub

Open in new window

TestV1.xlsm
0
 

Author Closing Comment

by:AXISHK
ID: 40561245
That's work perfect. Tks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

609 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