Break Long Excel VBA Intersect Statements Across Lines
Question details
The user needs to split a long Excel VBA Intersect condition into readable range variables and continued 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.
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.
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.
Press Alt+F11, then double-click the relevant worksheet under Microsoft Excel Objects in the Project Explorer.
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
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.

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.

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.




