VBA is OOP (sort of a poor version)
I think the problem is that the named ranges are not on the sheet being changed so Intersect doesn't work
Also I don't think you want to use the workbook event, but instead maybe use the worksheet event for the worksheet where the named ranges are
The Init variables sub would normally be in a standard module (IMO anyway)
Try to avoid On Error Resume Next unless really necessary
1. Your original with tests for the sheet that was changed
Option Explicit
Dim RngRoster As Range, RngCategory As Range, RngCategoryList As Range
Dim RefRoster As String, RefCategory As String, RefCategoryList As String
Public Sub Initialize_Variables()
' On Error Resume Next
Set RngRoster = ThisWorkbook.Names("RangeRoster").RefersToRange
Set RngCategory = ThisWorkbook.Names("RangeCategory").RefersToRange
Set RngCategoryList = ThisWorkbook.Names("CategoryList").RefersToRange ' iconsistant name?
RefRoster = ActiveWorkbook.Names("RangeRoster")
RefCategory = ActiveWorkbook.Names("RangeCategory")
RefCategoryList = ActiveWorkbook.Names("CategoryList")
' On Error GoTo 0
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Call Initialize_Variables
MsgBox "content changed! " & Target.Address & ", " & Sh.Name
'This will print the formula if you need it:
'MsgBox "named: " & ActiveWorkbook.Names("RngCategoryList")
'MsgBox "named: " & ActiveWorkbook.Names("RngCategory")
'MsgBox "named: " & ActiveWorkbook.Names("RngRoster")
If RngRoster.Parent Is Sh Then
If Not Intersect(Target, RngRoster) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
End If
If RngCategory.Parent Is Sh Then
If Not Intersect(Target, RngCategory) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
End If
If RngCategoryList.Parent Is Sh Then
If Not Intersect(Target, RngCategoryList) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
End If
End Sub
2. Standard module (note the Public scope) and no On Error
Option Explicit
Public RngRoster As Range, RngCategory As Range, RngCategoryList As Range
Public RefRoster As String, RefCategory As String, RefCategoryList As String
Public Sub Initialize_Variables()
' On Error Resume Next
Set RngRoster = ThisWorkbook.Names("RangeRoster").RefersToRange
Set RngCategory = ThisWorkbook.Names("RangeCategory").RefersToRange
Set RngCategoryList = ThisWorkbook.Names("CategoryList").RefersToRange ' iconsistant name?
RefRoster = ActiveWorkbook.Names("RangeRoster")
RefCategory = ActiveWorkbook.Names("RangeCategory")
RefCategoryList = ActiveWorkbook.Names("CategoryList")
' On Error GoTo 0
End Sub
3. The worksheet change event on the sheet where the named ranges are
This only fires is a cell on this sheet is changed
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Call Initialize_Variables
MsgBox "content changed! " & Target.Address
'This will print the formula if you need it:
'MsgBox "named: " & ActiveWorkbook.Names("RngCategoryList")
'MsgBox "named: " & ActiveWorkbook.Names("RngCategory")
'MsgBox "named: " & ActiveWorkbook.Names("RngRoster")
If Not Intersect(Target, RngRoster) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
If Not Intersect(Target, RngCategory) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
If Not Intersect(Target, RngCategoryList) Is Nothing Then
MsgBox "A name was added or changed!"
Exit Sub
End If
End Sub
If you decide to try 2 and 3, remember to delete/comment out the ThisWorkbook event handler