Office Settings & Configuration

How to Create a Word VBA If Statement Based on Line Count

Algirdas JasaitisAlgirdas Jasaitis Oct 10, 2026 869 views

Question details

The user wants to write a Word VBA conditional statement that evaluates the total number of lines in a text selection or document and displays a message based on that count.

How to Create a Word VBA If Statement Based on Line Count
Product
Word
Device & OS
not provided
Scenario
Automating document formatting and validation tasks using VBA macros.
Observed behavior
A VBA script determines the line count within a specified text range and executes a specific command (such as a MsgBox) when the line count meets the defined If statement criteria.
Before you start

Ensure that the Developer tab is enabled in your Word processor's ribbon, as you will need access to the Visual Basic for Applications (VBA) editor to write and run the macro.

Solution 1Recommended

Use VBA ComputeStatistics to Count Lines and Apply an If Statement

This method uses the built-in ComputeStatistics property to accurately count the lines in a selected range and triggers a message box based on the result.

To evaluate line counts efficiently, you can use the ComputeStatistics method combined with wdStatisticLines. Once the total line count is calculated and stored in a variable, an If statement can be utilized to execute specific commands, such as displaying an alert or formatting the text.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor. Click on Insert > Module from the top menu to create a blank macro module.

2
Write the Line Counting Logic

Declare an integer variable and assign it the line count of the current selection using: Dim lineCount As Integer lineCount = Selection.Range.ComputeStatistics(wdStatisticLines).

3
Add the If Statement

Below your calculation, add the conditional check. For example: If lineCount = 2 Then MsgBox "Exactly two lines are selected!" Else MsgBox "You selected " & lineCount & " lines."

4
Run the Macro

Close the VBA editor, highlight a portion of text in your document, go to the Developer tab, click Macros, select your newly created macro, and click Run.

Use VBA ComputeStatistics to Count Lines and Apply an If Statement
Accuracy Consideration: The ComputeStatistics method calculates lines based on how the text wraps on the page. Changing document margins or font sizes will alter the line count result.
Advanced Document Automation

Create VBA Macros Easily with WPS Writer

WPS Office provides robust compatibility with Microsoft VBA, allowing you to run custom macros, including line-counting scripts and conditional statements, to automate your document processing seamlessly.

  1. 1. Download and Install WPS Office: Visit the official WPS Office website to download and install the software.
  2. 2. Open WPS Writer: Launch WPS Writer and open the document where you want to apply or create the macro.
  3. 3. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on the VBA Editor icon to start writing your line-counting script.
  4. 4. Run Your Macro: Use the built-in macro runner within WPS Writer to execute your script seamlessly on your document selections.
Fully compatible with Microsoft Word (.doc, .docx) formats and standard VBA scripts.Supports advanced document automation for executing complex logic and macros.Lightweight software with a familiar, easy-to-navigate ribbon interface.Provides an efficient and free environment for writing, editing, and formatting documents.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA line count return the wrong number?

This often happens because word wrap, hidden formatting, or empty paragraph markers are calculated differently by the VBA engine. Using ComputeStatistics(wdStatisticLines) on a specific text range usually yields the most accurate results.

Can I count lines in the entire document instead of just a selection?

Yes. Instead of evaluating the Selection.Range, you can apply the ComputeStatistics(wdStatisticLines) method directly to ActiveDocument.Content to get the total number of lines in the file.

How do I trigger an action if the document has more than 100 lines?

You can write a simple macro utilizing the logic: If ActiveDocument.ComputeStatistics(wdStatisticLines) > 100 Then MsgBox "Document exceeds 100 lines." You can replace the MsgBox command with your desired formatting or validation actions.