Consulting

Results 1 to 6 of 6

Thread: vba print 2 ranges in one page

  1. #1

    vba print 2 ranges in one page

    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
    Last edited by Aussiebear; 01-23-2023 at 11:12 AM. Reason: Added code tags to supplied code

  2. #2
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    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

  3. #3
    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!!!

  4. #4
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    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

  5. #5
    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.

  6. #6
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Thanks for the positive feedback , glad having been of some help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •