문제

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
도움이 되었습니까?

해결책

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

다른 팁

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?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top