I have some vba code I want to loop over different scopes depending on the value of a boolean. I want it to look something like this:
Sub Loop()
ActiveSheet.Select
Dim LoopScope as Collection
If Boolean = True then
LoopScope = ActiveSheet.ChartObjects
Else
LoopScope = Selection
End if
For Each ChartObject In LoopScope
*Some code*
Next ChartObject
End Sub
The error message tells me I can only iterate over collection objects, which makes sense, however I can't figure out how to dim LoopScope as a collection object (?). The loop works both when typing:
For Each ChartObject in Selection
and:
For Each ChartObject in ActiveSheet.ChartObjects
I only can't figure out how to make the scope dependent on my boolean. Thank you in advance for your time.
Selectionare you anticipating here? Are you just missingSeton yourLoopScopeassignment? If that's just pseudocode can you post the actual code you tried?