How to Use VBA Macro to Set Row Height Based on Specific Text in Excel
Question details
The user wants to use a VBA script to search for specific text within a range and automatically adjust the row height when a match is found.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating row height adjustments when working with worksheets that contain specific keywords, particularly because merged cells do not autofit correctly.
- Observed behavior
- Standard autofit features fail to adjust the row height correctly when merged cells are involved, requiring explicit row height definitions via VBA loops.
Ensure you have the Developer tab enabled in your ribbon and remember to save your file as a Macro-Enabled Workbook (.xlsm) so your VBA code is not lost upon closing.
Use a VBA Loop to Find Text and Set Row Height
This method utilizes a 'For Each' loop in VBA to evaluate each cell in a specified range. When the cell's value matches the target text, it explicitly sets the height of that entire row.
Because merged cells disable Excel's standard AutoFit functionality, writing a custom VBA macro is the most reliable workaround. The script iterates through a defined range, checks for a specified string, and modifies the row height attribute accordingly.
Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.
In the top menu, click 'Insert' and select 'Module' to create a blank window for your code.
Copy and paste the following code into the module window: Sub SetRowHeight() Dim cel As Range For Each cel In Range("A1:J1") If cel.Value = "Adjust the expiration" Then cel.EntireRow.RowHeight = 40 Exit For End If Next cel End Sub
Change 'A1:J1' to your actual search range, replace 'Adjust the expiration' with your target text, and change '40' to your desired row height.
Press F5 or go to 'Run' > 'Run Sub/UserForm' from the top menu to execute the macro and update your row heights.

Automate Your Worksheets Seamlessly with WPS Spreadsheet
WPS Office offers a powerful spreadsheet application that fully supports VBA macros in its advanced versions, allowing you to run custom scripts, automate formatting, and manage row heights exactly as you would in Microsoft Excel.
- 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
- 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing Macro-Enabled Workbook (.xlsm).
- 3. Enable the Developer Tools: Navigate to the Developer tab to seamlessly view, edit, and run your VBA macros.

Frequently Asked Questions
Why doesn't the AutoFit feature work on merged cells?
By default, spreadsheet applications ignore merged cells when calculating AutoFit dimensions to avoid conflicting column widths or row heights. Using a VBA macro is the standard workaround to enforce specific row heights for merged cells.
How can I search an entire column instead of a small range?
You can change the range in the VBA code from Range("A1:J1") to Range("A:A") to search the entire column A. However, be cautious as searching an entire column row by row may slow down the macro's execution time.
Will this macro differentiate between uppercase and lowercase text?
Yes, standard VBA text comparisons are case-sensitive. To make the search case-insensitive, you can add 'Option Compare Text' at the very top of your module, above the 'Sub SetRowHeight()' line.




