+ Reply to Thread
Results 1 to 7 of 7

Auto Row Height Merged Cells (Greg Wilson's Code) Questions

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Hi all

    To autosize the row height of merged cells I am using Greg Wilson's Code


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim NewRwHt As Single
    Dim cWdth As Single, MrgeWdth As Single
    Dim c As Range, cc As Range
    Dim ma As Range
    
    With Target
    If .MergeCells And .WrapText Then
    Set c = Target.Cells(1, 1)
    cWdth = c.ColumnWidth
    Set ma = c.MergeArea
    For Each cc In ma.Cells
    MrgeWdth = MrgeWdth + cc.ColumnWidth
    Next
    Application.ScreenUpdating = False
    ma.MergeCells = False
    c.ColumnWidth = MrgeWdth
    c.EntireRow.AutoFit
    NewRwHt = c.RowHeight
    c.ColumnWidth = cWdth
    ma.MergeCells = True
    ma.RowHeight = NewRwHt
    cWdth = 0: MrgeWdth = 0
    Application.ScreenUpdating = True
    End If
    End With
    End Sub

    BUT, it does not seem to work if I merge a cell over more than four rows:
    It ends in Runtime-Error '1004':
    Unable to set columnWidth property of the range class

    Excel has a problem with this line

    c.ColumnWidth = MrgeWdth

    Is any one else experiencing this problem?

    Thanks
    FD
    Last edited by FallingDown; 02-25-2013 at 06:48 AM.

  2. #2
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Hi everyone,

    Any help on the above would be appeciated greatly - many thanks

  3. #3
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Hi all

    No solution to this problem?
    I can't understand why the code would not work when the merged cells are distributed over more than four rows

  4. #4
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Good morning

    OK, last try - no one who would know how to fix this at all?

  5. #5
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    The code looks fine, and when I test in a dummy workbook (even when merging 4 rows) I do not receive any error message. I guess the column widths, row heights and size of text in the merged cell may be causing a problem. Would it be possible to post a copy of the workbook? If not, debug the code and tell us what the value of the MrgeWdth variable is when the error occurs.
    If you like my contribution click the star icon!

  6. #6
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Had another look, the maximum width you can assign via .ColumnWidth is the value 255 (representing characters for some reason - according to the helptext). So if the total of the width of the merged columns exceeds 255, the code is going to result in an error.

  7. #7
    Forum Contributor
    Join Date
    12-19-2011
    Location
    Central Europe
    MS-Off Ver
    Excel O365
    Posts
    364

    Re: Auto Row Height Merged Cells (Greg Wilson's Code) Questions

    Hi OllieB

    Many Many thanks for your comment
    I amended the code to read c.ColumnWidth = 254
    This works perfectly fine!!

    Thanks again

    FD

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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