Results 1 to 7 of 7

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

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0

    VBA Code To Autofit The Row Height Of Merged Cells

    Hi, I found the code below and it works pretty well, but it limits how much content is displayed. For example, I have 1 sheet where the combined width of the merged columns is 1,146 pixels and so, if a ton of content is entered, the # of rows that are displayed is limited to 6. On another sheet where the combined width is 815 pixels, the limit is 8 rows. Does anybody know of a way to address this? On the sheet where the combined width is 1,146 pixels, I need to display at least 10 rows worth of content.

    Thank you!

    PHP Code:
    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 
    "OrderNote"

      
    If Not Intersect(TargetRange(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
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Welcome to board !!

    Try something like this.

    Code:
    Option Explicit
    
    Private Function NewRowHeight(ByVal MyPixels As Long) As Long
        
        Select Case MyPixels
            Case 1 To 100
                NewRowHeight = 15
            Case 101 To 250
                NewRowHeight = 35
            'more cases
            Case 1100 To 1200
                NewRowHeight = 90
        End Select
        
    End Function
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0
    Hi, thank you for the reply, but I unfortunately have to ask a stupid question: where should that new code go? I'm obviously a VBA novice so I tried inserting it a few different ways (inside the exisiting code on the worksheet, as a module, as a class module), but either got a compile error or didn't see any change from previous results.

    Thanks again!

  4. #4
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    The code goes in the same sheet module.

    Code:
    Option Explicit
    
    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 = "OrderNote"
    
      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
          
          '//get the row height based on the given pixel
          NewRowHt = NewRowHeight(MergeWidth) 'here MergeWidth is the pixels
          
          .Cells(1).ColumnWidth = CWidth
          .MergeCells = True
          .RowHeight = NewRowHt
        End With
        Application.ScreenUpdating = True
      End If
    
    End Sub
    
    
    Private Function NewRowHeight(ByVal MyPixels As Long) As Long
        
        Select Case MyPixels
            Case 1 To 100
                NewRowHeight = 15 '<< adjust the row height here
            Case 101 To 250
                NewRowHeight = 35 '<< adjust the row height here
            'more cases
            Case 1100 To 1200
                NewRowHeight = 90 '<< adjust the row height here
        End Select
        
    End Function
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    4
    Rep Power
    0
    Hi, I cleared the code from the worksheet in question and pasted in what you provided. I can see that it's doing something, but it doesn't seem to be operating as intended. The row height was set to 15 (20 pixels) regardless of how much content I entered. I did play with changing "15" to another value and can see the effect, but it continues to set the row height to the new value every time.

    I might just be misunderstanding. I can see that the code assigns different row heights to "Case 1 To 100", "Case 101 To 250" and "Case 1100 To 1200", but I guess I don't understand what that's referring to. It doesn't seem to refer to the amount of content entered (as I got the same row height regardless of whether I entered a little or a ton) or the combined width of the merged columns (as I played with those and saw no change).

    Thanks again for your help!

  6. #6
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    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

  7. #7
    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
  •