VBA & Macro Problems

How to Use VBA Macro to Set Row Height Based on Specific Text in Excel

Khadija KhanKhadija Khan Nov 2, 2026 869 views

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.

How to Use a VBA Macro to Set Row Height Based on Specific Text
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Visual Basic Editor

Press ALT + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click 'Insert' and select 'Module' to create a blank window for your code.

3
Paste the VBA 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

4
Modify the Parameters (Optional)

Change 'A1:J1' to your actual search range, replace 'Adjust the expiration' with your target text, and change '40' to your desired row height.

5
Run the Macro

Press F5 or go to 'Run' > 'Run Sub/UserForm' from the top menu to execute the macro and update your row heights.

Use a VBA Loop to Find Text and Set Row Height
Handling Multiple Rows: If you want the macro to set the row height for every matching cell rather than just the first one it finds, simply delete the 'Exit For' line from the code.
Advanced Spreadsheet Capabilities

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. 1. Install WPS Office: Download and install the free version of WPS Office from the official website.
  2. 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing Macro-Enabled Workbook (.xlsm).
  3. 3. Enable the Developer Tools: Navigate to the Developer tab to seamlessly view, edit, and run your VBA macros.
High compatibility with Microsoft Excel (.xlsx, .xls, .xlsm) formatsRobust macro support for complex data automationLightweight installation with a highly intuitive and familiar interface
microsoft office alternative - wps office

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.