Using VBA how do I prevent the user from going to a different sheet in Excel?

I wrote a little code at the worksheet level to stop the user from moving to a different worksheet within the same workbook, assuming a specific cell is blank. The code works, but it technically moves to another sheet and moves quickly back. And the page flickers when I run it so it's not very elegant. There has to be an easier way to do this. Is there a way of stopping the user before the sheet changes?

Private Sub Worksheet_Deactivate()

    Dim destinationSheet
    destinationSheet = ActiveSheet.Name
    Application.EnableEvents = False
    Sheets("PAY TV-S").Activate
    Application.EnableEvents = True
    
    Cells.Find(What:="Service Name", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0).Select
    If ActiveCell.Value = "" Then
        Call MsgBox("You cannot leave this sheet with an incomplete period. Please add the period or clear the values.", vbCritical)
    Else
        Application.EnableEvents = False
        Sheets(destinationSheet).Activate
        Application.EnableEvents = True
    End If

End Sub

Open in new window

shampouyaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Just add Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wayne Taylor (webtubbs)Commented:
Can the sheet be hidden? Then use the Worksheet_Change() event to monitor the specified cell, and if not blank show the sheet.
0
Martin LissOlder than dirtCommented:
@shampouya:

I'm surprised that you graded my answer as a "B". Please see this Experts Exchange article on grading.
0
shampouyaAuthor Commented:
You can make it an A if you would like, but it did not solve my problem without creating another problem. Application.ScreenUpdating=False runs the risk of not showing the move back to the destination sheet when ActiveCell.Value is not = "" and the move is appropriate. No one else provided a valid solution, so I awarded a B, since his answer had some merits functionally speaking, but had an unintended consequence. Grade A answers, according to the EE criteria, should be thorough. If all the contingencies are not taken into consideration, then an answer is not thorough.
0
Martin LissOlder than dirtCommented:
When I or the others here give an answer that you have concerns about, just mention them to me/us. If you had I would have pointed out that you could add another Application.ScreenUpdating = True between your lines 12 and 13 above if you wanted to but it's not necessary since when you Activate a sheet and stay there, the activation shows up even if ScreenUpdating is still False. You can prove that by running this little sub from any sheet other than sheet2.
Sub sub1()
Application.ScreenUpdating = False
Sheets("Sheet2").Activate
End Sub

Open in new window


Also you may not be aware that Application.ScreenUpdating (unlike Application.EnableEvents) returns to True at the end of a sub automatically which can be seen if you run sub2 and then sub3.
Sub sub2()
Application.ScreenUpdating = False
Range("A1") = "hi"
End Sub

Sub sub3()
MsgBox Application.ScreenUpdating
End Sub

Open in new window


So with that explanation please let me know if you have any other concerns. If so we can work on them until you are happy, and if not and you still think my answer should be a B then I won't object.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.