Dirk Douwes
Dirk Douwes

Reputation: 33

VBA Scenario Deleter Macro only Deletes odd macros

Hello I wrote a VBA Macro to delete my What if Scenarios I have 84 of them and they are named 1,2,3,4,....,84 so I wrote this code

Sub Dismantle()
For Count = 1 To 84
    ActiveSheet.Scenarios(Count).Delete
Next

End Sub

But it only deletes scenarios 1,3,5,7,9.....,83 then returns the error: Unable to get Scenarios property of the worksheet class. This means there is no scenario with the name count for it to delete (I think anyway)
Running again it deletes the first third ect again so its skipping every second scenario.

Upvotes: 1

Views: 454

Answers (1)

Cor_Blimey
Cor_Blimey

Reputation: 3310

Sub Dismantle()
    For Count = 84 to 1 step -1
        ActiveSheet.Scenarios(Count).Delete
    Next
End Sub

You were so close :-)

Or, for more flexibility:

Do While ActiveSheet.Scenarios.Count > 0
    ActiveSheet.Scenarios(1).Delete()
Loop

Upvotes: 3

Related Questions