+ Reply to Thread
Results 1 to 9 of 9

VBA - check if user name matches requirements, if not, dont open

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    876

    VBA - check if user name matches requirements, if not, dont open

    afternoon all!

    I think i've posted something similar here before but i can't find it through the 'My Posts' option, so please forgive me for asking.

    I have the following:
    Private Sub Workbook_Open()
    Application.ScreenUpdating = False
        Range("A2").Select
        ActiveCell.FormulaR1C1 = "=NOW()"
        Selection.NumberFormat = "[$-F400]h:mm:ss AM/PM"
        Range("B2").Select
        ActiveCell.FormulaR1C1 = "=NOW()"
        Selection.NumberFormat = "m/d/yyyy"
        Range("C2").Value = Environ("Username")
    Application.ScreenUpdating = True
    End Sub
    How can i (if i even can) get my VBA to check the usernam that is trying to open the spreadsheet, and if it is one of 3 (for example) then the spreadsheet will open.
    If the username does not match one of the threee names, i dont want the sheet to open.

    Don't mind at present if the names are defined either in the code itself or the code reads from a list on one of the tabs.

    I know i can put a password in to prevent the sheet being opened, but at present i dont want to follow that route, so thought i would ask if the above is possible.

    Many thanks in advance.

    Rich

  2. #2
    Forum Expert
    Join Date
    10-09-2012
    Location
    Dallas, Texas
    MS-Off Ver
    MO 2010 & 2013
    Posts
    3,049

    Re: VBA - check if user name matches requirements, if not, dont open

    Why not lock it down with a password and avoid this?
    Please ensure you mark your thread as Solved once it is. Click here to see how.
    If a post helps, please don't forget to add to our reputation by clicking the star icon in the bottom left-hand corner of a post.

  3. #3
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: VBA - check if user name matches requirements, if not, dont open

    I usually use the following code and then do an if statement in the on open event.

    Public Function GetUserName() As String
        GetUserName = Environ("USERNAME")
    End Function
    Some people volunteer in soup kitchens or hospitals. I choose to make the world better by trying to help you with Excel. We're all learning.

    <---Click * Add Reputation for all helpful comments. It's like giving a smile.
    Forum Rules: How to mark your post [Solved] and have a happier, Excel enriched life.

  4. #4
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    876

    Re: VBA - check if user name matches requirements, if not, dont open

    MikeTRON - Agreed, it is a way to do it, but at present i'm trying to delete all passwords from opening sheets.
    Nigelbloomy - Thanks, i'll give that a go, could you provide a little more detail on how the code will work?

  5. #5
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: VBA - check if user name matches requirements, if not, dont open

    You could use it like this

    Public Function GetUserName() As String
        GetUserName = Environ("USERNAME")
    End Function
    
    Sub testNames()
    Select Case GetUserName
        Case "Nigel": Exit Sub
        Case "Bill": Exit Sub
        Case Else: ThisWorkbook.Close
    End Select
    End Sub

  6. #6
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    876

    Re: VBA - check if user name matches requirements, if not, dont open

    potentially a daft question incoming :D

    I dont understand how i can get the code to work upon opening the sheet.

    If for example the usernames i want to be able to open the spreadsheet are:
    Dduck
    Wcoyote
    Bbunny

    then how do i incorporate that into the
    Private Sub Workbook_Open()
    
    End Sub
    Either with those usernames being hard coded into the macro or with the usernams being entered into a sheet within the workbook?

  7. #7
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: VBA - check if user name matches requirements, if not, dont open

    Just a warning. If someone holds shift while opening the file, the code will not run when the file opens. That can be helpful for you to know while troubleshooting and making sure it works for the correct people. Just know that someone can get around the code if they do a little Google searching. I would recommend the password option if you want a little more security. That can easily be broken too, but it takes a little more than just pressing shift while opening the file.

    http://www.excelforum.com/excel-prog...ng-macros.html
    Last edited by nigelbloomy; 06-16-2015 at 02:40 PM.

  8. #8
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: VBA - check if user name matches requirements, if not, dont open

    This would work. I just added in to close without saving changes. Otherwise the user could click cancel and still use the workbook.

    Private Sub Workbook_Open()
    Dim GetUserName As String
    
    GetUserName = Environ("USERNAME")
    Select Case GetUserName
        Case "Nigel": Exit Sub
        Case "Bill": Exit Sub
        Case Else: ThisWorkbook.Close SaveChanges:=False
    End Select
    End Sub
    Last edited by nigelbloomy; 06-16-2015 at 02:17 PM. Reason: added in close without saving changes.

  9. #9
    Forum Contributor
    Join Date
    11-02-2011
    Location
    Rugby, England
    MS-Off Ver
    Office 365
    Posts
    876

    Re: VBA - check if user name matches requirements, if not, dont open

    Great stuff, thanks alot.
    Not overly concerned about people googling a way to get round it, when i get to that stage i'll have to work something else out :D
    Thanks an awful lot for the code and also the link, good to know!

    Rich

+ 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. Check and list names of all workbooks that already open by another user
    By ilsley_excel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2015, 06:23 AM
  2. VBA to check if user has pdf file open
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 10-11-2014, 09:36 AM
  3. if worksheet already open then dont open again
    By vipulhumein in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2013, 12:17 PM
  4. [SOLVED] If value in Column A matches K, enter value in Column O into H, dont do if F is empty
    By rkobeyer in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-18-2013, 02:05 AM
  5. VBA Spell Check - Dont ask to check rest of Doc?
    By Chris424 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-03-2008, 07:42 AM

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