Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 64
Default Cell validation

Hi all,

can somebody please help me?

i have a userform that asks uses to type in the results to
the questions, but for each answer there is 4 textboxes,
and in each one there can only be a number between 1 and 4
in them, also the number cannot be repeated in the
following textboxes.

if this is possible please could i also have the piece of
code that will select the textbox information if it is not
valid and then allow the user to retype the information.

thank you,

Robert Couchman
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,236
Default Cell validation

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox3_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Function ValidateTextBoxes() As Boolean
ValidateTextBoxes = Not ( _
(Len(TextBox1.Text) 0 And _
(TextBox1.Text = TextBox2.Text Or _
TextBox1.Text = TextBox3.Text Or _
TextBox1.Text = TextBox4.Text)) Or _
(Len(TextBox2.Text) 0 And _
(TextBox2.Text = TextBox3.Text Or _
TextBox2.Text = TextBox4.Text)) Or _
(Len(TextBox3.Text) 0 And _
TextBox3.Text = TextBox4.Text))
End Function


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Robert Couchman" wrote in message
...
Hi all,

can somebody please help me?

i have a userform that asks uses to type in the results to
the questions, but for each answer there is 4 textboxes,
and in each one there can only be a number between 1 and 4
in them, also the number cannot be repeated in the
following textboxes.

if this is possible please could i also have the piece of
code that will select the textbox information if it is not
valid and then allow the user to retype the information.

thank you,

Robert Couchman



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Cell validation

Robert,

Here's an improved (G) version of Rob's code. It only allows a 1 to 4 to be
keyed in, and if Rob's code traps an error, it highlights the field to
facilitate easy change (which I think is what you were originally asking
for)
Option Explicit

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 49 Or KeyAscii 52 Or Len(Me.TextBox1.Text) = 1 Then
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
End If
End Sub


Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not ValidateTextBoxes Then
With Me.TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
End If
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 49 Or KeyAscii 52 Or Len(Me.TextBox2.Text) = 1 Then
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
End If
End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not ValidateTextBoxes Then
With Me.TextBox2
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
End If
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 49 Or KeyAscii 52 Or Len(Me.TextBox3.Text) = 1 Then
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
End If
End Sub

Private Sub TextBox3_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not ValidateTextBoxes Then
With Me.TextBox3
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
End If
End Sub

Private Sub TextBox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 49 Or KeyAscii 52 Or Len(Me.TextBox4.Text) = 1 Then
Application.EnableEvents = False
KeyAscii = 0
Application.EnableEvents = True
End If
End Sub

Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If Not ValidateTextBoxes Then
With Me.TextBox4
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
Cancel = True
End If
End Sub

Private Function ValidateTextBoxes() As Boolean
ValidateTextBoxes = Not ( _
(Len(TextBox1.Text) 0 And _
(TextBox1.Text = TextBox2.Text Or _
TextBox1.Text = TextBox3.Text Or _
TextBox1.Text = TextBox4.Text)) Or _
(Len(TextBox2.Text) 0 And _
(TextBox2.Text = TextBox3.Text Or _
TextBox2.Text = TextBox4.Text)) Or _
(Len(TextBox3.Text) 0 And _
TextBox3.Text = TextBox4.Text))
End Function


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Rob van Gelder" wrote in message
...
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox3_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Sub TextBox4_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = Not ValidateTextBoxes
End Sub

Private Function ValidateTextBoxes() As Boolean
ValidateTextBoxes = Not ( _
(Len(TextBox1.Text) 0 And _
(TextBox1.Text = TextBox2.Text Or _
TextBox1.Text = TextBox3.Text Or _
TextBox1.Text = TextBox4.Text)) Or _
(Len(TextBox2.Text) 0 And _
(TextBox2.Text = TextBox3.Text Or _
TextBox2.Text = TextBox4.Text)) Or _
(Len(TextBox3.Text) 0 And _
TextBox3.Text = TextBox4.Text))
End Function


--
Rob van Gelder - http://www.vangelder.co.nz/excel


"Robert Couchman" wrote in message
...
Hi all,

can somebody please help me?

i have a userform that asks uses to type in the results to
the questions, but for each answer there is 4 textboxes,
and in each one there can only be a number between 1 and 4
in them, also the number cannot be repeated in the
following textboxes.

if this is possible please could i also have the piece of
code that will select the textbox information if it is not
valid and then allow the user to retype the information.

thank you,

Robert Couchman





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation Data using Validation Table cell range..... Dermot Excel Discussion (Misc queries) 16 January 5th 10 09:35 PM
how blank data validation cell after changing dependent cell? Ian Elliott Excel Discussion (Misc queries) 5 August 16th 09 02:42 AM
Force entry into cell, based on validation selection in adjacent cell Richhall[_2_] Excel Worksheet Functions 3 June 18th 09 10:28 AM
Selecting a cell entry based on cell validation selection Brutalius Excel Worksheet Functions 2 December 17th 08 03:44 AM
Dynamically adding an in-cell drop-down list (i.e. Validation Object) to a cell? debartsa Excel Programming 5 March 5th 04 08:45 AM


All times are GMT +1. The time now is 11:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"