Consulting

Results 1 to 6 of 6

Thread: help with validation

  1. #1
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location

    help with validation

    Hi all,

    am having a problem with a routine that is supposed to set some validation.
    can anyone spot my error?
    thanks
    Werafa

    Sub RoleValidation()
    'set/reset validation in Data Entry column D
    Dim myRange As Range
    Dim myString As String
    
    
        Set myRange = Worksheets("PM List").Range("H3:H5")
        myString = ValidationRange(myRange)
        Set myRange = Worksheets("Data Entry").Range("D:D")
    
    
        ActiveSheet.Protect UserInterfaceOnly:=True
        With myRange.Validation
            .Delete
            .Add Type:=xlValidateList, Formula1:=myString 'fails here
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = False
            .ShowError = False
        End With
        
        Set myRange = Worksheets("Data Entry").Range("D1:D10")
        With myRange.Validation
            .Delete
        End With
    End Sub
    Function ValidationRange(myRange As Range) As String
    ' writes the formula1 string for validation routines
    'Dim myCell As Range
    Dim myString As String
    Dim wsName As String
    Dim rAddress As String
    
    
        wsName = myRange.Parent.Name
        rAddress = myRange.Address
        myString = "= '" & wsName & "'!" & rAddress
        
    ValidationRange = myString
    End Function
    Remember: it is the second mouse that gets the cheese.....

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    >ActiveSheet.Protect UserInterfaceOnly:=True


    This line should be moved to the last

  3. #3
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    the worksheet is already protected, and must be unprotected for the vba to run cleanly.
    this removes (or is supposed to remove) sheet protection for vba

    but you are correct - it is the sheet protection that is causing the problem despite my attempt to deal with it.
    thanks for the clue
    Last edited by werafa; 03-27-2017 at 02:56 PM.
    Remember: it is the second mouse that gets the cheese.....

  4. #4
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    this is getting old quickly
    I had it working, and now it has stopped working again ....

    Mana's comment was correct - the worksheet protection was still blocking the validation.
    and it still is (as far as I can tell), despite the sheet.unprotect line.
    It continues to fail on the validation.add line

    can anyone save me from further hair loss?

    Sub RoleValidation()
         'set/reset validation in Data Entry column D
         ' used to manage transition from early model (V3x) workbooks in which column D validation did not exist
        Dim myRange As Range
        Dim myString As String
        Dim mySheet As Worksheet
        
        ' get formula1 string
        Set mySheet = ThisWorkbook.Worksheets("PM List")
        Set myRange = mySheet.Range("H3:H5")
        myString = ValidationRange(myRange)
        
        'reset validation
        Set myRange = Worksheets("Data Entry").Range("D:D")
        mySheet.Unprotect
        
        With myRange.Validation
            .Delete
            .Add Type:=xlValidateList, Formula1:=myString
            .IgnoreBlank = True
            .InCellDropdown = True
            .ShowInput = False
            .ShowError = False
        End With
         
        Set myRange = Worksheets("Data Entry").Range("D1:D10")
        With myRange.Validation
            .Delete
        End With
        mySheet.Protect
    End Sub
    Remember: it is the second mouse that gets the cheese.....

  5. #5
    VBAX Mentor
    Joined
    Aug 2012
    Posts
    367
    Location
    doh,

    was unprotecting the wrong sheet
    (sheepish grin)

    must go to bed more often
    Remember: it is the second mouse that gets the cheese.....

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    You wouldnt be the first to have done so. Glad you got it fixed
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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