Search code examples
excelvba

Adding multiple ranges into one argument


I have this working code:

If Not Intersect(ActiveCell, Range("D6:D15")) Is Nothing Then
    MsgBox "busses"
Else
    MsgBox "Select Only Cells With Asset ID", vbCritical
    Exit Sub
End If

I would like to add more ranges into that line, like so:

If Not Intersect(ActiveCell, Range("D6:D15") Or ("G6:G15") Or ("J6:J15")) Is Nothing Then
    MsgBox "busses"
Else
    MsgBox "Select Only Cells With Asset ID", vbCritical
    Exit Sub
End If

Solution

  • You can define a range composed of different cells:

    If Not Intersect(ActiveCell, Range("D6:D15,G6:G15,J6:J15")) Is Nothing Then
        MsgBox "busses"
    Else
        MsgBox "Select Only Cells With Asset ID", vbCritical
        Exit Sub
    End If