Conditional Row Height

  • Thread starter Thread starter dhstein
  • Start date Start date
D

dhstein

I have a cell that may have no data or from 1 to 6 lines of data. Is there a
way to make the Row Height conditional based on the number of lines of data
in the cell? Thanks.
 
In Excel 2007, highlight the cells, column or entire worksheet where you want
the row height to adjust. On the Home tab, in the Cells group, click Format.
Under Cell Size, click Autofit Row Height. (Taken from Excel 2007 Help
under "Change the row height to fit the contents".)

Your row height will automatically adjust to fit your cell contents.
 
Try
'The macro lloks for ASCII character 10 (ALT-ENTER)
'and adjusts height accordingly

Sub rowHeight()
Dim str As String
Dim i, j, k, ht, lastRow As Long
With ActiveSheet
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
End With
For k = 1 To lastRow
Cells(k, 1).Select
str = Selection.Value
j = 0
For i = 1 To Len(str)
c = Mid(str, i, 1)
If c = Chr(10) Then
j = j + 1
End If
Next
If j = 0 Then
ht = 0
Else
ht = 12.8 * j
End If
Selection.rowHeight = ht
Next
End Sub
 
THenr and Sheloo. Thank you so much for responding. The Autofit Row Height
is exactly what I need, but I will also save the macro for use with another
spread sheet that I have. Thanks to you both.
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top