+ Reply to Thread
Results 1 to 13 of 13

Automatically resizing rows when value changed

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Automatically resizing rows when value changed

    Hi All

    So, at the moment I have two date cells where users can change a month or year. Depending on what they select, some text - using a formula - is shown within a merged cell. I want the cell to automatically update in height to fit the text, and I'm nearly there.

    So far I have this code in the worksheet where the cell is:

    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 = "CommentBox"
    
    Worksheets("Summary").Unprotect
    
      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
        
     Worksheets("Summary").Protect
      
    End Sub
    I also have some more code in the same sheet:

    Private Sub Worksheet_Activate()
      
     Worksheets("Summary").Unprotect
        
        Range("CommentBox").Formula = "=IF(ISERROR(VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)), ""n/a"", (VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)))"
        Range("CommentBox").Calculate
        
     Worksheets("Summary").Protect
    
    End Sub
    Now, the code works, but only if I select another sheet and then go back. What I essentially need to do is combine the two so the user doesn't have to select a different sheet!

    Any ideas?

    Thanks,
    John

  2. #2
    Forum Contributor
    Join Date
    02-24-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Automatically resizing rows when value changed

    Wouldn't this work?

     Selection.Rows.AutoFit
    Maybe
     .EntireRow.AutoFit
    Or are there any merged cells in the rows? Merged cells create problems. Use Center Across Selection instead.
    Last edited by Jim885; 12-06-2013 at 10:02 AM.

  3. #3
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Thanks for the reply. Yep - the cell is merged. I think it improves formatting and makes it look nicer... and it seems like I'm almost there with the merged cells anyway.

  4. #4
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Anyone?

    Thanks...

  5. #5
    Forum Contributor
    Join Date
    02-24-2013
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    317

    Re: Automatically resizing rows when value changed

    You can use CENTER ACROSS SECTION and achieve the same "looks" without the problems of merged cells. Merged cells and VBA don't get along.

  6. #6
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Thanks again, I've tried using center across selection, but the text is centered.

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Automatically resizing rows when value changed

    Hi, john_london,

    if you monitor a cell which has a formula inserted you would need to apply the Calculate-event as that would be triggered on any calculation/change of formula. Workshet_change would need to monitor both cells which might be changed.

    You might try to alter the code like
    '...
        With AutoFitRng
          .MergeCells = False
          Range(str01).Formula = "=IF(ISERROR(VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)), ""n/a"", (VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)))"
          Range(str01).Calculate
          CWidth = .Cells(1).ColumnWidth
          MergeWidth = 0
    '...
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  8. #8
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Hi Holger - thanks so much for replying. This is the code I used, but the row height stays the same? It seems it only updates when I have the calculate code you mention separately in the WorkSheet_Activate sub?

    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 = "CommentBox"
    
    Worksheets("Summary").Unprotect
    
      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
          Range(str01).Formula = "=IF(ISERROR(VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)), ""n/a"", (VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)))"
          Range(str01).Calculate
          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
        
     Worksheets("Summary").Protect
    
    End Sub
    So, when I have the original code that I posted it works (the row height adjusts) when I select a different sheet and then come back to the original worksheet. I would hope that the user can just update the cell and the row updates rather than having to switch sheets.

    Private Sub Worksheet_Activate()
      
     Worksheets("Summary").Unprotect
        
        Range("CommentBox").Formula = "=IF(ISERROR(VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)), ""n/a"", (VLOOKUP(Data!$A$39, all_data, MATCH(selected_date, Data!$1:$1, 0), FALSE)))"
        Range("CommentBox").Calculate
        
     Worksheets("Summary").Protect
    
    End Sub
    Last edited by john_london; 12-07-2013 at 09:36 AM.

  9. #9
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Could I maybe send you the Excel spreadsheet?

  10. #10
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Okay - I think it's best if I attach the spreadsheet and explain what's going on.

    So,if you open the spreadsheet and change the month from 'September' to 'August', you will see the comments are shorter but the height of the cell stays the same. If you then click on the 'Data' tab, then go back to the 'Summary' tab, the height of the cell correctly adjusts. What I want to get is this happening without having to click on the 'Data' tab and back to the 'Summary' tab!
    Attached Files Attached Files

  11. #11
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Automatically resizing rows when value changed

    Hi, john_london,

    change
      If Not Intersect(Target, Range(str01)) Is Nothing Then
    which will not be triggered as the cell monitored is changed by formula not by entry/edit/query to
      If Not Intersect(Target, Range("J3:K3")) Is Nothing Then
    which will monitor the cells changing to start the event.

    Ciao,
    Holger

  12. #12
    Registered User
    Join Date
    04-19-2010
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Automatically resizing rows when value changed

    Hi Holger. Thank you so much! This is perfect.

    Really appreciate your time and effort.

    Regards,
    John

  13. #13
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Automatically resizing rows when value changed

    Hi, John,

    if your problem has been resolved please mark this thread as Solved via Thread Tools over the first post in this thread.

    Ciao,
    Holger

+ 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. Automatically unhide rows based on a value that is changed by a formula
    By Sky188 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2013, 06:51 PM
  2. Problem with columns and rows automatically resizing
    By Fest911 in forum Excel General
    Replies: 10
    Last Post: 11-17-2012, 08:37 PM
  3. [SOLVED] Automatically resizing columns and rows
    By devm01@gmail.com in forum Excel General
    Replies: 1
    Last Post: 04-17-2006, 03:38 AM
  4. Automatically Resizing a Graph
    By PaulW in forum Excel General
    Replies: 1
    Last Post: 04-13-2006, 11:20 AM
  5. [SOLVED] resizing row height automatically?
    By Gumby in forum Excel General
    Replies: 2
    Last Post: 07-11-2005, 11:05 AM

Tags for this Thread

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