PDA

View Full Version : vba print 2 ranges in one page



MITSARAS73
01-23-2023, 07:20 AM
Hello,

I wrote this code but when I print it prints one range in a page and next range in another page on printer.
Can you help me coorect the code to print all 2 ranges in only one page?
Thanks for your time.


Sub CustomPA()
Dim ws As Worksheet
Dim yRng1 As Range
Set ws = ThisWorkbook.Sheets("POS")
wslR = ws.Cells(Rows.Count, 6).End(xlUp).Row
wsLC = ws.Cells(1, Columns.Count).End(xlToLeft).Column
Set yRng1 = Sheets("pos").Range("k19:m24")
Set printA = ws.Range("f2:h" & wslR)
With ActiveSheet.PageSetup
.PrintArea = printA.Address(0, 0) & "," & yRng1.Address
ActiveWindow.SelectedSheets.PrintOut
End With
Range("F2").Select
End Sub

rollis13
01-23-2023, 08:20 AM
This could be a solution for you since your first range is a fixed area:
Option ExplicitSub CustomPA()
Dim ws As Worksheet
Dim wsLR As Long
Set ws = ThisWorkbook.Sheets("POS")
wsLR = ws.Cells(Rows.Count, 6).End(xlUp).Row
Sheets.Add 'add a help sheet
ws.Range("K19:M24").Copy Range("A1") 'copy to the help sheet
ws.Range("F2:H" & wsLR).Copy Range("A8") 'copy to the help sheet
With ActiveSheet
.PageSetup.PrintArea = .UsedRange.Address
.PrintOut 'printout the help sheet
Application.DisplayAlerts = False
.Delete 'delete the help sheet
Application.DisplayAlerts = True
End With
ws.Range("F2").Select
End Sub

MITSARAS73
01-24-2023, 12:29 AM
Thank you very much for your help
Now it prints both ranges on the same page but in the first range cells shows no data but #ref!(There are formulas on these cells)
The second range is fine but the columns are not wide enough to show the data.

Thanks!!!

rollis13
01-24-2023, 03:40 AM
You could have worked on it yourself, you are the only one who knows the contents of your project. The topic of the thread was: 1 page against 2 pages.
So I'll try to guess once again; use the macro with these few changes:
Option ExplicitSub CustomPA()
Dim ws As Worksheet
Dim wsLR As Long
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("POS")
wsLR = ws.Cells(Rows.Count, 6).End(xlUp).Row
Sheets.Add 'add a help sheet
ws.Range("K19:M24").Copy 'copy to the help sheet
Range("A1").PasteSpecial Paste:=xlPasteValues
ws.Range("F2:H" & wsLR).Copy 'copy to the help sheet
Range("A8").PasteSpecial Paste:=xlPasteValues
Columns("A:C").EntireColumn.AutoFit 'autofit help sheet column widths
With ActiveSheet
.PageSetup.PrintArea = .UsedRange.Address
.PrintOut 'printout the help sheet
Application.DisplayAlerts = False
.Delete 'delete the help sheet
Application.DisplayAlerts = True
End With
Application.ScreenUpdating = True
ws.Range("F2").Select

End Sub

MITSARAS73
01-24-2023, 04:09 AM
Thanks alot for your help..You guess correct!!..Now everything is fine..I tried a lot of different ways to do that but i have no knowledges of vba...Everything I try to learn from internet...
Thank you again for your time..You are very kind.:yes:yes

rollis13
01-24-2023, 07:52 AM
Thanks for the positive feedback :thumb, glad having been of some help.