+ Reply to Thread
Results 1 to 13 of 13

VB Code to restrict opening excel file for certain computer name only

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    VB Code to restrict opening excel file for certain computer name only

    Hello:

    Please refer to attached file.
    I am trying to put the code in the workbook so that the excel file can be open only if the computer name is as assigned.
    However, it is malfunctioning although i have correct Computer name (User as below) in the code.
    Would appreciate if you can help me with this to solve

    1st set of code is in "ThisWorkbook"
    Please Login or Register  to view this content.
    Below code in Module

    Please Login or Register  to view this content.
    Let me know if you have any questions.
    Thanks.
    Riz
    Attached Files Attached Files

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VB Code to restrict opening excel file for certain computer name only

    Hello Riz,

    The macro code below has been added to the attached workbook. I tested it and it worked for my computer. Let me know if you have any issues.

    Workbook Open Event Code
    Please Login or Register  to view this content.
    Module1 Macro Code
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Re: VB Code to restrict opening excel file for certain computer name only

    Hi Leith:

    Yup, seems to work.

    Thanks a lot

    Riz

  4. #4
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    853

    Re: VB Code to restrict opening excel file for certain computer name only

    What is the user has macro's disabled? Wouldn't they still be able open the file?

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VB Code to restrict opening excel file for certain computer name only

    Hello Riz,

    You're welcome. You can always password protect the VBA Project to prevent others from saving and changing the code.

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: VB Code to restrict opening excel file for certain computer name only

    Thanks Mr. Leith for the code
    I tested it and put the computer name in the variable "Users" but I still get the Access Denied message
    Any idea about my try
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VB Code to restrict opening excel file for certain computer name only

    Hello Yasser,

    Change the XQuit code to this. Add you PC name to the Users.
    Please Login or Register  to view this content.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: VB Code to restrict opening excel file for certain computer name only

    Please Login or Register  to view this content.
    If there's only 1 name in users Instr will always give 1 as result. Therefore >1 will always fail. Has to be >0.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VB Code to restrict opening excel file for certain computer name only

    Hello bakerman2,

    The Users variable requires each computer name to end in a semi-colon. In the revised code, if the a final semi-colon is missing it it will added. Therefore you can a valid return of 1 for InStr that is incorrect. It must be set to greater than 1.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: VB Code to restrict opening excel file for certain computer name only

    Sorry but no can do.
    Replace bakerman2 with computername and test following and you see what I mean.
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: VB Code to restrict opening excel file for certain computer name only

    Thank you very much Mr. Leith
    The code in post #7 worked fine for me

    As for bakerman's hint worked fine for me also when I replaced 1 with 0
    Regards
    Last edited by YasserKhalil; 06-30-2016 at 04:08 AM.

  12. #12
    Registered User
    Join Date
    04-07-2021
    Location
    Pakistan
    MS-Off Ver
    office 2016
    Posts
    1

    Re: VB Code to restrict opening excel file for certain computer name only

    If i want to add more than one computer name then what i need to do???

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,265

    Re: VB Code to restrict opening excel file for certain computer name only

    As it says in a few of the posts, and in the code, you need to separate them with a semicolon - eg:

    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

+ 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: 03-13-2015, 01:36 PM
  2. vba code for opening excel file and insert a new column in exisiting excel file
    By saratu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2014, 02:55 PM
  3. Replies: 1
    Last Post: 06-30-2012, 02:03 PM
  4. Macro to not allow opening of file only on certain computer if it is already opened
    By Repent34 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-04-2011, 05:41 PM
  5. [SOLVED] restrict a user from opening a dbf file from excel?
    By WyldX in forum Excel General
    Replies: 0
    Last Post: 01-18-2006, 09:45 AM
  6. [SOLVED] Opening an excel file on another computer changes the setup on fil
    By Viviana in forum Excel General
    Replies: 3
    Last Post: 11-29-2005, 04:55 PM
  7. Replies: 1
    Last Post: 01-06-2005, 10:06 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