+ Reply to Thread
Results 1 to 3 of 3

Need a tweak to worksheet change code that autofits the row height of merged cells

  1. #1
    Registered User
    Join Date
    02-26-2014
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Need a tweak to worksheet change code that autofits the row height of merged cells

    Hi, I found the below code to autofit the row height of merged cells. It works fine, but Excel sets the row height to 17 pixels per row and the rest of the worksheet is 20. Is there any way to tweak the code so that each row is allocated 20 instead of 17?

    Thank you!


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MergeWidth As Single
    Dim cM As Range
    Dim AutoFitRng As Range
    Dim CWidth As Double
    Dim NewRowHt As Double
    Dim str01 As String
    str01 = "LossEval"

    If Not Intersect(Target, Range(str01)) Is Nothing Then
    Application.ScreenUpdating = False
    On Error Resume Next
    Set AutoFitRng = Range(Range(str01).MergeArea.Address)

    With AutoFitRng
    .MergeCells = False
    CWidth = .Cells(1).ColumnWidth
    MergeWidth = 0
    For Each cM In AutoFitRng
    cM.WrapText = True
    MergeWidth = cM.ColumnWidth + MergeWidth
    Next
    'small adjustment to temporary width
    MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
    .Cells(1).ColumnWidth = MergeWidth
    .EntireRow.AutoFit
    NewRowHt = .RowHeight
    .Cells(1).ColumnWidth = CWidth
    .MergeCells = True
    .RowHeight = NewRowHt
    End With
    Application.ScreenUpdating = True
    End If

    End Sub

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Need a tweak to worksheet change code that autofits the row height of merged cells

    Hi David,

    Perhaps change
    NewRowHt = .RowHeight
    to
    NewRowHt = 20
    ?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    02-26-2014
    Location
    Los Angeles, CA
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Need a tweak to worksheet change code that autofits the row height of merged cells

    Hi Marvin, that changed the pixels on the first row to 26 and the wrap function doesn't work anymore.

    I previously tried the below, but it would only work if there was 1 row of content. Any more and it went back to 17 for all rows (so 2=34, 3=51, etc.)

    Changed:
    NewRowHt = .RowHeight

    To:
    NewRowHt = Application.Max(.RowHeight, 15)

+ 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. [SOLVED] Autofit Row Height on Merged Cells
    By Dragman in forum Excel General
    Replies: 0
    Last Post: 11-08-2013, 03:16 PM
  2. [SOLVED] Auto Row Height Merged Cells (Greg Wilson's Code) Questions
    By FallingDown in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-25-2013, 06:25 AM
  3. How to auto-fit row height for worksheet with merged cells after importing data
    By GregMcAfee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-19-2012, 06:20 PM
  4. Editing a macro to change the row height in a merged cell
    By ordoff73 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2008, 09:41 AM
  5. Row height and Merged Cells
    By Darin Kramer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2005, 01:06 PM

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