+ Reply to Thread
Results 1 to 5 of 5

How to AutoFit Row Height Merge Cells?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-04-2015
    Location
    Jakarta
    MS-Off Ver
    15
    Posts
    154

    Lightbulb How to AutoFit Row Height Merge Cells?

    Please, help me to AutoFit Row Height the contents on merge cells with wrap text. On the unmerge cell AutoFit Row Height works perfectly.
    Last edited by putritersenyum; 04-14-2016 at 07:55 AM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: How to AutoFit Row Height Merge Cells?

    The following code adds up the column widths of the individually merged cells, unmerges the cell, resets the width of the first cell in the range to the combined width, autofits the row height of the single cell and remerges the cell.

    The way I suggest you use this is to make up a macro like Test() and map it to a control key. Then you can select the cell and click on the control combination and the cell will be formatted.

    Sub AutoFitMergedCellRowHeight(MyCell As Range)
        Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
        Dim CurrCell As Range
        Dim ActiveCellWidth As Single, PossNewRowHeight As Single
        
        ' Make the cell active
        MyCell.Activate
        ' Set the intial height to 0
        ActiveCell.RowHeight = 0
        
        ' If the cell is merged
        If ActiveCell.MergeCells Then
            ' With the merged area
            With ActiveCell.MergeArea
                ' If it is merged across more than one column
                If .Rows.Count = 1 And .WrapText = True Then
                    Application.ScreenUpdating = False
                    CurrentRowHeight = .RowHeight
                    ActiveCellWidth = ActiveCell.ColumnWidth
                    ' Add up the width of each of the cells in the merge
                    For Each CurrCell In Selection
                        MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
                    Next
                    ' Unmerge the cells
                    .MergeCells = False
                    ' Make the singe cell the combined with
                    .Cells(1).ColumnWidth = MergedCellRgWidth
                    ' Autofit the cell
                    .EntireRow.AutoFit
                    
                    PossNewRowHeight = .RowHeight
                    .Cells(1).ColumnWidth = ActiveCellWidth
                    ' Remerge the cells
                    .MergeCells = True
                    .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
                     CurrentRowHeight, PossNewRowHeight)
                End If
            End With
        End If
    End Sub
    
    Sub test()
    AutoFitMergedCellRowHeight Selection
    End Sub
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    03-04-2015
    Location
    Jakarta
    MS-Off Ver
    15
    Posts
    154

    Re: How to AutoFit Row Height Merge Cells?

    @ dflak, thanks a lot it works well. but please, help me to apply the change automatically without run macro sub (test) manually. And how to apply multiple selections?
    Last edited by putritersenyum; 04-14-2016 at 10:35 AM.

  4. #4
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,957

    Re: How to AutoFit Row Height Merge Cells?

    I don't think I can make it automatic. To make it apply to multiple selections, try using the following code.
    Sub test()
    Dim cl As Range
    For Each cl In seletction
        AutoFitMergedCellRowHeight cl
    Next
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-04-2015
    Location
    Jakarta
    MS-Off Ver
    15
    Posts
    154

    Re: How to AutoFit Row Height Merge Cells?

    I have tried for multiple selections, but it didn't work for me.
    Sub test()
    Dim cl As Range
    For Each cl In selection
        AutoFitMergedCellRowHeight cl
    Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. AutoFit Row Height for merged cells
    By VBA FTW in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-03-2014, 07:03 PM
  2. [SOLVED] Autofit Row Height on Merged Cells
    By Dragman in forum Excel General
    Replies: 0
    Last Post: 11-08-2013, 03:16 PM
  3. Row height using autofit, with no merged cells
    By D.Smith in forum Excel General
    Replies: 1
    Last Post: 07-02-2006, 06:51 AM
  4. [SOLVED] Autofit Row Height of Merged Cells
    By Jon in forum Excel General
    Replies: 3
    Last Post: 08-05-2005, 04:05 PM
  5. [SOLVED] How to autofit row height for merged wrapped cells
    By michele@quality-computing.com in forum Excel General
    Replies: 3
    Last Post: 05-22-2005, 08:06 PM
  6. Make Autofit row height work with merged cells
    By Franketh in forum Excel General
    Replies: 2
    Last Post: 04-06-2005, 06:06 PM
  7. Autofit row height in merged cells
    By BobT in forum Excel General
    Replies: 1
    Last Post: 02-25-2005, 01:06 PM
  8. [SOLVED] Autofit of Row Height with Merged Cells - AGAIN!
    By Paige in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-14-2005, 03:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1