Ethun_Hunt
New Member
I am trying to get number of dependents of selected range (if number of cells in selection is one). When I use following code it works without any error for all cells (for cells without any dependents and for cells having dependents)
But when same logic is used in Worksheet_SelectionChange (as shown in following code)
it works for cells not having any dependents, but for cells having dependents "Dependents.Count" restarts macro. After executing
Statement, macro restarts and executes
again. My question is why macro is restarting in second case after executing "Dependents.Count"?
For "Worksheet_SelectionChange" I have put the code in Sheet Code area not in module.
Code:
'Case 1
Sub Example()
Dim rng As Excel.Range
Set rng = Excel.SelectionIf Target.Count =1 Then
If HasDependents(rng) Then
MsgBox rng.Dependents.Count &" dependancies found."
Else
MsgBox "No dependancies found."
End If
End If
End Sub
Public Function HasDependents(ByVal Something As Excel.Range)As Boolean
On Error Resume Next
HasDependents = Something.Dependents.Count
End Function
But when same logic is used in Worksheet_SelectionChange (as shown in following code)
Code:
'Case 2
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Count =1 Then
If HasDependents(Target) Then
MsgBox Target.Dependents.Count &" dependancies found."
Else
MsgBox "No dependancies found."
End If
End If
End Sub
Public Function HasDependents(ByVal Something As Excel.Range)As Boolean
On Error Resume Next
HasDependents = Something.Dependents.Count
End Function
it works for cells not having any dependents, but for cells having dependents "Dependents.Count" restarts macro. After executing
Code:
HasDependents = Something.Dependents.Count
Code:
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
For "Worksheet_SelectionChange" I have put the code in Sheet Code area not in module.