Excel VBA Column Width - Merged Cells

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
974
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi everyone,
I need help with VBA code.
I would like to re-size the columns H through M based on the maximum number of characters (numbers & dashes) in each cells
For example column H there are 30 cells that may range anywhere from 6 to 24 characters in each cell.
Problem is cell range H7 through M7 and H8 through M8 are merged cells.
If I run the current macro it defaults to width of 8 due to the merged cells (H7 through M7 and H8 through M8)
Column M is the last column
All columns H through M have a column width of 8

Column H
Selection.ColumnWidth = 20

Column i
Selection.ColumnWidth = 16

Column j
Selection.ColumnWidth = 12

Column k
Selection.ColumnWidth = 12

Column l
Selection.ColumnWidth = 10

Column m
Selection.ColumnWidth = 8

Thank you in advance
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
The only other way I can think of is to focus in on just the rows you want to consider, find the maximum length of those cells, and then set the column to be that wide.
For example, if we only wanted to consider rows 9 through 39 for columns H through M, that code might look something like this:

First, we will create a custom Function that will find the maximum length in any range of cells:
VBA Code:
Function MaxCellWidth(rng As Range) As Long
'   Get the maximum length on entry in a range of cells

    Dim cell As Range
    Dim w As Long
    
'   Loop through all cells and return longest width
    For Each cell In rng
        If Len(cell) > w Then w = Len(cell)
    Next cell
    
'   Return width
    MaxCellWidth = w
    
End Function

Then, we have our code which goes through those columns, uses the function above to find the max length, and then set that column width:
VBA Code:
Sub SetColWidths()

    Dim fr As Long
    Dim lr As Long
    Dim fc As Long
    Dim lc As Long
    Dim c As Long
    Dim rng As Range
    Dim w As Long
    
'   Set rows and columns to consider and re-size
    fr = 9  'first row (9)
    lr = 39 'last row (39)
    fc = 8  'first column ("H")
    lc = 13 'last column ("M")
    
'   Loop through all columns
    For c = fc To lc
'       Build range to check
        Set rng = Range(Cells(fr, c), Cells(lr, c))
'       Get max column width
        w = MaxCellWidth(rng)
'       Set column width
        Cells(1, c).EntireColumn.ColumnWidth = w
    Next c
    
End Sub
 
Upvote 0
Solution
Thank you for the code and suggestions.
I used Tom's suggestion and the code worked.
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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