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

  • Home
  • SEARCH
  • 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 4091494
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:22:26+00:00 2026-05-20T19:22:26+00:00

I have a report generator in Excel VBA, which works so far. It reads

  • 0

I have a report generator in Excel VBA, which works so far. It reads some input data and produces a formatted Excel sheet as result. The last column of this sheet is filled with some kind of free text.

Now, sometimes the free text does not fit into the last column, it is just too wide. Since the row height of all rows of this report is fixed, I cannot set range.WrapText=True for this column (to let the text stay visible, one would have to increase the row height). A manual (non-VBA) solution is easy: split the text into several parts and spread it over different rows. For example:

A          | B               |C
content    |This text is too wide for column B.
here       |This text fits.  |
is         |                 |
fixed      |                 |

should be transformed into

A          | B               |C
content    |This text is too |
here       |wide for column  |
is         |B.               |
fixed      |This text fits.  |

I could easily code that if I would be able to determine the real text width (using a proportional font!) of the content in column B using VBA. range.ColumnWidth gives me the actual width of the column, but I have no idea how I determine the width of "This text is too wide for column B." in VBA. Any suggestions?

(I am currently using Excel 2002 / XP).

  • 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-05-20T19:22:27+00:00Added an answer on May 20, 2026 at 7:22 pm

    This will work (in 2003… surely in 2002 as well).

    With Columns("B:B")
        oldWidth = .ColumnWidth
    
        .EntireColumn.AutoFit ' Aha!
        fitWidth = .ColumnWidth
    
        .ColumnWidth = oldWidth ' Restore original width
        If oldWidth < fitWidth Then
            ' Text is too wide for column.
            ' Do stuff.
        End If
    End With
    

    I wouldn’t get bogged down in this estimation business if I were you… That’s just asking for unexpected stuff to happen.


    EDIT: Addressing points in your comment.

    The above will AutoFit the column to the cell with the widest text. To consider only the text in the current cell, copy cell text into some blank column and do the AutoFit there.

    If you’re concerned about performance, then you’ll have to bite the bullet and make a look-up table of character widths. Cycle through Chr(i) and measure the width of each character, using the technique above. Store results in an array, make a function to get width of a string by looking up char widths in that array and summing. There will be an initial cost for making the LUT, but look-ups will be very quick, unnoticeably so. Of course, the LUT will only be valid for the current font, so I’d re-create it every time the code is run. Worth the cost if there are as many rows as you say.

    NB: fitWidth above will return the width of the character PLUS some small value (0.29pt on my machine) which is a kind of thin “white margin” automatically added on both sides of the cell for aesthetic purposes. Remember to subtract that from fitWidth to get the true character width. You can figure out how wide that is by doing an autofit on “A” and “AA”. The difference between these will be the true width of a single “A”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a report generator. Daily, it writes its data into a excel file.
We use an Excel sheet which contains some business logic (and so it is
I am looking for a report generator done in ruby or rails which allow
I have a Java class which uses Apache POI to generate reports in Excel.
I have a PHP script that will generate a report using PHPExcel from data
I have an ASP.Net MVC site that generates a Microsoft Excel 2003 XML formatted
I have a fairly complex report generating work, which needs to be generated spaning
I have an image generator which would benefit from running in threads. I am
I produce a complex HTML summary report from data in a database which could
I have generated excel report and i want the file to be downloaded by

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.