VBA & Macro Problems

How to Change Excel Font Size Based on Word Count Using VBA

Khadija KhanKhadija Khan Oct 28, 2026 869 views

Question details

The user needs to write a VBA macro to automatically adjust the font size of specific cells based on the number of slash-separated words they contain.

How to Change Excel Font Size Based on Word Count with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
Automating font size adjustments in a specific cell range (e.g., B10:B21) depending on whether the cell contains multiple words separated by a slash.
Observed behavior
The user's standard worksheet formula works, but their initial VBA implementation using WorksheetFunction.Substitute is failing and requires correct native VBA syntax.
Before you start

Ensure you have the Developer tab enabled in your spreadsheet software and back up your workbook before writing or running new macro codes.

Solution 1Recommended

Use the Native VBA Replace Function to Count Slashes

Use the built-in VBA Replace function instead of calling WorksheetFunction.Substitute to calculate character differences and adjust font sizes dynamically.

VBA provides native string manipulation functions which are faster and more reliable than calling Excel worksheet functions from within a macro. By calculating the length of the cell text before and after removing a specific delimiter (like a slash "/"), you can accurately determine if multiple words exist and trigger a font size change.

1
Open the VBA Editor

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

2
Insert or Edit a Module

In the Project Explorer pane on the left, double-click the module containing your existing macro, or click 'Insert' > 'Module' to create a new one.

3
Apply the VBA Code

Use a For loop to iterate through your target range (e.g., For i = 10 To 21) and insert the following conditional logic inside the loop: If Len(Range("B" & i).Value) - Len(Replace(Range("B" & i).Value, "/", "")) > 0 Then Range("B" & i).Font.Size = 10 Else Range("B" & i).Font.Size = 14 End If

4
Run the Macro

Close the editor and press ALT + F8 to select and run your macro. The font sizes in the specified cells will instantly update based on the character logic.

Use the Native VBA Replace Function to Count Slashes
Execution Successful: This script will dynamically scale down the font to size 10 if slashes (multiple words) are found, and enlarge it to size 14 if none are present.
Advanced Macro Support

Automate Formatting Seamlessly with WPS Spreadsheet

WPS Spreadsheet offers robust support for VBA and macros, allowing you to run complex automation scripts like dynamic font resizing without compatibility issues. It is an excellent alternative for heavy data manipulation.

  1. 1. Open your workbook in WPS Spreadsheet: Launch WPS Office and open your .xlsm file containing the data you want to format.
  2. 2. Access the Developer tools: Navigate to the 'Developer' tab on the top ribbon menu.
  3. 3. Open the VBA Editor: Click on the 'VBA Editor' icon to open the coding window.
  4. 4. Run your macro: Paste your custom VBA script into the module and click the 'Run' button (or press F5) to automatically adjust your font sizes.
Fully compatible with Microsoft Excel macro-enabled files (.xlsm formats).Includes a built-in, easy-to-use VBA Editor for writing and debugging scripts.Lightweight architecture ensures that complex loops run quickly and smoothly.Free to download with a highly familiar, intuitive user interface.
microsoft office alternative - wps office

Frequently Asked Questions

Why use Replace in VBA instead of WorksheetFunction.Substitute?

Replace is a native VBA string function, meaning it processes faster and requires less syntax overhead within the VBA environment than invoking the Excel-specific WorksheetFunction.Substitute method.

How can I change the VBA code to look for spaces instead of slashes?

You can change the delimiter inside the Replace function. Simply modify `Replace(Range("B" & i).Value, "/", "")` to `Replace(Range("B" & i).Value, " ", "")`. This will calculate the difference based on spaces, accurately reflecting standard word counts.

How do I apply this macro to a different range of cells?

You need to modify the loop parameters and column references in your code. For example, to check cells C1 to C50, change your loop definition to `For i = 1 To 50` and update `Range("B" & i)` to `Range("C" & i)` throughout the script.