+ Reply to Thread
Results 1 to 9 of 9

Validation code with Protected sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    06-01-2016
    Location
    Singapore
    MS-Off Ver
    13
    Posts
    55

    Validation code with Protected sheet

    Hello Guys,

    I am using this code to filter out one of the drop down list based on cell entry and I have to protect the sheet post which this code doesnt seem to work, please help.
    I tried unprotect and protect password in the code but that doesn't seems to work. Rest all other sheets with other codes are working fine.

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim str1 As String
      If Target.Address(0, 0) = "K10" Then
         Range("B5").ClearContents
         On Error Resume Next
            Range("B5").Validation.Delete
         On Error GoTo 0
         str1 = LCase(Target.Value)
         If InStr(1, str1, "@gmail.com") Then
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_gmail"
         ElseIf InStr(1, str1, "hotmail.com") Then
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_hotmail"
         Else
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_all"
         End If
      End If
    End Sub

    Please help
    Last edited by sandu1; 06-07-2016 at 02:45 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Validation code with Protected sheet

    Assuming that cell B5 and K10 is not locked, the code should be :

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim str1 As String
      If Target.Address(0, 0) = "K10" Then
         Range("B5").ClearContents
         On Error Resume Next
            Range("B5").Validation.Delete
         On Error GoTo 0
         str1 = LCase(Target.Value)
         Me.Unprotect Password:="abc"
         If InStr(1, str1, "@gmail.com") Then
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_gmail"
         ElseIf InStr(1, str1, "hotmail.com") Then
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_hotmail"
         Else
            Range("B5").Validation.Add Type:=xlValidateList, Formula1:="=vld_all"
         End If
         Me.Protect Password:="abc", UserinterfaceOnly:=True
      End If
    End Sub
    If above code is not worked, probably caused by other code that you have (as you stated here, you have 5 modules of code) :

    Quote Originally Posted by sandu1 View Post
    Hello,

    I am difficulties in running macro on protected sheets.

    I have 5 modules also codes on each sheet. Now, I have to protect the sheet for unauthorized user input.

    Its throwing an error while I try to execute the code now. Please suggest.

    Thanks
    Sandu
    and we can't help without seeing all your code that may cause this, you need to post your workbook so we can analyze the problem.
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    06-01-2016
    Location
    Singapore
    MS-Off Ver
    13
    Posts
    55

    Re: Validation code with Protected sheet

    Thank you very much. I have an other question.

    I have question with regards to below code. I have to use both the codes in the "workbook". Can I use both under same SUB?

    Private Sub Workbook_Open()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
    ws.Protect Password:="abc", UserinterfaceOnly:=True
    Next
    End Sub
    and other code is to block user adding new sheets
    Private Sub Workbook_NewSheet(ByVal Sh As Object)
    With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
             ActiveSheet.Delete
            MsgBox "Sorry, Adding new sheets is not Allowed!", 16
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
    End Sub

    Please suggest.

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Validation code with Protected sheet

    You don't need the second sub (Workbook_NewSheet), remove this sub.
    Just protect your workbook with a password, and user cannot add any sheet to it.

    http://www.excel-easy.com/examples/p...-workbook.html

  5. #5
    Registered User
    Join Date
    06-01-2016
    Location
    Singapore
    MS-Off Ver
    13
    Posts
    55

    Re: Validation code with Protected sheet

    I had tried that earlier, if I protect my workbook it will be disable sheets. But, it will lead to another issue, as I have other hidden worksheets and macros running on the that worksheet will throw an error "unable to set the visible property of the worksheet class"

    Sheets("Docs").Visible = True
    ActiveWorkbook.Sheets("Docs").Copy _
    After:=ActiveWorkbook.Sheets("Docs")

    Above code will be at module 2 and has been assigned to button that will copy and data from the hidden sheet and creates a new sheet. Please advise on how to overcome this.

  6. #6
    Registered User
    Join Date
    06-01-2016
    Location
    Singapore
    MS-Off Ver
    13
    Posts
    55

    Re: Validation code with Protected sheet

    Please suggest, if I have any other better option where I can disable user adding new sheets and can run the above mentioned code.

  7. #7
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Validation code with Protected sheet

    The method will be same with the sheet : unprotect --> make changes --> (re) protect.

    For example the code for the sheet above :
    ....
    ....
         Me.Unprotect Password:="abc"
         Make any changes
         Me.Protect Password:="abc", UserinterfaceOnly:=True
    ....
    ....
    For workbook, the code will be something like this (assuming the workbook is protected with password abc) :
    ....
    ....
    ThisWorkbook.Unprotect Password:="abc"
    
    'Code that make changes
    ActiveWorkbook.Sheets("Docs").Copy _
    After:=ActiveWorkbook.Sheets("Docs")
    
    ThisWorkbook.Protect Password:="abc", Structure:=True
    ....
    ....

  8. #8
    Registered User
    Join Date
    06-01-2016
    Location
    Singapore
    MS-Off Ver
    13
    Posts
    55

    Re: Validation code with Protected sheet

    Thank you very much, I will mark thread as resolved.

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Validation code with Protected sheet

    You are welcome, and thanks for marking the thread.


    Regards

+ 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. Unable to use data validation in protected sheet
    By vimal768 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-08-2014, 06:16 AM
  2. Combobox Data Validation on Protected Sheet
    By medz80 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-24-2014, 01:40 AM
  3. Protecting Validation on Protected Sheet
    By mealypudd1ng in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-16-2014, 08:13 AM
  4. Problem with Data Validation VBA code and protected sheets. Please help.
    By drew.j.harrison in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-20-2013, 01:33 AM
  5. Can't add Data Validation to Protected Sheet
    By foxguy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-20-2011, 07:52 PM
  6. Changing cell validation on a protected sheet
    By agentsmith83 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-18-2011, 12:33 PM
  7. [SOLVED] Apply Validation on a protected sheet
    By Frederick Chow in forum Excel General
    Replies: 0
    Last Post: 01-03-2006, 02:00 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