VBA & Macro Problems

Break Long Excel VBA Intersect Statements Across Lines

Chanuka GeekiyanageChanuka Geekiyanage Nov 18, 2026 868 views

Question details

The user needs to split a long Excel VBA Intersect condition into readable range variables and continued lines.

Break Long Excel VBA Intersect Statements Across Lines
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A worksheet macro checks multiple rows and columns as part of a cell-protection workflow.
Observed behavior
The condition should remain valid VBA while being easier to read and maintain.
Before you start

Identify the rows, columns, and worksheet your condition should cover. Open the VBA editor with Alt+F11 and locate the worksheet event or macro containing the condition.

Solution 1Recommended

Build Separate Ranges and Continue the VBA Statement

Use this approach to make a long Intersect condition easier to read and update.

The example checks rows 2–3 and columns B and D within rows 11–6000. Replace these sample addresses with the ranges needed in your worksheet.

A VBA line continuation consists of a space followed by an underscore at the end of a line. You cannot put a line break inside a quoted string; instead, join separate strings with the & operator.

1
Open the worksheet module

Press Alt+F11, then double-click the relevant worksheet under Microsoft Excel Objects in the Project Explorer.

2
Define and check the ranges

In the worksheet module, adapt this example to your existing Worksheet_Change event. If the event already exists, merge the range declarations and check into it instead of creating a duplicate: Private Sub Worksheet_Change(ByVal Target As Range) Dim ws As Worksheet Dim protectedRows As Range Dim selectedColumns As Range Dim protectedColumns As Range Dim protectedCells As Range Set ws = Me Set protectedRows = ws.Rows("2:3") Set selectedColumns = Union( _ ws.Columns("B"), _ ws.Columns("D")) Set protectedColumns = Intersect( _ selectedColumns, ws.Rows("11:6000")) Set protectedCells = Union( _ protectedRows, protectedColumns) If Not Intersect(Target, protectedCells) Is Nothing Then MsgBox "The changed cells overlap the selected ranges." End If End Sub

3
Test the condition

Return to the worksheet and change a cell in one of the sample ranges, such as B11, then a cell outside them, such as C11. Adjust the addresses in the VBA editor if the results do not match your intended coverage.

Build Separate Ranges and Continue the VBA Statement
The example detects changes: Worksheet_Change runs after a cell is changed. The message does not prevent edits or protect cells; use Excel's worksheet protection settings if you need to block changes.
Free Microsoft Office alternative

Work with Excel Spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for a smooth move from Microsoft Office. It can open common Excel formats, but it does not fix Excel VBA syntax or guarantee that Excel macros will run unchanged.

Open and edit spreadsheets with a familiar interfaceCompatible with common Microsoft Excel formats, including XLS and XLSXReview worksheet ranges and organize spreadsheet dataUse a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

How do I continue a VBA statement on the next line?

In the VBA editor, type a space and an underscore at the end of the line, then press Enter. Continue the statement on the next line at a valid break point, such as after an operator or list separator.

Can I split a quoted string across VBA lines?

Not inside the quotation marks. Close the first string, add the & concatenation operator and a space followed by an underscore, then start the next quoted string on the following line.

What is the difference between Union and Intersect?

Union combines ranges, while Intersect returns only their overlapping cells. For example, use Union to combine columns B and D, then Intersect with rows 11:6000 to limit the result to those rows.

Does the Worksheet_Change example stop users from editing cells?

No. Worksheet_Change runs after an edit, and the example only displays a message. To restrict editing, configure locked cells and use Review > Protect Sheet in Excel.

How do I keep the VBA code when saving the workbook?

In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Enable macros only when you trust the workbook.