+ Reply to Thread
Results 1 to 11 of 11

Data from Input Box overrides cell input validation

Hybrid View

  1. #1
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Smile Data from Input Box overrides cell input validation

    I have two cells on a worksheet whith a custom validation for cell "M13" permitting Istext only and for cell "H17" permitting Isnumber only. To make it easier for the user, I have created an Input Box, but regardless of what is entered in this box the cell/s accept/s and ignore/s any validation.

    I found some code in the 'On-Screen Help' to Add or Modify xlValidationCustom etc., but it does not work, no matter how hard I try to tweak and twist.

    Could someone please help me with the correct code to maintain validations for the respective cells via the Input Box, to include an error message "You must enter a number" or "Text only is allowed, and no numbers"?

    If it is of any help, I am still working with Office2000.

    Thanks Guys!
    Last edited by Winon; 05-20-2007 at 12:04 AM. Reason: Wrong cell references in original post might create confusion

  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,259
    Hello Tjaart,

    Can you post the InputBox code portion?

    Thanks,
    Leith

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Wink Data from Input Box overrides cell input validation

    Hello Leith,

    And once again it is Leith to the rescue!

    Thank you.

    I have taken out the xlValidateCustom etc. since it does not work, and I am sending you what I have got in terms of the InputBox.

    If Sheet1.Range("M13") = "" Then
    Application.DisplayAlerts = True
    Range("M13").Validation.IgnoreBlank = True
    Dim Message, Title, Default, MyValue
    Message = "                                     Please enter your name"
    Title = "                                              Welcome"
    Default = ""
    MyValue = InputBox(Message, Title, Default)
    Range("M13").Value = MyValue
    End If
    If Range("M13") <> "" And Range("H17") = 0 Then
    Message = "                                     Please enter any number"
    Title = "                                              Next step"
    Default = "0"
    MyValue = InputBox(Message, Title, Default, 1850, 1875)
    Range("H17").Value = MyValue
    End If
    Regards

    Tjaart

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

    Data Validation only occurs when Excel is in edit mode. The user is either typing in the Formula Bar or changing the cells contents directly. Changing a cell's contents using VBA bypasses the Validation, as you discovered. The trick is how do we trigger the Validation when using VBA code?

    The obvious answer is type something into the formula. With VBA, we can do just that. Excel has a hot Key "F2" that places the cursor in the Formula Bar. Next we need to transfer the string from the InputBox to the Formula Bar along with the Enter key. we can accomplish all this with the VBA SendKeys statement. Copy the macro and paste it into a Standard VBA Module. It is highlighted in Blue in your code.

    Macro code:
    Sub EnterData(Input_Cell As Range, Input_Data As String)
    
      With Input_Cell
        .Value = ""
        .Select
      End With
    
       'Place the Cursor in the Formula Bar
        SendKeys "{F2}"
    
       'Put the data in the Formula and Enter it
        SendKeys Input_Data & "{Enter}"
      
    End Sub
    Macro added to your code:
      Dim Message, Title, Default, MyValue    
        If Sheet1.Range("M13") = "" Then
           Application.DisplayAlerts = True
           Range("M13").Validation.IgnoreBlank = True
             Message = "Please enter your name"
             Title = "Welcome"
             Default = ""
               MyValue = InputBox(Message, Title, Default)
             EnterData Sheet1.Range("M13"), MyValue
        End If
    
        If Range("M13") <> "" And Range("H17") = 0 Then
           Message = "Please enter any number"
           Title = "Next step"
           Default = "0"
             MyValue = InputBox(Message, Title, Default, 1850, 1875)
           EnterData Sheet1.Range("H17").Value, MyValue
        End If
    Sincerely,
    Leith Ross

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Thumbs up Data from Input Box overrides cell input validation

    Hello Leith,

    Thank you for your help. You make it look so simple and yet, I would not have been able to figure it out for myself!

    I am sure that other forum viewers must have learnt from your solution as well.

    Keep up the good work! I may call on you again.

    Kind regards,

    Tjaart

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    There are two kinds of input boxes. The one you use (the function)
    MyValue = Application.InputBox(Message, Title, Default)
    will not allow data validation

    The version Application.InputBox (method) allows you to specify which kind of data it will accept.
    MyValue = Application.InputBox(Message, Title, Default, Type:=1)
    will only accept numerical entries
    MyValue = Application.InputBox(Message, Title, Default, Type:=2)
    will only accept text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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