Article ID: 158638
Article Last Modified on 11/23/2006
Sub CheckArrayofNames()
'Set the range to which you want to apply names.
Set Range1 = Range("B1:B5")
'Assume that none of the names exist.
OneNameExists = False
'Set the array of names you want to apply.
MyArray = Array("Alpha", "Bravo", "Charlie")
'Prevent the macro from stopping if a name doesn't exist.
On Error Resume Next
'For each name we want to apply...
For Each xItem In MyArray
'For each defined name in the workbook...
For Each yName In ActiveWorkbook.Names
'If a match exists, then...
If xItem = yName.Name Then
'A name that you are applying exists, so exit
'the loop.
OneNameExists = True
Exit For
End If
Next yName
If OneNameExists = True Then Exit For
Next xItem
'Re-enable normal error handling.
On Error GoTo 0
'If one of the names you are applying exists, then...
If OneNameExists = True Then
'...apply names now.
Range1.ApplyNames MyArray
End If
End Sub
Range("B5").ApplyNames "Alpha"
Any reference to cell A1 is replaced by a reference to the defined name
"Alpha".
Range("B5").ApplyNames Array("Alpha", "Bravo", "Charlie")
If you create an array, and none of the names specified in the array exist
in the active workbook, you will receive an invalid page fault and
Microsoft Excel will stop responding. To prevent this behavior from
occurring, verify that at least one of the names specified in the array
actually exists in the active workbook.
Additional query words: XL97 crash hang EXCEL EXE
Keywords: kbbug kbdtacode kberrmsg kbfix kbprogramming KB158638