Reputation: 1886
I have an IF Else statement that checks if a spreadsheet exists in the workbook. If it does exist, it is meant to run a particular macros, and then stop. If the spreadsheet does not exist, it should run to the end of the IF statement, and then generate a MSGBOX with an error message.
I can't get the IF statement to end once the condition is satisfied, and the macro has run.
The code is:
Dim k As Integer
Dim t As String
Dim x As Integer
k = Sheets.Count
x = 1
While x <= k
t = Sheets(x).Name
If t = "Details" Then
Call Details_1
Else
x = x + 1
End If
Wend
If x > k Then
MsgBox "Something seems to have gone wrong." & vbCrLf & "Please ensure that you have pressed 'Button 1' before you press any of the other buttons", vbOKOnly, "ERROR MESSAGE"
End If
Upvotes: 0
Views: 1284
Reputation: 892
Move x = x + 1
so that it is after the first End If
. Right now after the macro is called the x = x + 1
doesn't get executed and x is then always <= k.
Upvotes: 0
Reputation: 61
I am assuming that you mean it calls Details_1 and then continues the While loop, which you don't want. If so then I would suggest you change your code to something like this:
Dim k As Integer
Dim t As String
Dim x As Integer
k = Sheets.Count
x = 1
Do While x <= k
t = Sheets(x).Name
If t = "Details" Then
Call Details_1
Exit Do
Else
x = x + 1
End If
Loop
If x > k Then
MsgBox "Something seems to have gone wrong." & vbCrLf & "Please ensure that you have pressed 'Button 1' before you press any of the other buttons", vbOKOnly, "ERROR MESSAGE"
End If
This code should cycle through your sheets until it finds Details, call Details_1 and then continue with the next section of code
Upvotes: 1