Consulting

Results 1 to 11 of 11

Thread: Unable to use Validation

  1. #1

    Unable to use Validation

    Hi all,
    I am trying to apply validation to a range of cells as follows:
    Private Sub Worksheet_Activate()   
        With Range("A1:D4").Validation
               .IgnoreBlank = True
               .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertWarning, Operator:=xlBetween, Formula1:="0", Formula2:="50000"
               .InputTitle = "Numeric"
               .ErrorTitle = "Numeric"
               .InputMessage = "Enter a number between 0 and 50,000"
               .ErrorMessage = "You must enter a number between 0 and 50,000"
       End With
        
    End Sub
    When I executed the code, I got the following error message:


    Run-Time error '1004'
    Application-defined or object-defined error




    Thereafter, I move the code the Open event of the Workbook as follows:
    Private Sub Workbook_Open()   
        With Range("A1:D4").Validation
               .IgnoreBlank = True
               .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertWarning, Operator:=xlBetween, Formula1:="0", Formula2:="50000"
               .InputTitle = "Numeric"
               .ErrorTitle = "Numeric"
               .InputMessage = "Enter a number between 0 and 50,000"
               .ErrorMessage = "You must enter a number between 0 and 50,000"
       End With
        
    End Sub

    I got the same error message. So, I wish to know in what context I am supposed to use validation.


    Thanks.

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    With Range("A1:D4").Validation.add

    or if the validation exists:

    With Range("A1:D4").Validation.Modify

  3. #3
    what do you mean by "if Validation exists"?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    Alt-F11 / F1 / validation.modify / Enter

  5. #5
    Apology for coming back late. Here's where the help and recording the validation process lead me:
    Private Workbook_Open() 
    Range("A1:D4").Select
    With Selection.Validation 
            .IgnoreBlank = True 
            .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertWarning, Operator:=xlBetween, Formula1:="0", Formula2:="50000" 
            .InputTitle = "Numeric" 
            .ErrorTitle = "Numeric" 
            .InputMessage = "Enter a number between 0 and 50,000" 
            .ErrorMessage = "You must enter a number between 0 and 50,000" 
    End Sub

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,050
    Location
    How is that different from that which you initially offered? Did you try the concept offered by snb?
    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

  7. #7
    I followed the Alt-F11 / F1 / validation.modify / Enter and found helpful remarks on Validation object. As far as I am concerned, the invocation of the Select method made the difference. I may be wrong, but my challenge with applying validation on a range has been resolved just by invoking the Select method.

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,050
    Location
    invoking the select method simply slows the process down. Ask any experienced coder about using select, and they will tell you to ditch it. Anywhere you see .select With selection smacks of macro recorder code and that's a no no.
     With Range("A1:D4").validation
    Now, earlier snb offered you two options, one was .add (if validation did not already exist) and two, .modify ( if it already existed)mMy question you is, is the validation new or pre- existing? If its new then use validation.add, if not then use validation.modify
    Last edited by Aussiebear; 02-17-2014 at 03:39 AM. Reason: Typo
    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

  9. #9
    Once more, apology for responding late. Could you give me an example code, please. The following came up with error message: "Compiler error, argument not optional"
    With Range("A1:D4").Validation.Add         
            .IgnoreBlank = True 
            .Add Type:=xlValidateDecimal, AlertStyle:=xlValidAlertWarning, Operator:=xlBetween, Formula1:="0", Formula2:="50000" 
            .InputTitle = "Numeric" 
            .ErrorTitle = "Numeric" 
            .InputMessage = "Enter a number between 0 and 50,000" 
            .ErrorMessage = "You must enter a number between 0 and 50,000" 
    End With

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,638
    This might be of interest before continuing:

    http://it-ebooks.info/book/1903/

  11. #11
    Thanks for the reference.

Tags for this Thread

Posting Permissions

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