How to Set Row Heights Based on Cell Text Using Excel VBA Macros
Question details
The user needs an Excel VBA macro that searches a column for specific keywords and dynamically adjusts the row height for the matching cells.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Adjusting row heights automatically after applying advanced filters, where the exact cell positions containing specific text change dynamically.
- Observed behavior
- The user wants to replace static cell references with a dynamic search approach that finds multiple keywords and adjusts their respective row heights without failing if a keyword is missing.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools in your ribbon to access the VBA Editor.
Use VBA Find Method to Dynamically Adjust Row Heights
Create a VBA macro that uses the Find function to search for specific text strings and adjusts the row height for every match found.
Using the Find and FindNext methods allows Excel to dynamically locate text even after advanced filters have changed row positions. Using wildcards (asterisks) helps find partial matches, and setting MatchCase ensures accuracy.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor. Insert a new Module by clicking Insert > Module from the top menu.
Define your sub procedure and use the Range.Find method on your target column (e.g., Columns("A")). Specify the keyword you are looking for. To find partial matches, wrap your keyword in wildcards like "*YourKeyword*" and set MatchCase:=True if case sensitivity is required.
Create a Do...While loop using FindNext to iterate through all matching cells in the column. Ensure your loop checks that the found cell's address does not equal the first found address to prevent an infinite loop.
Inside the loop, assign the new row height using the RowHeight property of the matched cell, such as FoundCell.RowHeight = 25. Add an 'If Not FoundCell Is Nothing Then' check so the macro continues running smoothly even if a specific keyword is completely absent from the filtered data.

Easily Run and Manage VBA Macros with WPS Spreadsheet
WPS Spreadsheet fully supports VBA macros, allowing you to automate repetitive tasks like dynamically adjusting row heights based on cell text. It offers a seamless development environment that reads and executes your existing Excel macro code without modification.
- 1. Open your Workbook in WPS Office: Launch WPS Spreadsheet and open your existing .xlsm file.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon menu and click 'Visual Basic' to launch the VBA Editor.
- 3. Insert and Run the Macro: Paste your dynamic row-height adjustment macro directly into a module within the editor.
- 4. Execute to Format Data: Save the file and run the macro to instantly format the row heights of your filtered data.

Frequently Asked Questions
Why does my VBA macro fail when the target keyword isn't found?
If the Find method does not locate a match, it returns 'Nothing'. If you try to set the RowHeight property on 'Nothing', the code throws a runtime error. You must wrap your row height adjustment and FindNext logic inside an 'If Not FoundCell Is Nothing Then' conditional statement.
How can I make my macro search for multiple different keywords at once?
You can create an array of your target keywords (e.g., Array("Keyword1", "Keyword2", "Keyword3")) and use a For Each loop to iterate through the array. This allows the macro to run the Find and FindNext commands for each word sequentially without writing separate code blocks for each word.
Does the Find method work on hidden rows after applying an advanced filter?
By default, the Find method often searches through visible cells when LookIn is set to xlValues. If you need to search and adjust hidden rows as well, ensure you specify LookIn:=xlFormulas in your Find parameters to check the underlying cell contents regardless of visibility.




