How to Create a Word VBA If Statement Based on Line Count
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.

- 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.
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.
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.
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.
Declare an integer variable and assign it the line count of the current selection using: Dim lineCount As Integer lineCount = Selection.Range.ComputeStatistics(wdStatisticLines).
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."
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.

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. Download and Install WPS Office: Visit the official WPS Office website to download and install the software.
- 2. Open WPS Writer: Launch WPS Writer and open the document where you want to apply or create the macro.
- 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. Run Your Macro: Use the built-in macro runner within WPS Writer to execute your script seamlessly on your document selections.

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.




