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
-
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
15 minutes ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
16 minutes ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
7 hours, 17 minutes ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
8 hours, 24 minutes ago -
0Patch, where to begin
by
cassel23
2 hours, 26 minutes ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
22 hours, 1 minute ago -
89 million Steam account details just got leaked,
by
Alex5723
9 hours, 47 minutes ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
1 day, 6 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
21 hours, 33 minutes ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
8 hours, 41 minutes ago -
Installer program can’t read my registry
by
Peobody
3 hours, 38 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
19 hours, 20 minutes ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
1 day, 2 hours ago -
False error message from eMClient
by
WSSebastian42
1 day, 17 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
2 days, 2 hours ago -
Office 2021 Perpetual for Mac
by
rebop2020
2 days, 3 hours ago -
AutoSave is for Microsoft, not for you
by
Will Fastie
1 day ago -
Difface : Reconstruction of 3D Human Facial Images from DNA Sequence
by
Alex5723
2 days, 7 hours ago -
Seven things we learned from WhatsApp vs. NSO Group spyware lawsuit
by
Alex5723
1 day, 8 hours ago -
Outdated Laptop
by
jdamkeene
2 days, 12 hours ago -
Updating Keepass2Android
by
CBFPD-Chief115
2 days, 18 hours ago -
Another big Microsoft layoff
by
Charlie
2 days, 18 hours ago -
PowerShell to detect NPU – Testers Needed
by
RetiredGeek
15 hours, 24 minutes ago -
May 2025 updates are out
by
Susan Bradley
58 minutes ago -
Windows 11 Insider Preview build 26200.5600 released to DEV
by
joep517
3 days ago -
Windows 11 Insider Preview build 26120.3964 (24H2) released to BETA
by
joep517
3 days ago -
Drivers suggested via Windows Update
by
Tex265
2 days, 23 hours ago -
Thunderbird release notes for 128 esr have disappeared
by
EricB
20 hours ago -
CISA mutes own website, shifts routine cyber alerts to X, RSS, email
by
Nibbled To Death By Ducks
3 days, 6 hours ago -
Apple releases 18.5
by
Susan Bradley
3 days, 1 hour 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.