Setting Print Area

doumob

New Member
Joined
Jul 3, 2024
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to set up a record sheet that I need to be printable.

The sheet has information set in columns A-C and then records of issue in row 7 of further columns (plus other data in various rows)

I am looking to have a way to set the printing area to be columns A-S originally, but when there is data in columns beyond S the print area changes to columns A-C plus the last 16 columns with data in on row 7.
I have tried solutions around print areas, print titles etc, but getting very lost and not getting to a working solution. Is there a VBA or other solution to this so its user friendly?
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Untested here. See if the following works for you :

VBA Code:
Sub SetDynamicPrintArea()
    Dim ws As Worksheet
    Dim lastCol As Long
    Dim printArea As String
    Dim additionalColsStart As Long
    Dim additionalColsEnd As Long

    ' Set the worksheet (use ActiveSheet if this macro will apply to the active sheet)
    Set ws = ActiveSheet

    ' Find the last column with data in row 7
    lastCol = ws.Cells(7, ws.Columns.Count).End(xlToLeft).Column

    ' Check if the last column is within or beyond column S
    If lastCol <= 19 Then ' Column S is the 19th column
        ' Set the print area to columns A-S
        printArea = ws.Range("A1:S" & ws.Rows.Count).Address
    Else
        ' Set the print area to columns A-C and the last 16 columns with data in row 7
        additionalColsStart = lastCol - 15
        additionalColsEnd = lastCol
        printArea = ws.Range("A1:C" & ws.Rows.Count).Address & "," & _
                    ws.Range(ws.Cells(1, additionalColsStart), ws.Cells(ws.Rows.Count, additionalColsEnd)).Address
    End If

    ' Set the print area
    ws.PageSetup.PrintArea = printArea

    ' Optional: Notify the user
    MsgBox "Print area set to: " & printArea, vbInformation
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,225,501
Messages
6,185,339
Members
453,287
Latest member
Emeister

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top