Results 1 to 6 of 6
  1. #1
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Trenton, Ontario
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Prevent Changing Sheets (Excel 2k)

    Good Morning,
    I have created a spreadsheet that on Sheet1 a user enters some data and then goes to Sheet2 and the customized document they need is there. I have Sheet1 protected with some conditional formatting to prevent them from entering invalid data, but is there a macro that will prevent them from going to sheet2 unless certain conditions are met, say A1 must equal "Yes" before they can change Sheets. When they switch back from Sheet2 to Sheet1 there is a macro that clears the input data on Sheet1 so they start with a clean page.
    Thanks in advance for any information you fine folks can give!
    David

  2. #2
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Prevent Changing Sheets (Excel 2k)

    Hi David,
    Since you can disable macros which might bypass your selection validation, I would suggest having Sheet2 hidden to start with. You can then have a button on Sheet1 which runs a macro to check what data has been entered on sheet1 and, if it's OK, unhide and activate sheet2.
    Hope that helps.
    Regards,
    Rory

    Microsoft MVP - Excel

  3. #3
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Trenton, Ontario
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Prevent Changing Sheets (Excel 2k)

    Hi Rory,
    Thank you for your quick reply. I have no experience with macros, the macro I use in that program is one that one of the nice people in this lounge provided for me. If it's not too much trouble could I impose upon you to provide me an example. From the way you described it, it sounds like it will work perfectly.
    Thanks again for all of your help.
    David

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Prevent Changing Sheets (Excel 2k)

    Without knowing the exact details I can't be too specific, but it would be something fairly simple like:
    If [A1] = "Yes" then
    sheets("Sheet2").Visible = true
    else
    msgbox "A1 must equal Yes for you to continue"
    end if

    once you've hidden sheet2. You can add more conditions as necessary.
    To add the command button to the sheet, choose View-Toolbars-Control Toolbox then select the command button icon and draw it wherever you want. Next select the View Code button on the toolbox and paste your code into the module that appears.
    Hope that helps though if you need more info please repost.
    Regards,
    Rory

    Microsoft MVP - Excel

  5. #5
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Prevent Changing Sheets (Excel 2k)

    You can use the Sheet Activate and Deactivate event routines to do what you want. Code like the following in the Deactivate event will prevent changing to another sheet if A1 is not "Yes".

    <pre>Private Sub Worksheet_Deactivate()
    If Worksheets("Sheet1").Range("A1") <> "Yes" Then
    Application.EnableEvents = False
    Worksheets("Sheet1").Activate
    Application.EnableEvents = True
    MsgBox "All data not entered.", vbExclamation + vbOKOnly
    End If
    End Sub
    </pre>


    You can put code in the Sheet activate event to clear out old data.
    Legare Coleman

  6. #6
    2 Star Lounger
    Join Date
    Jan 2002
    Location
    Trenton, Ontario
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Prevent Changing Sheets (Excel 2k)

    Thank you very much for taking the time to reply to my post, it worked like a charm!!
    David

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •