how to stop macro when "If" condition is met

Savuti

Board Regular
Joined
Apr 30, 2008
Messages
156
Hello all, I need to have this macro stop at the point that ("K7") = "okay" - otherwise it should continue running the rest of the code.
Help is much appreciated as always.


Code:
Application.ScreenUpdating = False
    ActiveSheet.Unprotect Password:="joker"
    If ActiveSheet.Range("K7").Value = "" Then ActiveSheet.Range("K7").Value = "okay"
    Range("E3").Select

    
    Dim iComNm As String
    Dim iUsrNm As String
    iComNm = ReturnComputerName
    iUsrNm = ReturnUserName
    MsgBox "You are logged in as the following..." & vbNewLine & _
    "Computer : " & iComNm & vbNewLine & _
    "Username : " & iUsrNm
   Sheets("Sheet1").Range("L7").Value = iComNm
   Sheets("Sheet1").Range("M7").Value = iUsrNm
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Just add
Code:
[/FONT]
[FONT=Courier New]Application.ScreenUpdating = False[/FONT]
[FONT=Courier New]ActiveSheet.Unprotect Password:="joker"[/FONT]
[FONT=Courier New]If ActiveSheet.Range("K7").Value = "" Then [/FONT]
[FONT=Courier New]ActiveSheet.Range("K7").Value = "okay"[/FONT]
[FONT=Courier New]Exit sub[/FONT]
[FONT=Courier New]else[/FONT]
[FONT=Courier New]end if[/FONT]
[FONT=Courier New]   Range("E3").Select[/FONT]
[FONT=Courier New]    Dim iComNm As String[/FONT]
[FONT=Courier New]    Dim iUsrNm As String[/FONT]
[FONT=Courier New]    iComNm = ReturnComputerName[/FONT]
[FONT=Courier New]    iUsrNm = ReturnUserName[/FONT]
[FONT=Courier New]    MsgBox "You are logged in as the following..." & vbNewLine & _[/FONT]
[FONT=Courier New]    "Computer : " & iComNm & vbNewLine & _[/FONT]
[FONT=Courier New]    "Username : " & iUsrNm[/FONT]
[FONT=Courier New]   Sheets("Sheet1").Range("L7").Value = iComNm[/FONT]
[FONT=Courier New]   Sheets("Sheet1").Range("M7").Value = iUsrNm[/FONT]
[FONT=Courier New][/FONT] 
[FONT=Courier New]
 
Upvote 0
Which line is giving you error? I think the part of the code below end if needs to be rechecked...:)
 
Upvote 0
Pedie, the word Else in the code is highlighted with the Compile Error message. The entire code works well, I just need it to stop if the word "okay" is present.
Thanks
 
Upvote 0
Code:
Sub try[/FONT][FONT=Courier New]Application.ScreenUpdating = False[/FONT]
[FONT=Courier New]Dim iComNm As String[/FONT]
[FONT=Courier New]Dim iUsrNm As String[/FONT]
 
[FONT=Courier New]ActiveSheet.Unprotect Password:="joker"[/FONT]
[FONT=Courier New]iComNm = ReturnComputerName[/FONT]
[FONT=Courier New]iUsrNm = ReturnUserName[/FONT]
[FONT=Courier New]If ActiveSheet.Range("K7").Value = "" Then[/FONT]
[FONT=Courier New]ActiveSheet.Range("K7").Value = "okay"[/FONT]
[FONT=Courier New]Exit Sub[/FONT]
[FONT=Courier New]Else[/FONT]
[FONT=Courier New]End If[/FONT]
[FONT=Courier New]   MsgBox "You are logged in as the following..." & vbNewLine & _[/FONT]
[FONT=Courier New]   "Computer : " & iComNm & vbNewLine & _[/FONT]
[FONT=Courier New]   "Username : " & iUsrNm[/FONT]
[FONT=Courier New]  Sheets("Sheet1").Range("L7").Value = iComNm[/FONT]
[FONT=Courier New]  Sheets("Sheet1").Range("M7").Value = iUsrNm[/FONT]
[FONT=Courier New]Application.ScreenUpdating = True[/FONT]
[FONT=Courier New]End Sub[/FONT]
 
Upvote 0
Thanks for your help pedie, the code ran well but I have picked up additional issues further on down the line of code - I will have to figure out and repost at some time.
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,522
Members
452,923
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top