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
-
The end of Windows 10 is approaching, consider Linux and LibreOffice
by
Alex5723
9 minutes ago -
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
1 hour, 20 minutes ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
12 hours, 17 minutes ago -
Update from WinPro 10 v. 1511 on T460p?
by
CatoRenasci
1 hour, 49 minutes ago -
System Restore and Updates Paused
by
veteran
14 hours, 47 minutes ago -
Windows 10/11 clock app
by
Kathy Stevens
1 hour, 53 minutes ago -
Turn off right-click draw
by
Charles Billow
18 hours, 1 minute ago -
Introducing ChromeOS M137 to The Stable Channel
by
Alex5723
21 hours, 32 minutes ago -
Brian Wilson (The Beach Boys) R.I.P
by
Alex5723
4 hours, 37 minutes ago -
Master patch listing for June 10, 2025
by
Susan Bradley
23 hours, 9 minutes ago -
Suggestions for New All in One Printer and a Photo Printer Windows 10
by
Win7and10
2 hours, 2 minutes ago -
Purchasing New Printer. Uninstall old Printer Software First?
by
Win7and10
1 day, 5 hours ago -
KB5060842 Issue (Minor)
by
AC641
1 day, 9 hours ago -
EchoLeak : Zero Click M365 Copilot leak sensitive information
by
Alex5723
1 day, 12 hours ago -
24H2 may not be offered June updates
by
Susan Bradley
4 hours, 52 minutes ago -
Acronis : Tracking Chaos RAT’s evolution (Windows, Linux)
by
Alex5723
2 days ago -
June 2025 updates are out
by
Susan Bradley
13 hours, 8 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
2 days, 15 hours ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
2 days ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
2 days, 18 hours ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
2 days, 18 hours ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
2 days, 18 hours ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
2 days, 10 hours ago -
re-install Windows Security
by
CWBillow
2 days, 21 hours ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
3 days, 1 hour ago -
macOS Tahoe 26
by
Alex5723
2 days, 19 hours ago -
Migrating from win10 to win11, instructions coming?
by
astro46
9 hours, 59 minutes ago -
Device Eligibility for Apple 2026 Operating Systems due this Fall
by
PKCano
2 days, 9 hours ago -
Recommended watching : Mountainhead movie
by
Alex5723
2 days, 10 hours ago -
End of support for Windows 10
by
Old enough to know better
9 hours, 24 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.