Link to home
Start Free TrialLog in
Avatar of Shaft960
Shaft960

asked on

Prevent changing worksheets if data is not correct

How can I prevent someone from changing worksheets if a value is incorrect in the activeworksheet...

like something simple like if A1 <> 2 just for example

If you try to change sheets and A1 is not equal to 2 then you get an error message saying that you need to fix the problem before changing worksheets...

I tried the deactivate but the and the error message come up but it allow you to exit.. I tried cancel and ...

Thank you..
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Private FalseEvent As Boolean

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    If Not FalseEvent Then
        If Range("A2") <> 2 Then
            FalseEvent = True
            Application.Undo
        End If
    Else
        FalseEvent = False
    End If
End Sub
You can display a messagebox before the application.undo
Avatar of Shaft960
Shaft960

ASKER

Where does the

Private FalseEvent As Boolean  go

I copied and pasted and I got an error
I figured out where the falseevent as boolean goes... but the coding provided is giving me run time errors
What is the error and at what line.  Put a breakpoint and step through.

ps.  I assumed that you are in EXcel doing VBA (not automating Excel via VB) correct?
For now I just put this weak code in the deactivate of the 130_setup sheet

Private Sub Worksheet_Deactivate()
If Range("a53") <> "Master" Then
If Range("af154") > 0 Then

  '  I WANT IT TO RETURN TO THE sheet "130_Setup" right here and then give a message

MsgBox "Return to the Setup and select the appropriate 'USE POSITION' button", vbCritical


End If
End If
End Sub
All I want it to do is on the leaving of a sheet named 130_setup.. If af154 > 0 then stay on that sheet and give a message to press a button

Now.. they read the message and have to go to the sheet on there own
Put this before the msgbox

Sheets("130_setup").Activate
I get a run time error 1004

It highlights a code in the activate of the sheet that I go to ... the code is

Range("b5").Select
Before Range("b5").Select

add

Sheets("130_setup").Select
Wouldn't that me that every time that I went to that sheet it would go to the 130 file regardless if the data in the 130 is correct?
I am a bit confused because you don't name all the sheets, so please give each sheet a name so that i know which sheet we are moving to and from

The way I see so far is that you are in sheet 130_setup when you move to another sheet you want to check that some cells contain certain values, if they are not you want to remain in sheet 130_setup and display a message to the user, is that correct?  

The code above I suggested

Sheets("130_setup").Select

I was assuming that you are putting it in the activate event of sheet 130_setup is this so?

I am off to lunch and be back in an hour and half or so

Cheers
I have about 40 different sheet names.  In each of these sheets there is numerous lines of coding in the on worksheet_activate.. one line is to set the focus to cell b5.  Range("b5").Select

The program deals with monthly totals.  When you first setup a month you are in a sheet named "130_setup".

You need to enter values into the "130_setup" sheet to transfer to all of the other sheets.  

What I want it to do is if "130_setup" sheet does not have certain data .. I want it to remain there.  If the user clicks one of the 40 sheets it will bring up a message which tells them that they need to enter that data.





Ok I understand you but I can't visualise what your code is like.  can you post your code in the deactivate event of the 130_setup sheet and I will see if I can find why Range("b5").Select is failing
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
   
           
         If Sh.Name = "130_setup" Then Exit Sub
   
    Application.ScreenUpdating = False
         
        If Application.Sheets("130_setup").Range("p9").Value <> "" Then
            If Application.Sheets("130_setup").Range("i64").Value = "" Then
               ' Application.Sheets("130_setup").Activate
                MsgBox "You need to co"
            End If
            End If
       
        Application.ScreenUpdating = True

   
   

End Sub
Code here looks fine.  where is the
Range("b5").Select
that was causing the error?
The Range("B5").select is in EVERY sheet in the on activate except the 130_setup sheet.
ASKER CERTIFIED SOLUTION
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial