Loading
Excel.Tips.Net ExcelTips (Menu Interface)

Pixels in a Text String

Jerrold knows that the LEN function returns the number of characters in a text string. What he needs to know, however, is the number of pixels (not characters) in a text string. He wonders if there is some way to calculate the value.

The short answer is no, there isn't. The longer answer is that maybe there is. (Sound confusing? Read on.) The problem is that pixels—short for picture elements—are basically dots on a rendering device, such as a monitor or (more loosely) on a printer. The number of pixels packed into an inch depends on lots of variables, such as the resolution of the output device as well as its size.

For instance, at the same resolution, the number of pixels in an inch on a 19-inch monitor will be different than the number on a 21-inch monitor. Why? Because the screen resolution is spread over a larger display surface, resulting in fewer pixels per inch of that display surface.

Now let's throw fonts into the mix. Different fonts pack different number of characters into a given linear measurement. You can see this most frequently if you put a bunch of text in a cell, copy the cell down a number of rows, and then apply different fonts to each cell. The contents of the cells will occupy a different horizontal space depending on the font (and point size) selected.

If you are using a monospace font (like Courier) which has a set size for all characters you can calculate the characters per inch for all characters and then, based on the number of characters, determine a rough estimate of the number of pixels. But, you still run into problems because you have to take the output device into consideration.

This will not work with proportionate fonts since the size of the characters vary for each character: Ms are much wider than Is for example. Therefore for each character of each font (and perhaps even each font size) one would need a table of pixel size and one would have to create a function to loop through the string and keep a running total of the sizes from each character. This gets even more complicated if various character attributes, such as bold or italic, or applied to the font.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3802) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Related Tips:

A Picture is Worth Thousands! Your worksheets are not limited to holding numbers and text. You can also add graphics or easily create charts based on your data. Excel Graphics and Charts, available in two versions, helps you make your graphics and charts their absolute best. Check out Excel Graphics and Charts today!

 

Comments for this tip:

Damian    17 May 2012, 15:48
I don't agree with the fact that this is not possible. I'm not an excel expert but I have what I think is a solution (modified from something I did in js) to get the pixel width, the code snippet from my simple macro is as follows:
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "sample text"
    Columns("A:A").Select
    Columns("A:A").EntireColumn.AutoFit
    Range("B1").Select
    With Worksheets("Sheet1").Columns("A")
        ActiveCell.FormulaR1C1 = .ColumnWidth
    End With
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "=(RC[-1]*5+5)"
    Range("C1").Select


this essentially does the following:
1) Writes text to a cell (A1)
2) Autofits the column (A) to the contents
3) Puts the width of the column into another cell (B1)
4) Uses the following formula to determine the pixels from the cell width
 - (columnwidth * 5) + 5 = pixels (I got the formula for the pixels from here: http://processtrends.com/pg_charts_cell_size.htm)

I think that this could be used to get the pixel width for a string, and automated in VB to get the pixel length for a list.
Damian

Leave your own comment:

*Name:
Email:
  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*Text:
*What is 3+4? (To prevent automated submissions and spam.)
 
          Commenting Terms
 
 

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us

 

Advertise with Us

Our Privacy Policy

Our Sites

Tips.Net

Beauty and Style

Cars

Cleaning

Cooking

DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)

Gardening

Health

Home Improvement

Money and Finances

Organizing

Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Our Products

Premium Newsletters

Helpful E-books

Newsletter Archives

 

Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2014 Sharon Parq Associates, Inc.