Results 1 to 7 of 7

Thread: VBA Code To Autofit The Row Height Of Merged Cells

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    David, here's another suggestion. Replace your entire code with this.

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        
        Dim cM As Range
        Dim AutoFitRng As Range
        Dim MergeWidth As Single
        Dim NewRowHt As Single
        Dim str01 As String
        str01 = "OrderNote"
    
        If Not Intersect(Target, Range(str01)) Is Nothing Then
            Application.ScreenUpdating = False
            Set AutoFitRng = Range(str01).MergeArea
            For Each cM In AutoFitRng.Cells
                MergeWidth = cM.ColumnWidth + MergeWidth
            Next cM
            
            With Worksheets.Add
                .Columns(1).ColumnWidth = MergeWidth
                With .Cells(1)
                    .Value = AutoFitRng.Cells(1).Value
                    .WrapText = True
                    .EntireRow.AutoFit
                End With
                NewRowHt = .Rows(1).Height
                Application.DisplayAlerts = False
                .Delete
                Application.DisplayAlerts = False
            End With
            With AutoFitRng
                .WrapText = True
                .RowHeight = NewRowHt
            End With
            Application.ScreenUpdating = True
        End If
    
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0
    Hi, I'm sorry to report that this new code just gets me back to the same place as the original code, i.e. it works in terms of autosizing the row height, but limits that row height to 6-8 rows worth of content, depending on the combined width of the merged cells.

    I'm starting to think that this might not be possible and so am considering a workaround: in addition to using this code, which will cover any use of the worksheet where the data entered in the field is 6-8 rows or less, I could put a button above the field with something like "Enlarge" written on it, and assign a macro that expands the row height to allow for 10 rows of content.

    Everything I just described I can do, but the macro would work better if, rather than just changing the row height to 170 pixels (10 rows), it could identify the current row height and add 17 pixels (1 row). In this way, each press of the "Enlarge" button would expand the row height to allow 1 more row's worth of content to be displayed. Is it possible to build a macro that does that? If it helps, the sheet I'm working on is called "Loss Evaluation", the field is named "OrderNote" and the cell range is A18:L18.

    Thank you!

Similar Threads

  1. Fetch values from merged cells in macro
    By dhivya.enjoy in forum Excel Help
    Replies: 3
    Last Post: 11-20-2013, 02:49 PM
  2. VBA Code To Protect Sheet With Only A Few Cells Unlocked
    By rich_cirillo in forum Excel Help
    Replies: 3
    Last Post: 07-04-2013, 06:47 PM
  3. VBA code to move row to new spreadsheet
    By cdurfey in forum Excel Help
    Replies: 6
    Last Post: 06-10-2013, 10:38 PM
  4. Vba Code to find value and paste on certain row
    By jwitte in forum Excel Help
    Replies: 3
    Last Post: 11-28-2012, 08:52 PM
  5. Find Merged Cells VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 02-25-2012, 03:07 PM

Posting Permissions

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