+ Reply to Thread
Results 1 to 5 of 5

Thread: Autofit row height

  1. #1
    Registered User
    Join Date
    07-27-2010
    Location
    Aberdeen
    MS-Off Ver
    Excel 2007
    Posts
    1

    Question Autofit row height

    I have the following code which I am applying to an excel template.
    Public Sub ReSizeRow()
    
    If ActiveCell.MergeCells = False Then Exit Sub
    
    With ActiveCell
    .WrapText = True
    .UnMerge
    Rows(.Row).AutoFit
    .RowHeight = .Width * .Height / Selection.Width
    Selection.Merge
    End With
    
    End Sub
    The problem is that the Autofit is only applying to the first row in the merged cell so I am still not managing to see all of the text entered.

    The row height is at 129 pixels which I am presuming is the maximum and thats why I am having the problem.

    Any suggestions

    Thanks

    Max

    Edited to add attachment - the cells I want to expand depending on content is the 'Summary Field' and 'Full Details of Inspection Field'
    Attached Files Attached Files
    Last edited by romperstomper; 07-27-2010 at 09:37 AM. Reason: Add code tags

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Autofit row height

    The max row height in excel is 409pts

    Length of cell contents (text) 32,767 characters. Only 1,024 display in a cell; all 32,767 display in the formula bar.
    A quick check on one of your fields resulted in 1969 characters

    This is probably where your problem arises

    Check these helpful (?) M.S. sites, between them they seem to refer to a mix of 2003 & 2007 specs

    http://office.microsoft.com/en-us/ex...005199291.aspx

    http://office.microsoft.com/en-us/ex...010137545.aspx
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  3. #3
    Registered User
    Join Date
    07-27-2010
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Autofit row height

    @ maximoomoo

    Hi Max,

    I have been working to resize merged cells myself. I found some code that would do it for me however I couldn't get to run properly. Anyway after 2 days of pulling my hair out I stumbled across this code that works beautifully and automatically.

    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
    just copy and past this code into your worksheet object.

    Hope this helps

    Windaroo
    Last edited by shg; 09-28-2010 at 05:13 PM. Reason: deleted quote

  4. #4
    Registered User
    Join Date
    03-25-2008
    Location
    Evansville, IN
    Posts
    38

    Re: Autofit row height

    Windaroo ,

    I copied this code into my spreadsheet module but can't get it to work. How do you execute it?

    Denny

  5. #5
    Registered User
    Join Date
    10-03-2010
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    1

    Smile Re: Autofit row height

    Quote Originally Posted by windaroo View Post
    @ maximoomoo

    Hi Max,

    I have been working to resize merged cells myself. I found some code that would do it for me however I couldn't get to run properly. Anyway after 2 days of pulling my hair out I stumbled across this code that works beautifully and automatically.

    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
    just copy and past this code into your worksheet object.

    Hope this helps

    Windaroo
    Brilliant Brillliant Brilliant, although, it did want to lock unlocked cells after running, but simply added a couple lines to the bottom to inlock them and now better than brilliant. I have searching for this solution for ages, everybody saying thumbs down to merged cells.

+ 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.2.0