Nested If Statements with Intersect

crookesa

Board Regular
Joined
Apr 11, 2013
Messages
88
I am watching a number of named ranges on one sheet and want to try make my code a simple as possible.

Each named range I am watching corresponds to a day of the month so it can be up to 31 named ranges.

I have the following working for one range

VBA Code:
If Not Intersect(Target, Range("day_1")) Is Nothing Then
        Call ThisWorkbook.update_day_duties(1)
End If

What I was trying to do was

VBA Code:
If Not Intersect(Target, Range("day_1")) Is Nothing Then
        Call ThisWorkbook.update_day_duties(1)
Else If Not Intersect(Target, Range("day_2")) Is Nothing Then
        Call ThisWorkbook.update_day_duties(2)
.... and so on
End If

I get the error "Must be first statement on the line".
Is it ok to list the first code snippet 31 times to watch for each named range (even if it doesn't exists, e.g. 28 days in feb) or is that inefficient.

I will have the same code running on 2 other sheets as each sheet represents a different employee grade and has to be separated like that.
Also, should I be looking for other problems regarding error catching or exiting subs, etc.
First time using this intersect function on Worksheet Changes.
 
In that case, I would stick with the IF, ElseIf.
If each named range was a single cell, then I would have suggested another approach, but it doesn't work if the named range is more than 1 cell.
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
In that case, I would stick with the IF, ElseIf.
If each named range was a single cell, then I would have suggested another approach, but it doesn't work if the named range is more than 1 cell.
Thanks for the suggestion, maybe I might find it useful elsewhere?
 
Upvote 0
You could use something like
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range
    
    Set Rng = Union(Range("Day_1"), Range("Day_2"), Range("Day_3"))
    If Not Intersect(Target, Rng) Is Nothing Then
        MsgBox Target.Name.Name & vbLf & Split(Target.Name.Name, "_")(1)
    End If
End Sub
 
Upvote 0
You could use something like
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Rng As Range
   
    Set Rng = Union(Range("Day_1"), Range("Day_2"), Range("Day_3"))
    If Not Intersect(Target, Rng) Is Nothing Then
        MsgBox Target.Name.Name & vbLf & Split(Target.Name.Name, "_")(1)
    End If
End Sub
Thanks I'll keep it in the treasure chest.
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,934
Members
449,275
Latest member
jacob_mcbride

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top