+ Reply to Thread
Results 1 to 15 of 15

Allowing only select users to print a worksheet from a workbook

  1. #1
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Allowing only select users to print a worksheet from a workbook

    I am challenged with finding a way to only allow certain users of a workbook to be able to print a particular worksheet. I have searched online and tried a few different codes, but nothing seems to be working. I am new to codes, so I need easy.

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Allowing only select users to print a worksheet from a workbook

    In "This Workbook" module, try this code.
    Please Login or Register  to view this content.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    Thank you Fotis! But I am getting a compile error: Variable not defined. It is highlighting "entrypw". Any ideas?

    Code error.jpg

  4. #4
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Allowing only select users to print a worksheet from a workbook

    Maybe even trigger a msgbox in the event of wrong password,
    Please Login or Register  to view this content.
    Just notice that enableevents must be "True" for it to work.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Allowing only select users to print a worksheet from a workbook

    For Fotis code, you get an error since you need to declare all variables using "Option Explicit". For that, add the line (in the beginning of the procedure):
    Please Login or Register  to view this content.
    This type of inputbox (member of VBA.Interaction) is treated as a string.

  6. #6
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    Thanks Berlan! This worked for the whole workbook, how do I get this to work for just one of the Worksheets and not all the Worksheets?

  7. #7
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Allowing only select users to print a worksheet from a workbook

    Thanks Berlan!


    yamalady

    Use the code to that worksheet module.

  8. #8
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Allowing only select users to print a worksheet from a workbook

    Fotis, to my knowledge it's not a worksheet specific event, but I may be wrong

    yamalady, since you can print multiple sheets at a time, I tried to cater fort that. Maybe:

    Please Login or Register  to view this content.
    See if this works.

  9. #9
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    I tried both yours Fotis, and yours Berlan, in the specific Worksheet module I am needing it in, but now neither are working at all. It just prints with no warnings or even allowing to enter a password. I know it's got to be something simple I am overlooking. I even tried changing the word "workbook" to "worksheet"...but that did not work either.

    I tried this:

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Application.EnableEvents = True
    Const sPW As String = "12345"
    If InputBox("Please enter the correct password!", "Password Required") <> sPW Then
    Cancel = True
    MsgBox "Wrong password, unable to print.", vbCritical
    End If
    End Sub

    Then I tried this:

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim entrypw as string
    Const pw = "12345"
    entrypw = InputBox("Please enter the correct password!", "Password Required")
    If entrypw <> pw Then
    Cancel = True
    Else
    Exit Sub
    End If

    End Sub

    Then I tried it without the "Dim":

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Const pw = "12345"
    entrypw = InputBox("Please enter the correct password!", "Password Required")
    If entrypw <> pw Then
    Cancel = True
    Else
    Exit Sub
    End If

    End Sub

  10. #10
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    Berlan, that did not work either.

  11. #11
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Allowing only select users to print a worksheet from a workbook

    Did you put my code in the Thisworkbook module and not in a Sheet module?

    Then change Sheet1 to the sheet you want to protect...

  12. #12
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    Berlan, yes, I moved your code to the Sheet module and yes, I did change the sheet name to the proper name.

  13. #13
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: Allowing only select users to print a worksheet from a workbook

    You may have misunderstood me, put it in the Thisworkbook module, and see how it works for you.

    The code won't be triggered in a Sheet module.

  14. #14
    Registered User
    Join Date
    08-01-2012
    Location
    Ohio
    MS-Off Ver
    Excel 2016
    Posts
    60

    Re: Allowing only select users to print a worksheet from a workbook

    Sorry, I did misunderstand. Ok, I moved it back to ThisWorkbook. It did work, but the interesting part is that it now prints doubles of the other sheets that are ok to print.

  15. #15
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Allowing only select users to print a worksheet from a workbook

    Quote Originally Posted by berlan View Post
    Fotis, to my knowledge it's not a worksheet specific event, but I may be wrong
    Very kind of you!!



    Ok. so let's do something that i know better than codes!

    yamalady

    Your post#9, does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 11-21-2013, 02:51 PM
  2. [SOLVED] How do you protect multiple sheets while only allowing users to select unlocked cells?
    By mrssteelerhall in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2013, 02:05 PM
  3. Allowing users to select the range for a macro to be run on
    By H&MITGuy in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-13-2009, 12:03 PM
  4. Allowing other users to use Workbook created with Macros without enabling Macro
    By jlcford in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-07-2008, 03:10 PM
  5. Replies: 1
    Last Post: 11-30-2005, 08:30 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1