Find widest column in range?

9tanstaafl9

Well-known Member
Joined
Mar 23, 2008
Messages
535
How would I go about finding the widest column in a range using VBA?

I'm trying to make all the columns equal in width, and as wide as the widest column. I've got the part to adjust the column widths figured out, I just need to know how to find the one that is widest in the first place so I can steal it's columnwidth for my macro. needs to be done in VBA since it will change every time the report is run.

Thanks!

And if this is cross posting I apologize. I'm not sure since the question no longer has to do with the function I asked about. I had asked a question earlier about using MAX and it not working correctly. Someone helped me with that, and I got MAX to work, but it didn't do what I thought it would do and i think I was on the totally wrong track.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try:

Code:
    Dim maxWidth As Long
    For Each col In ActiveSheet.UsedRange.Columns
        If col.Width > maxWidth Then
            maxWidth = col.Width
        End If
    Next
 
Upvote 0
Try:

Code:
    Dim maxWidth As Long
    For Each col In ActiveSheet.UsedRange.Columns
        If col.Width > maxWidth Then
            maxWidth = col.Width
        End If
    Next


Thank you! This had been bugging me for two days. I had to modify it a little:


Code:
Range("RangeDrag").Columns.AutoFit
    Dim maxWidth As Long, Col As Variant
    maxWidth = 0
    For Each Col In Range("rangedrag").Columns
        If Col.ColumnWidth > maxWidth Then
            maxWidth = Col.ColumnWidth
        End If
    Next
 
Range("RangeDrag").ColumnWidth = maxWidth
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top