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, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
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, 10 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
-
‘Minority Report’ coming to NYC
by
Alex5723
7 hours, 10 minutes ago -
Apple notifies new victims of spyware attacks across the world
by
Alex5723
7 hours, 17 minutes ago -
Tracking content block list GONE in Firefox 138
by
Bob99
6 hours, 41 minutes ago -
How do I migrate Password Managers
by
Rush2112
3 hours, 47 minutes ago -
Orb : how fast is my Internet connection
by
Alex5723
4 hours, 14 minutes ago -
Solid color background slows Windows 7 login
by
Alex5723
18 hours, 57 minutes ago -
Windows 11, version 24H2 might not download via Windows Server Updates Services
by
Alex5723
17 hours, 27 minutes ago -
Security fixes for Firefox
by
Susan Bradley
26 minutes ago -
Notice on termination of services of LG Mobile Phone Software Updates
by
Alex5723
1 day, 5 hours ago -
Update your Apple Devices Wormable Zero-Click Remote Code Execution in AirPlay..
by
Alex5723
1 day, 14 hours ago -
Amazon denies it had plans to be clear about consumer tariff costs
by
Alex5723
1 day, 5 hours ago -
Return of the brain dead FF sidebar
by
EricB
16 hours, 57 minutes ago -
Windows Settings Managed by your Organization
by
WSDavidO61
3 hours, 7 minutes ago -
Securing Laptop for Trustee Administrattor
by
PeachesP
1 hour, 50 minutes ago -
The local account tax
by
Susan Bradley
18 hours, 17 minutes ago -
Recall is back with KB5055627(OS Build 26100.3915) Preview
by
Alex5723
2 days, 3 hours ago -
Digital TV Antenna Recommendation
by
Win7and10
1 day, 20 hours ago -
Server 2019 Domain Controllers broken by updates
by
MP Support
2 days, 15 hours ago -
Google won’t remove 3rd party cookies in Chrome as promised
by
Alex5723
2 days, 17 hours ago -
Microsoft Manager Says macOS Is Better Than Windows 11
by
Alex5723
2 days, 20 hours ago -
Outlook (NEW) Getting really Pushy
by
RetiredGeek
1 day, 22 hours ago -
Steps to take before updating to 24H2
by
Susan Bradley
7 hours, 23 minutes ago -
Which Web browser is the most secure for 2025?
by
B. Livingston
2 days, 3 hours ago -
Replacing Skype
by
Peter Deegan
1 day, 15 hours ago -
FileOptimizer — Over 90 tools working together to squish your files
by
Deanna McElveen
2 days, 14 hours ago -
Excel Macro — ask for filename to be saved
by
nhsj
1 day, 11 hours ago -
Trying to backup Win 10 computer to iCloud
by
SheltieMom
1 day, 15 hours ago -
Windows 11 Insider Preview build 26200.5570 released to DEV
by
joep517
4 days, 20 hours ago -
Windows 11 Insider Preview build 26120.3941 (24H2) released to BETA
by
joep517
4 days, 22 hours ago -
Windows 11 Insider Preview Build 22635.5305 (23H2) released to BETA
by
joep517
4 days, 22 hours ago
Recent blog posts
Key Links
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
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.