I would like to automate the process of widening columns that are too narrow (#####). If column is too narrow (#####), autofit it, BUT no other column widths must be changed in any way. How can I check if a column is too narrow for any data, and how do I automate this?
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Widen (#####) columns with code (Excel 2000 >)
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Widen (#####) columns with code (Excel 2000 >)
- This topic has 39 replies, 7 voices, and was last updated 19 years, 11 months ago.
Viewing 3 reply threadsAuthorReplies-
WSJohnBF
AskWoody Lounger -
WSRudi
AskWoody LoungerJune 21, 2005 at 2:09 pm #954967Hi John,
The tricky part is Selection!!! Its easy to say Entirecolumn.Autofit, but this must only apply to columns that have #### in them! No other column widths must be adjusted. If my sheet has 100 columns and 73 of those columns have #### due to being too narrow to show the number, it must widen by use of autofit. How do I determine what column has ####?
Tx
-
-
WSHansV
AskWoody LoungerJune 21, 2005 at 2:05 pm #954965You could loop through the columns and
– Store the current width in a variable.
– AutoFit the column (look up AutoFit in the VBA help)
– Compare the new width to original width (the value of the variable).
– If the new width is LESS than the original width, restore the original width.
(If the new width is MORE, the column needed to be widened, and if it is the same, nothing has happened) -
WSRudi
AskWoody Lounger -
WSJohnBF
AskWoody LoungerJune 22, 2005 at 2:14 pm #954981(Edited by JohnBF on 22-Jun-05 08:14. Code now accounts for Hans point that the original code would reset any custom column width settings, code fixes and improvements.)
I locked my machine up, TWICE, writing this, hence the delay. Never enter “?activesheet.columns” in the Immediates window.
See if it’s what you want.
Sub AutofitMin()
Dim dblW As Double
Dim rngR As RangeFor Each rngR In Selection.Columns
With rngR.EntireColumn
dblW = .ColumnWidth
.AutoFit
If .ColumnWidth < dblW Then .ColumnWidth = dblW
End With
Next rngR
End Sub -
WSHansV
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSHansV
AskWoody LoungerJune 21, 2005 at 3:19 pm #955007What if Rudi has manually changed the widths of some columns the way he wants them? Your code runs the risk of resetting them to the default column width. My idea was to compare the autofitted width of each column to the width of that column before autofit, and to restore the old width if the column has become narrower.
-
WSJohnBF
AskWoody Lounger
-
-
WSRudi
AskWoody Lounger -
WSRudi
AskWoody LoungerJune 22, 2005 at 12:17 pm #955218Hi John,
I see you used integer to store the initial column width. I’ve always thought integer only allows whole numbers. The excel help files say: stored as 16-bit (2-byte) numbers ranging in value from -32,768 to 32,767. Does this number reperesent decimals or is it a whole number? Also column widths are decimaled eg. 10.75. Please shed some light on what integer stores, and if column widths are stored as decimal..or is it points (whole numbers!)
TIA
-
-
-
-
H. Legare Coleman
AskWoody PlusJune 21, 2005 at 5:11 pm #955036If you want to check cell B1 to see if it is displaying #####, you can do the following:
If Left(Range(“B1”).Text,1)) = “#” Then
To check and AutoFit all of the cells in the current selection, you could do this:
Dim oCell As Range
For Each oCell In Selection
If Left(Trim(oCell.Text), 1) = “#” Then
oCell.EntireColumn.AutoFit
End If
Next oCell -
WSRudi
AskWoody LoungerJune 22, 2005 at 4:51 am #955088Thanx for the suggestion Legare. I tested your code and it works just as well.
One question: Your method tests for each cell in the selection. (The selection might at certain times be up to 20 – 30 columns.) This may account for the loop running 1966080 times. When a column is autofitted, does it also test each cell internally in code? I suppose my question boils down to: Which code example is more efficient, or are they both ultimately the same? I ask simply for interest sake; I’m grateful to both you and John for helping out with the code! Many many thanx!
-
WSHansV
AskWoody Lounger -
WSRudi
AskWoody LoungerJune 22, 2005 at 5:26 am #955099Thanx. You don’t need to answer this…but it makes me wonder what the code looks like that drives autofit? How is it possible to autofit a column based on the longest entry in a cell in the column without testing the iduvidual length of each cell. It must have to do with arrays… or your favourite UBound/LBound functions…
-
WSHansV
AskWoody Lounger
-
-
-
H. Legare Coleman
AskWoody Plus -
WSRudi
AskWoody Lounger -
H. Legare Coleman
AskWoody Plus -
WSHansV
AskWoody LoungerJune 22, 2005 at 1:46 pm #955253If the range has many columns and few rows, your code might be slightly more efficient, but if it contains many rows, looping through all cells is costly – the code has to check them all, even though most columns will already have been autofitted early on. Since a worksheet has 256 columns and 65,536 rows, looping through columns will in general be faster, I think.
-
H. Legare Coleman
AskWoody Plus -
WSJohnBF
AskWoody LoungerJune 22, 2005 at 2:08 pm #955259 -
WSHansV
AskWoody Lounger -
H. Legare Coleman
AskWoody Plus -
H. Legare Coleman
AskWoody PlusJune 22, 2005 at 2:18 pm #955262Doing the equivalent thing in VBA and internal code, then I would think that the internal code is always more efficient. However, in this case, I don’t know that the loops are equivalent. I have no idea what the internal code is doing in its loop. It may be having to calculate the width of the contents of each cell, which may require a call to the display driver for each cell. That could be much more inefficient than VBA to look to see if the first character is a “#”. Since the font may or may not be a porportional font, you can’t just look at the length of the displayed characters to know what the widest width required is.
-
WSrory
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSrory
AskWoody Lounger -
WSJohnBF
AskWoody Lounger -
WSrory
AskWoody Lounger -
WSLeighW
AskWoody LoungerJune 24, 2005 at 4:36 am #955631Tip (and a bit off subject – but it may be helpful): For any English language only Loungers, try copying and pasting unknown words into the following site, selecting (guessing) the language and click on Translate.
http://babelfish.altavista.com/%5B/url%5D
Leigh
-
H. Legare Coleman
AskWoody PlusJune 22, 2005 at 1:38 pm #955248I also just thought that if you are going to be selecting entire columns, then my code could be made more efficient by this change:
Dim oCell As Range
For Each oCell In Intersect(Selection, ActiveSheet.UsedRange)
If Left(Trim(oCell.Text), 1) = “#” Then
oCell.EntireColumn.AutoFit
End If
Next oCell -
WSrory
AskWoody LoungerJune 22, 2005 at 3:42 pm #955296You could also try:
Sub FindSmallColumns() Dim rngFound As Range With Selection Set rngFound = .Find(what:="#", LookIn:=xlValues, lookat:=xlPart, MatchCase:=True) If Not rngFound Is Nothing Then Do rngFound.EntireColumn.AutoFit Set rngFound = .FindNext(rngFound) Loop While Not rngFound Is Nothing End If End With End Sub
which works for me in XL2002. The downside is it will refit any columns with cells containing # in them (e.g. “invoice#”)
-
WSRudi
AskWoody LoungerJune 24, 2005 at 5:21 am #955635Thanx Rory. Your answer is just as valid.
TxJohn. I see you reworked your code in post 490558. Thanx for your input!
-
-
-
-
-
WSJamesGreen
AskWoody Lounger -
WSRudi
AskWoody LoungerJune 22, 2005 at 12:10 pm #955216Thanx for your input Danny. I do know about this utility, and I have used it in the past! The underlying reason for my original post is to learn to code this. I’m playing a lot with VBA for Excel currently and I am trying to break out of a “way of thinking” that I have developed. I find the lounge as a good resource to get different ideas and code structures. It is helping me to think out of the box and be more creative in developing solutions to common issues in Excel. Simply put…the lounge is a BIG learning curve for me!
My deepest thanx and respect to the lounge and all who contribute to my development!
-
Viewing 3 reply threads -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
How well does your browser block trackers?
by
n0ads
1 hour, 16 minutes ago -
You can’t handle me
by
Susan Bradley
3 hours, 29 minutes ago -
Chrome Can Now Change Your Weak Passwords for You
by
Alex5723
1 hour, 27 minutes ago -
Microsoft: Over 394,000 Windows PCs infected by Lumma malware, affects Chrome..
by
Alex5723
8 hours, 50 minutes ago -
Signal vs Microsoft’s Recall ; By Default, Signal Doesn’t Recall
by
Alex5723
9 hours, 1 minute ago -
Internet Archive : This is where all of The Internet is stored
by
Alex5723
9 hours, 14 minutes ago -
iPhone 7 Plus and the iPhone 8 on Vantage list
by
Alex5723
9 hours, 19 minutes ago -
Lumma malware takedown
by
EyesOnWindows
16 hours, 33 minutes ago -
“kill switches” found in Chinese made power inverters
by
Alex5723
18 hours, 7 minutes ago -
Windows 11 – InControl vs pausing Windows updates
by
Kathy Stevens
18 hours, 1 minute ago -
Meet Gemini in Chrome
by
Alex5723
22 hours, 6 minutes ago -
DuckDuckGo’s Duck.ai added GPT-4o mini
by
Alex5723
22 hours, 15 minutes ago -
Trump signs Take It Down Act
by
Alex5723
1 day, 6 hours ago -
Do you have a maintenance window?
by
Susan Bradley
17 hours, 23 minutes ago -
Freshly discovered bug in OpenPGP.js undermines whole point of encrypted comms
by
Nibbled To Death By Ducks
8 hours, 26 minutes ago -
Cox Communications and Charter Communications to merge
by
not so anon
1 day, 9 hours ago -
Help with WD usb driver on Windows 11
by
Tex265
1 day, 14 hours ago -
hibernate activation
by
e_belmont
1 day, 18 hours ago -
Red Hat Enterprise Linux 10 with AI assistant
by
Alex5723
1 day, 22 hours ago -
Windows 11 Insider Preview build 26200.5603 released to DEV
by
joep517
2 days, 1 hour ago -
Windows 11 Insider Preview build 26120.4151 (24H2) released to BETA
by
joep517
2 days, 1 hour ago -
Fixing Windows 24H2 failed KB5058411 install
by
Alex5723
21 hours, 27 minutes ago -
Out of band for Windows 10
by
Susan Bradley
2 days, 6 hours ago -
Giving UniGetUi a test run.
by
RetiredGeek
2 days, 13 hours ago -
Windows 11 Insider Preview Build 26100.4188 (24H2) released to Release Preview
by
joep517
2 days, 20 hours ago -
Microsoft is now putting quantum encryption in Windows builds
by
Alex5723
16 hours, 27 minutes ago -
Auto Time Zone Adjustment
by
wadeer
3 days, 1 hour ago -
To download Win 11 Pro 23H2 ISO.
by
Eddieloh
2 days, 22 hours ago -
Manage your browsing experience with Edge
by
Mary Branscombe
3 hours, 51 minutes ago -
Fewer vulnerabilities, larger updates
by
Susan Bradley
57 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.