I have the code below, im trying to make my selection of cells print on one a4 page rather than spread across 6 in a command button that is on a sheet, can anyone point out what im doing wrong? I expected to be able to have a printing window like with saving a sheet or something like that, but this is all i can find. thanks a tonne for your help :)
Private Sub CommandButton3_Click()
Sheets("Home").PageSetup.PrintArea = "$A$1:$W$44"
Sheets("Home").PageSetup.Orientation = xlLandscape
Sheets("Home").PageSetup.FitToPagesWide = 1
Sheets("Home").PageSetup.FitToPagesTall = 1
Sheets("Home").PrintPreview (EnableChanges = True)
'Sheets("Home").PrintOut (Preview = True)
End Sub
Ive looked for other some VBA online and looked up the pagesetup, but i cant seem to find what im after, the code above is the closest ive found, but it makes no difference to the printing process.
Is this what you are trying? I have commented the relevant parts of the code. Let me know if you still have any questions.
Option Explicit
Sub Sample()
With ThisWorkbook.Sheets("Home")
With .PageSetup
.PrintArea = "$A$1:$W$44"
.Orientation = xlLandscape
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
'~~> To fit text spanning in different pages change the view
ActiveWindow.View = xlPageBreakPreview
'~~> Extend the Horizontal Pagebreak to extreme right
'~~> This mimics what you do when you are dragging the PgBrk
If .HPageBreaks.Count > 0 Then _
.HPageBreaks(1).DragOff Direction:=xlDown, RegionIndex:=1
'~~> Similarly extend the Vertical Pagebreak to extreme down
If .VPageBreaks.Count > 0 Then _
.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1
.PrintPreview EnableChanges:=True
'~~> Change the view back to normal
ActiveWindow.View = xlNormalView
End With
End Sub
BEFORE
and the print preview
AFTER
Related
I am working with 5 different buttons that have a single Icon visible in each Button Group. All in 1 Module (If that matters) to hide/unhide row ranges.
So far I have this, but I get a 438 error. I was thinking that I would stack the buttons and toggle them. But I would rather just toggle the icon within the Group.
BTW: I am using Text (No Fill) as the parameter because I don't know another way.
There really isn't much out there that talks about working with Shape Buttons that I can find.
Sub HideInactive()
Call TurnOffStuff 'sub that turns off calculation and screenUpdating
With ActiveSheet.Shapes("HideInactive_Button1")
If .TextFrame.Characters.Text = "Hide" Then 'invisible text on the button
.Shapes("HideInactive_Button2") = HIDE 'Hides the "show" button
StartRow = 13
EndRow = 250
ColNum = 10
For i = StartRow To EndRow
If Cells(i, ColNum).Value = "0" Then
Rows(i).EntireRow.Hidden = True
Else
Rows(i).EntireRow.Hidden = False
End If
Next i
.TextFrame.Characters.Text = "Show"
.Shapes("HideInactive_Button1").Visible = HIDE 'Exposes the "show" button
Range("J13").Select 'moves the active cell to the top of the range
Else
Rows("13:250").EntireRow.Hidden = False 'resets the range view
.TextFrame.Characters.Text = "Hide"
.Shapes.Range("HideInactive_Button2").Visible = HIDE 'Exposes "hide" button
Range("J13").Select
End If
End With
HIDE = Not (HIDE)
Call TurnOnStuff
End Sub
I need something that I can reuse for many buttons just by renaming the shapes and the ranges.
Thank you for the help. I appreciate your time!
I have created a multi-sheet financial file containing 1 sheet for each month. The data on each sheet is exactly the same size and shape (A1 to V186). Each sheet contains 2 print program buttons; one to print a detail report, and the other to print a summary report. The same 2 buttons are on each sheet running the same 2 programs and use variables to get the code name and index number of the active sheet. The summary print uses A4 Portrait, no page breaks and works fine; but the A3 Landscape 2 page breaks in the detail report are a real problem for me to solve. I have spent 2 full days researching and trying all different suggested solutions to get the page breaks to work,(Location, Before, etc etc) and I just can't work it out. Your help would be greatly appreciated.
Sub Print_Month_Detail()
'
' Print_Month_Detail Macro
'
Dim CShtIdx As Integer
Dim SheetName As String
CShtIdx = ActiveSheet.Index
SheetName = ActiveSheet.CodeName
Worksheets(CShtIdx).Activate
ActiveWindow.View = xlPageBreakPreview
Worksheets(CShtIdx).ResetAllPageBreaks
Set Worksheets(CShtIdx).HPageBreaks(1).Location = Worksheets(CShtIdx).Range("A70")
Set Worksheets(CShtIdx).HPageBreaks(2).Location = Worksheets(CShtIdx).Range("A138")
'
Application.PrintCommunication = False
ActiveSheet.PageSetup.PrintArea = "$A$1:$V$186"
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$2"
.PrintTitleColumns = ""
' ...... more print settings.......
End With
'
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Preview:=True, Collate:=True, IgnorePrintAreas:=False
Range("A1").Select
End Sub
I am using this code
Sub print_area()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws.PageSetup
.PrintTitleColumns = "$A:$E"
.PrintArea = ws.Range("A128").Value
.CenterHorizontally = True
End With
Next
End Sub
Range("A128").Value has the value as this: $F$1:$AF$125,$AG$1:$BE$125,$BF$1:$CD$125,$CE$1:$DA$125,$DB$1:$DX$125
print areas are not set properly (the areas are close to set range but not as desired), what other parameters do I have to set to make it work right?
Red arrow indicates where the print area should be
As a rule, Excel automatically sets page breaks for printing based on paper size, scale, specified number of sheets in width and height, page orientation, and other parameters. You can use VPageBreak object and HPageBreak object in combination with a number of .PageSetup properties to manually fit page breaks. Note that you cannot set page breaks with the .PrintArea property (see below in the code for why). In the following code I set page breaks after the cells "AF1", "BE1", "CD1", "DA1", "DX1":
Sub print_area()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws.PageSetup
.PrintTitleColumns = "$A:$E"
.PrintArea = "$F$1:$AF$125,$AG$1:$BE$125,$BF$1:$CD$125,$CE$1:$DA$125,$DB$1:$DX$125" ' there will be one area between the upper left and lower right cells
Debug.Print .PrintArea ' check the final .PrintArea; prints $F$1:$DX$125
.Zoom = False
.Orientation = xlLandscape
.FitToPagesWide = False 'it's Auto
.FitToPagesTall = 1
.CenterHorizontally = True
ws.ResetAllPageBreaks
breaks = Array("AF1", "BE1", "CD1", "DA1", "DX1") 'zero-based array
For i = 1 To UBound(breaks)
ws.VPageBreaks.Add Before:=ws.Range(breaks(i - 1)).Offset(, 1)
Next
End With
Next
End Sub
Please note that in response to your pagination actions, Excel can
(and usually does) change the pagination itself
I have a dynamic table with 3 rows as the header and then some general remarks underneath the table. I want the header rows to repeat on each page on which the table appears but if the last page only contains the general remarks, then do not repeat the header rows on the last page.
I've set a logic to check if the last page only contains the general remarks but on printing to PDF, page 1 is printed twice and page 2 is not printed (if the worksheet contains 2 pages).
Can anyone help advise how this issue can be resolved? Thanks.
I've tried to add "Application.PrintCommunication = True" and "Worksheets("Quotation").Activate" but it seems that they are of no help.
Sub print_with_repeated_rows()
Call Precheck
Dim active_sheet As Variant, page_nums As Variant
Set active_sheet = ThisWorkbook.ActiveSheet
page_nums = active_sheet.PageSetup.Pages.count
If Last_page_need_title_row Then
active_sheet.PrintOut From:=1, To:=page_nums
Else
On Error Resume Next
Application.PrintCommunication = True
Err.Clear
Worksheets("Quotation").Activate
With ActiveSheet.PageSetup
.PrintTitleRows = "Print_Titles"
ActiveSheet.PrintOut From:=1, To:=page_nums - 1
.PrintTitleRows = ""
ActiveSheet.PrintOut From:=page_nums, To:=page_nums
End With
End If
End Sub
I have the following code that exports the selected range in the worksheet as .pdf file:
'More coding above
With Sheet7
If (CheckBox1.Value = True And CheckBox2.Value = True) Then
.PageSetup.PrintArea = "A8:M80"
ElseIf (CheckBox1.Value = True And CheckBox2.Value = False) Then
.PageSetup.PrintArea = "A8:M55"
ElseIf (CheckBox1.Value = False And CheckBox2.Value = True) Then
.PageSetup.PrintArea = "A8:M32, A56:M80"
Else
MsgBox 'At least one option must be selected!'
Exit Sub
End If
End With
'More coding below
However, when only CheckBox2 is checked, the file is generated selecting only the areas as set by If/Else, but still showing the A33:M55 gap between ranges.
Is there anyway I could suppress this gap? I want the code to print both ranges as if they were one.
I tried the Union method, but it gives me the same result.
Any help will be greatly appreciated!
The simplest way is to hide unnecesarry rows if it's possible for a moment before print to PDF. In this way you should make sure after macro all rows are visible (additional you can use On Error GoTo and unhide just in case)