Frage

Is there anyway to terminate before the script has finished.

For Example:

Sub Macro1()

Range("A1").Select
If ActiveCell.Value Like "1" Then ActiveCell.Value = "0"
       ' Put some sort of termination of script here
If ActiveCell.Value Like "0" Then ActiveCell.Value = "1"

End Sub

If [A1] did contain "1" it would be changed to "0". But since the second if statement changes a "0" to a "1", this would revert what the first statement did in the first place.

What I'm trying to do is terminate after the first statement (if it returns as true). If not follow though the second statement...

I've tried "Else" but I can't seem to get that to work...

Sorry for my naivety, I don't have much experience with Visual Basic

EDIT: I have fixed it using a ElseIf statement as suggested.

Sub Macro1()


Range("A1").Select

If ActiveCell.Value Like "1" Then
    ActiveCell.Value = "0"
ElseIf ActiveCell.Value Like "0" Then
    ActiveCell.Value = "1"
End If


End Sub
War es hilfreich?

Lösung

You can use Exit Sub to stop early, but it think what you want is:

If ActiveCell.Value Like "1" Then 
    ActiveCell.Value = "0"
ElseIf ActiveCell.Value Like "0" Then 
    ActiveCell.Value = "1"
End If

Andere Tipps

You can use Exit Sub to quit the sub altogether. But in your example, perhaps instead of the second If statement, you could use Else If or just Else?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top