Adjust Row Height based on cell input

judgejustin

Board Regular
Joined
Mar 3, 2014
Messages
155
I have a sheet that I am creating using a macro, from a template. There are 3 rows with merged cells in them that I am having data transferred to. They range in amounts of data dramatically. I nee the row height to automatically adjust for each of the rows dependent on the amount of data that is put into them. Because of the set up of the sheet I don't have a choice to do anything except merged cells in these rows. The rows are row 3, 5, and 7. Merged cells are B3:F3, B5:F5, and B7:F7.
Is there anything I can do to automatically size these once the data has been put into them?
 
Hey there judgejustin,

You can automatically fit the row height selecting the rows you want to size, then navigate to Home --> Cells --> Format and select AutoFit Row Height.

Hope this helps :D
 
Upvote 0
I assume that since your using a macro to setup your sheet that you want a VBA solution?

Since you've got lots of real estate in an excel sheet I would tackle it by creating single cells to simulate the merged cells somewhere not visible to the user, and then have your VBA resize the row based on that cell.
So:
  1. get your initial macro to dump the same data your transferring into the merged cells into single cells on the same rows as the merged cells. So for example since your transferring into merged cells B3:F3, B5:F5 and B7:F7, also transfer that data into AA3, AA5 and AA7.
  2. Make the width of the single cell versions equal to the width of each merged cell. Since you've only got 5 merged columns it's probably easiest to just add up the width of each column and then set the single cells to that width. You now have a set of single celled versions of the merged cells, on the same rows, but with the same column width.
  3. Each time your macro dumps some data, call a small bit of code to resize the row, which it will do correctly based on the contents of the single cells. Like so:
VBA Code:
Private Sub judgejustins_macro()
    Dim rng As Range
    '
    ' Dump data into merged cells B3:F3.
    ' Dump the same data into AA3
    ' Set value for rng variable and pass to row fitting sub
    '
    Set rng = Range("AA3")
    Call MergeAutofit(rng)
    '
    ' Repeat data dump for B5:F5 and AA5
    '
    Set rng = Range("AA5")
    Call MergeAutofit(rng)
    '
    ' Repeat all for row 7
    '
    Set rng = Range("AA7")
    Call MergeAutofit(rng)
End Sub
Private Sub MergeAutofit(rng As Range)
    If Not rng.EntireRow.Hidden = True Then
        rng.EntireRow.AutoFit
    End If
End Sub

PS. If you add your Excel version to your profile it's very helpful to those proposing solutions (not so much in this case but for the future).
 
Upvote 0
Hey there judgejustin,

You can automatically fit the row height selecting the rows you want to size, then navigate to Home --> Cells --> Format and select AutoFit Row Height.

Hope this helps :D
That won’t work for merged cells.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top