+ Reply to Thread
Results 1 to 4 of 4

Protecting certain cells using Vb.net

  1. #1
    Registered User
    Join Date
    09-26-2016
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    24

    Protecting certain cells using Vb.net

    Hi!
    I have an excel sheet and a windows form in visual studio 2013. The form allows users to enter data in excel sheet. It will be shared by multiple users. What I want to do is that certain cells in excel (eg: column B to G) should be locked but data entry in these cells should be allowed through the form. Once a user opens excel, they can edit other columns but not these columns. I locked the cells and protected the worksheet in excel. Then, in my vb code I added these:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    xlWorkBook = xlApp.Workbooks.Open("C:\Users\test.xlsx")
    'xlApp.Visible = True
    xlWorkSheet = xlWorkBook.Sheets("Sheet1")
    xlWorkBook.Unprotect()

    'entering data in excel

    xlWorkBook.Save()
    xlWorkBook.Protect()
    End Sub
    I get the error saying that 'the cells you are trying to enter data is protected.' something like this. What should I do? Please help!!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Protecting certain cells using Vb.net

    Isn't it the worksheet you should be unprotecting rather than the workbook?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-26-2016
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    24

    Re: Protecting certain cells using Vb.net

    Quote Originally Posted by Norie View Post
    Isn't it the worksheet you should be unprotecting rather than the workbook?
    Yeah my bad. But I have changed it to xlWorksheet.Unprotect() and xlWorksheet.Protect(). Now it's allowing me to enter data but the problem is the protection is entirely removed. When i open excel the sheet is no longer protected. I want the sheet to be protected

  4. #4
    Registered User
    Join Date
    09-26-2016
    Location
    Dubai
    MS-Off Ver
    Excel 2013
    Posts
    24

    Re: Protecting certain cells using Vb.net

    My bad. I was saving before allowing to protect the file. Its done now!!
    Thank you @Norie

+ 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. protecting all cells expect input cells
    By abirup in forum Excel General
    Replies: 0
    Last Post: 10-07-2016, 12:47 AM
  2. [SOLVED] Help with protecting only certain cells based on a cells contents
    By christopherjward in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-18-2013, 06:43 PM
  3. Protecting Cells
    By hodkd in forum Excel General
    Replies: 5
    Last Post: 11-06-2009, 06:02 PM
  4. Protecting various cells?
    By quikgun15 in forum Excel General
    Replies: 2
    Last Post: 03-10-2009, 02:41 PM
  5. Protecting cells in a range, if cells contain information
    By vvebmike in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-03-2008, 06:41 PM
  6. protecting cells
    By drecollet in forum Excel General
    Replies: 1
    Last Post: 10-12-2007, 02:02 PM
  7. [SOLVED] Protecting Cells
    By teresa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2005, 02:06 PM

Tags for this Thread

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