Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90280

VBA: Worksheet_Change - Target.Address vs Working with Intersect

$
0
0

I am successfully running a macro that includes the following piece of code:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$1" Or Target.Address = "$B$3" Then
        Call MainSub
    End If
End Sub

The point of this sub is to make the macro spring into action via calling MainSub when Cell B1 or B3 are changed. It works.

However, when looking for a solution on how to do this, I also came across this code:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("B1")) Is Nothing Then
        Call MainSub
    End If
End Sub

It works just as well with only B1.

Now I have two questions:

1) How would I include another cell (B3) to check in the intersect method? Using or didn't work for me, so I think another if-Statement after the first one would do the trick, but is there a more elegant solution?

2) What's the advantage of using intersect vs simply looking at Target.Address?


Viewing all articles
Browse latest Browse all 90280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>