Need VBA Code for dynamic row height

dsubash

New Member
Joined
Nov 22, 2024
Messages
35
Office Version
  1. 2019
  2. Prefer Not To Say
Platform
  1. Windows
Hi,

I am looking for a code to have a dynamic row height in my report. I have a database with certain criterias. I also have multiple sheets with filtered data. The report runs from row 9 on each sheet.

I need the default row height to be 12 from row 9 to end of report. I also have fields which has wrap text in them.

So I am basically looking for a code that would allow me to have a default row height of 12 for all the rows and whereever wrap text is available in a row, then the row height should automatically autoadjust to fit the contents.

I have this code which is embedded into the main code, but it is not working properly. Any suggestions on how to correct this code?


VBA Code:
Sub AdjustRowHeights(ws As Worksheet, lastRow As Long)
Dim r As Range
Dim cell As Range
Dim hasWrapText As Boolean

ws.Rows("9:" & lastRow).RowHeight = 12

For Each r In ws.Range("C9:E" & lastRow).Rows
hasWrapText = False

For Each cell In r.Cells
If cell.WrapText = True And cell.Value <> "" Then
hasWrapText = True
Exit For ' No need to check other cells in this row
End If
Next cell

If hasWrapText Then
r.EntireRow.AutoFit

End If
Next r
End Sub

This code works fine the autofit incase of availablility of wrap text, but the default row height of 12 is not maintained

Thanks in advance.
Subash D
 

Forum statistics

Threads
1,226,796
Messages
6,193,048
Members
453,772
Latest member
aastupin

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