• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 106
  • Last Modified:

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

0
shampouya
Asked:
shampouya
  • 3
1 Solution
 
Martin LissOlder than dirtCommented:
Just add Application.ScreenUpdating = False at the start and Application.ScreenUpdating = True at the end.
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now