Solved

Excel VBA

Posted on 2015-01-07
8
69 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
  • 4
  • 4
8 Comments
 
LVL 48

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 48

Expert Comment

by:Rgonzo1971
ID: 40557641
change line 7 to

Rounding = 80
0
 

Author Comment

by:AXISHK
ID: 40557652
Doesn't help... Tks
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

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 48

Accepted Solution

by:
Rgonzo1971 earned 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now