how to print table in two columns?

Asked

Viewed 45 times

-3

I have a table with more lines that fit on an A4 page, but if divided into two can be printed on one page, with two columns. I’ve checked other sites and got help, but it hasn’t. I attach the code I used in my attempt. If you can help me, I really appreciate it.


`Sub ImprimeLista()

'Imprime Lista do Mercado

Dim BlocPrint As Range
Dim FirstRow As Integer
Dim LastRow As Integer
Dim LastCol As Integer
  
    Sheets("Compras").Select
  
    Cells(1, 1).Select
  
    LastCol = ActiveSheet.Cells(4, 1).End(xlToRight).Column
    FirstRow = Selection.Row
    LastRow = Cells(Rows.Count, LastCol).End(xlUp).Row
  
    Set BlocPrint = Range(Cells(FirstRow, 1), Cells(LastRow, LastCol))

    With ActiveSheet.PageSetup
        BlocPrint.Select
        .FitToPagesWide = 1
        .FitToPagesTall = 2
        Selection.PrintOut Copies:=1, Collate:=True
        Range("A1").Select
    End With

End Sub
  • Hi Bautto! How are you? Please could you insert an image showing how your spreadsheet is structured, indicating, mainly, what is the last line filled in so I can help you? Thank you!

  • Hello, Andrea. Follow the link to the archive.

  • It has 2 spreadsheets, the first (Notary) is the data entry and the second (Purchases) is the one I need to print, which summarizes the items to be purchased. Thank you for your interest. https://www.sendspace.com/file/ivezqi

  • Hi! I tested the code on the spreadsheet you sent me and the table was printed on a single page. I tested it twice: in the first I did not change the default settings of the print and in the second I changed and in both cases the result was the same. Were you expecting a different result? https://www.sendspace.com/file/zula7n

  • The print comes out on a single page. What I want is that it comes out in two columns, because in one it is very reduced the print, making it difficult to read. I’m trying to do a routine to split the printing, for example, print the first 60 lines in one column and the rest in another, but I’m finding it difficult to configure the printer by VBA.

1 answer

0

Hi!

I did a slight gambit, but I believe it brings the desired result.

As it is not possible to adjust the print configuration to print two columns, my idea was to create an auxiliary tab in which the shopping list is copied and printed as desired.

Follows the code:

Sub ImprimeLista()

'Imprime Lista do Mercado

Dim BlocPrint As Range
Dim FirstRow As Integer
Dim LastRow As Integer
Dim LastCol As Integer
Dim W       As Worksheet
Dim WB      As Worksheet

Application.DisplayAlerts = False

    Sheets("Compras").Select
    
    Cells(1, 1).Select
  
    LastCol = ActiveSheet.Cells(4, 1).End(xlToRight).Column
    FirstRow = Selection.Row
    LastRow = Cells(Rows.Count, LastCol).End(xlUp).Row
  
       
    'Adiciona uma nova aba e copia a tabela para ela
    Sheets.Add After:=Sheets(Sheets.Count)
    ActiveSheet.Name = "Impressão"
    Set WB = Sheets("Impressão")
    Set W = Sheets("Compras")
    
    W.Select
    
    'Seleciona a primeira metade
    Set BlocPrint = W.Range(Cells(FirstRow, 1), Cells((LastRow / 2), LastCol))
    BlocPrint.Select
    Selection.Copy
    WB.Select
    WB.Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False
    Application.CutCopyMode = False
        
    'Seleciona a segunda metade
    W.Select
    Set BlocPrint = W.Range(Cells((LastRow / 2), 1), Cells((LastRow), LastCol))
    BlocPrint.Select
    
    Selection.Copy
    WB.Select
    WB.Range("G4").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False
    Application.CutCopyMode = False
    
    'Coloca um cabeçalho na segunda coluna
    WB.Range("A1:E3").Select
    Selection.Copy
    WB.Range("G1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("A1:E1").Select
    
    'Ajeita as colunas com produtos
    WB.Range("A:A").EntireColumn.AutoFit
    WB.Range("G:G").EntireColumn.AutoFit
    WB.Columns(6).ColumnWidth = 2
   
   
  'Configura a impressão
   With ActiveSheet.PageSetup
        WB.Select
        WB.Range("A:G").Select
'        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        Selection.PrintOut Copies:=1, Collate:=True
        Range("A1").Select
    End With
    
    'Seleciona a planilha Compras e exclui a planilha Impressão
    W.Select
    W.Range("A1").Select
    WB.Delete

Application.DisplayAlerts = True

End Sub


The end result was this: https://www.sendspace.com/file/6psv97

I hope you solve.

  • Andrea, good morning. I really liked your solution, mainly by using VBA. I had already arranged also an alternative, but what I did was very manual, transcribing the formula that compiles the list for the other half of the spreadsheet, and dividing the lines, without VBA programming. He did too, but his work was great. Grateful for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.