Excel VBA: Dynamic Row Height Adjustment Based on Text Length for Multi-line Cells
I'm stuck on something that should probably be simple. I'm stuck on something that should probably be simple. I'm currently working with an scenario in Excel VBA where I need to automatically adjust the row height based on the content of multi-line cells in a specific range. I've tried using the `AutoFit` method, but it doesn't seem to work correctly for cells that contain line breaks. For example, if a cell contains text that wraps to multiple lines due to a line break (Alt + Enter), the row height doesn't adjust as expected. Here's the code snippet I've been using: ```vba Sub AdjustRowHeight() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") Dim rng As Range Set rng = ws.Range("A1:A10") ' Loop through each cell in the range Dim cell As Range For Each cell In rng cell.WrapText = True cell.Rows.AutoFit Next cell End Sub ``` When I run this code, I receive the behavior "Method 'Rows' of object 'Range' failed". I suspect it might be due to the way I'm trying to access the row property for each cell. I've also tried using `cell.EntireRow.AutoFit`, but it still doesn't adjust the height properly if there is a line break. I'm using Excel 2016. Any suggestions on how to effectively manage this, or if there's a better approach to ensure the row heights reflect the actual content length? Additionally, I want to maintain performance, as the range may expand significantly with more data in the future. How would you solve this? Is there a better approach? This is my first time working with Vba 3.11. Any feedback is welcome!