Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9067725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:00:55+00:00 2026-06-16T17:00:55+00:00

I have an excel VBA print function that is called once for each sheet

  • 0

I have an excel VBA print function that is called once for each sheet I want to print in my workbook

I cycle through the sheets in VBA and call this function.

Sub PrintSheet
  With ActiveSheet

    'lastRow is worked out here....

    'Autofit YTD and SLY
    .Columns("J:J").AutoFit
    .Columns("K:K").AutoFit

    'Autofit email column
    .Columns("F:F").AutoFit

    With .PageSetup
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Orientation = xlLandscape
        .LeftMargin = Application.InchesToPoints(0.4)
        .RightMargin = Application.InchesToPoints(1)
        .TopMargin = Application.InchesToPoints(0.5)
        .BottomMargin = Application.InchesToPoints(0.5)
        .RightFooter = "&P of &N"

        .PrintArea = "$A$1:$O$" & CStr(lastRow)
    End With

    .PrintOut
 End With
End Sub

The issue I am having is that sometimes (random behaviour and not always the same sheet) the columns that I am autofitting stretch really wide forcing other columns off the page. This is not data related as you can run the print routine again and it prints the same sheet which was previously stretching columns out fine. I am trimming all column values in the sheet as they get inputted with VBA code in the first place

I have not been able to find any pattern with this behaviour apart from as I said the columns that stretch are the ones that are getting autofitted.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:00:56+00:00Added an answer on June 16, 2026 at 5:00 pm

    I have experienced this many times when using autofit; so I try to use it as little as possible now.

    Things you can do:

    1.Use a set width e.g if you know that column J will always be ok at 47 width then use Columns("D:D").ColumnWidth = 47
    2. You could add some tests to the end of your sub-routine like so:

    Sub PrintSheet()
    
    With Excel.ActiveSheet
    
        'lastRow is worked out here....
        lastRow = .Cells(.Rows.Count, 1).End(Excel.xlUp).Row
    
        'Autofit YTD / SLY / email
        .Columns("J:J").AutoFit
        .Columns("K:K").AutoFit
        .Columns("F:F").AutoFit
    
        With .PageSetup
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
            .Orientation = Excel.xlLandscape
            .LeftMargin = Application.InchesToPoints(0.4)
            .RightMargin = Application.InchesToPoints(1)
            .TopMargin = Application.InchesToPoints(0.5)
            .BottomMargin = Application.InchesToPoints(0.5)
            .RightFooter = "&P of &N"
            .PrintArea = "$A$1:$O$" & lastRow 'CStr(lastRow) <=== & does an implicit conversion
        End With
    
        'add three checks here <====
        '<==I've picked 100 out of thin air but you need to change this to a value which indicates that it has behaved badly
        If .Columns("J:J").ColumnWidth > 100 Then
            .Columns("J:J").ColumnWidth = 30  '<== you might want to amend 30
        End If
        If .Columns("k:k").ColumnWidth > 100 Then
            .Columns("k:k").ColumnWidth = 30
        End If
        If .Columns("f:f").ColumnWidth > 100 Then
            .Columns("f:f").ColumnWidth = 30
        End If
    
        .PrintOut
    
    End With
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's been a while since I have used VBA on Excel. I want to
I have a VBA script that inserts long strings into Excel cells. In some
I have written a VBA macro in an excel spreadsheet that opens all workbooks
I have an Excel workbook that is used as a starting point to generate
I have a VBA publishing macro in Excel which generates a published workbook based
I have the following code in my Excel VBA that copies data from a
I have a VBA function within a spreadsheet which operates on another spreadsheet that
I currently have a excel workbook that has values being pulled from other workbooks
I am very new to Excel and vba. I have an excel file that
I have a workbook (wkbk1) that has a sheet with eight different query tables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.