Excel

Consolidate data layout for printing

Ease of Use

Easy

Version tested with

2003 

Submitted by:

mdmackillop

Description:

Consolidates a long list of data to continue on the same page to save paper or any other useful purpose. 

Discussion:

It may be convenient to store data in one or two columns, but if you need a printout of all records, its a bit wasteful of paper, or of time in manually resetting the layout. This code checks the number of rows and columns, the horizontal page break settings, and moves the data to suit. It incorporates a blank column between sets of sdata and attemps to set this column width to suit vertical page breaks. This adjustment is stated as a Constant, which may be adjusted by the user dependent upon outcome. 

Code:

instructions for use

			

Option Explicit 'Adjust BlankFactor to fit data to vertical page breaks Const BlankFactor = 9.5 Sub Splits() Dim Rw As Long, Rws As Long, Cols As Long, Sets As Long Dim i As Long, j As Long, k As Long Dim Data As Range, Pages As Long Dim PRange As Range Dim CRange As Range Dim MySheet As String Application.ScreenUpdating = False 'Select cell A1 and cells to the right; count columns; add for blanks Cells(1, 1).Select Set Data = Range(Cells(1, 1), Cells(1, 1).End(xlToRight)) Cols = Data.Columns.Count + 1 'Set Cols to 2 if single column of data only If Cols > 255 Then Cols = 2 Columns(Cols).ColumnWidth = 1 'Count rows and divide by pagebreak row to determine loops Rws = Range("A1").End(xlDown).Row() Rw = ActiveSheet.HPageBreaks(1).Location.Row() - 1 Sets = (Rws / Rw) + 1 If Cols * Sets > 255 Then Pages = (Cols * Sets) / 255 End If 'Create new page MySheet = ActiveSheet.Name ActiveSheet.Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = "Tmp" Cells.ClearContents 'Format new page With ActiveSheet .DisplayAutomaticPageBreaks = True .PageSetup.PrintArea = "$1:$1" For i = 1 To Cols For j = 0 To 255 - Cols Step Cols .Columns(j + i).ColumnWidth = .Columns(i).ColumnWidth Next j Next i End With 'Determine vertical page break locations SortCols Cols ActiveSheet.PageSetup.PrintArea = "" Sheets(MySheet).Activate 'Write data from initial column across pages i = 1 k = 0 Set CRange = Range(Cells(1, 1), Cells(Rw, Cols)) For j = 0 To Pages With Sheets("Tmp") Set PRange = Range(.Cells(j * Rw + 1, 1), .Cells(j * Rw + Rw, Cols)) Debug.Print PRange.Address End With PRange = CRange.Value For i = 1 To CInt(255 / Cols) - 1 Set CRange = CRange.Offset(Rw) Set PRange = PRange.Offset(, Cols) 'MsgBox PRange.Address PRange = CRange.Value Next Next j 'Tidy up Set Data = Nothing Application.ScreenUpdating = True End Sub Sub SortCols(Cols As Long) Dim BkCol As Long, VPos As Single, Pos As Single, Space As Single Dim i As Long, NewWidth As Single ActiveSheet.PageSetup.PrintArea = "$1:$1" 'Get position of first vertical break BkCol = ActiveSheet.VPageBreaks(1).Location.Column ActiveSheet.PageSetup.PrintArea = "" 'Get position in points VPos = Columns(BkCol).Left 'Get width of copied columns Pos = Columns(Cols + 1).Left 'Get points needed to fill page Space = VPos Mod Pos 'Increase required for each blank column NewWidth = Cells(1, Cols).ColumnWidth + Int(Space / (Int(VPos / Pos) + 1)) / BlankFactor 'Apply increase to blank columns For i = Cols To 255 Step Cols Cells(1, i).ColumnWidth = NewWidth Next End Sub

How to use:

  1. Open an Excel workbook
  2. Select Tools/Macro/Visual Basic Editor
  3. In the VBE window, select Tools/Project Explorer
  4. Select Insert/Module to add a standard code module
  5. Copy and paste the code into the Module
  6. Now select File/Close and Return To Microsoft Excel
  7. Save your changes and close the workbook...
 

Test the code:

  1. Enter multiple rows of data in columns A-C
  2. For checking purposes enter ascending numbers in one of the columns
  3. Add a horizontal page break or use an automatic break determined by page setup
  4. Set the Tools/Options to view page breaks (not essential)
  5. Run the Splits macro
 

Sample File:

ReOrder.zip 241.11KB 

Approved by mdmackillop


This entry has been viewed 444 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express