Data Validation in UserForm base on worksheet cell value

mmaeng13

New Member
Joined
Aug 29, 2017
Messages
25
I am attempting to add data validation to a UserForm with textboxes. I need a way to have the userform alert the person entering data that an entry is out of range. This is for running hours of an engine and is entered daily, so 24 or less hours. I would also like to add in an alert for negative hours. This Userform has a number of textboxes so I would prefer just highlighting the specific textbox instead of a pop up message.

My current code, validates then enables Commandbutton1 to enter the data from the userform. Range("B17").Value is the worksheet cell with the previous days entry.

Code:
Private Sub Validate_Click()

ValidateDataDisplayLabel


End Sub


Sub ValidateDataDisplayLabel()


    If PMEHours.Value - Range("B17").Value > 24 Then
        PMEHours.BackColor = &HFF&
    End If
    
CommandButton1.Enable


End Sub
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about
Code:
Private Sub Validate_Click()
   Dim AllOk As Boolean
   
   AllOk = True
   If PMEHours.Value - Range("B17").Value > 24 Or PMEHours.Value < 0 Then
      PMEHours.BackColor = &HFF&
      AllOk = False
   Else
      PMEHours.BackColor = vbWhite
      AllOk = True
   End If
   If AllOk Then CommandButton1.Enabled = True
End Sub
 
Upvote 0
How about
Code:
Private Sub Validate_Click()
   Dim AllOk As Boolean
   
   AllOk = True
   If PMEHours.Value - Range("B17").Value > 24 Or PMEHours.Value < 0 Then
      PMEHours.BackColor = &HFF&
      AllOk = False
   Else
      PMEHours.BackColor = vbWhite
      AllOk = True
   End If
   If AllOk Then CommandButton1.Enabled = True
End Sub


Is there anyway to repeat this within this specific Sub for multiple engines and only enable the command button if all instances are True?
 
Upvote 0
Yes, simply repeat the part in blue like this
Code:
Private Sub Validate_Click()
   Dim AllOk As Boolean

   AllOk = True
   
  [COLOR=#0000ff] If PMEHours.Value - Range("B17").Value > 24 Or PMEHours.Value < 0 Then
      PMEHours.BackColor = &HFF&
      AllOk = False
   Else
      PMEHours.BackColor = vbWhite
      AllOk = True
   End If[/COLOR]
   If [COLOR=#ff0000]PMEHours[/COLOR].Value - Range("B17").Value > 24 Or [COLOR=#ff0000]PMEHours[/COLOR].Value < 0 Then
      [COLOR=#ff0000]PMEHours[/COLOR].BackColor = &HFF&
      AllOk = False
   Else
      [COLOR=#ff0000]PMEHours[/COLOR].BackColor = vbWhite
      AllOk = True
   End If

   If AllOk Then CommandButton1.Enabled = True
End Sub
Changing the values in red to suit
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
Scrub what I said in post#4, I've given you some duff info.
Use this instead
Code:
Private Sub Validate_Click()
   Dim AllOk As Long

   If tb1.Value - Range("B17").Value > 24 Or tb1.Value < 0 Then
      tb1.BackColor = &HFF&
      AllOk = 0
   Else
      tb1.BackColor = vbWhite
      AllOk = AllOk + 1
   End If
   
   If tb2.Value - Range("B17").Value > 24 Or tb2.Value < 0 Then
      tb2.BackColor = &HFF&
      AllOk = 0
   Else
      tb2.BackColor = vbWhite
      AllOk = AllOk + 1
   End If

   If AllOk = [COLOR=#ff0000]2 [/COLOR]Then CommandButton1.Enabled = True
End Sub
Change the value of 2 in red to then number of txtboxes you're checking
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top