Validation in UserForm

kimberly090

Board Regular
Joined
May 22, 2014
Messages
99
Hi,

I have 2 sheet in my workbook.

The first worksheet "Inventory" contain the incoming stock.

The second worksheet "Track Out" will contain the outgoing stock.

I have create a userform in my Track Out sheet like this:

1.PNG


and my Track Out list will look like this:
3.PNG


Here will be my inventory list:
2.PNG


I would like to find out how can I validate the data in Track Out page. For example when I type in the PT# in my userform, if the PT# is not exist inside Inventory list, it will not allow user to click on the Track Out button in the userform.

Here will be the code for my userform:

Code:
Private Sub TrackOut_Click()Dim cDelete As VbMsgBoxResult
                   With Me
                            If Len(.TextBox1.Value) * Len(.PTD.Value) * Len(.RackD.Value) * _
                Len(.OperatorD.Value) = 0 Then
            MsgBox "Please Complete All Fields Before Submit"
        Else
        
        cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Track Out")
        If cDelete = vbYes Then
           eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            Cells(eRow, 1).Value = TextBox1.Text
            Cells(eRow, 2).Value = PTD.Text
            Cells(eRow, 3).Value = RackD.Text
            Cells(eRow, 4).Value = OperatorD.Text
            
            Else
            
            If cDelete = vbNo Then
            Unload Me
            End If
            End If
        End If
    End With
   
End Sub

Thank you.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
After your If cDelete = vbYes Then line, I'd add another IF statement nested within that one. Try:

Code:
If Sheets("Inventory").Columns(2).Find(What:=PTD.Text) Is Nothing Then
    MsgBox "No stock for this PT#"
    exit sub
End If

All together now:
Code:
Private Sub TrackOut_Click()Dim cDelete As VbMsgBoxResult
                   With Me
                            If Len(.TextBox1.Value) * Len(.PTD.Value) * Len(.RackD.Value) * _
                Len(.OperatorD.Value) = 0 Then
            MsgBox "Please Complete All Fields Before Submit"
        Else
        
        cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Track Out")
        If cDelete = vbYes Then
           If Sheets("Inventory").Columns(2).Find(What:=PTD.Text) Is Nothing Then
              MsgBox "No stock for this PT#"
              Exit Sub
           End If
           eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            Cells(eRow, 1).Value = TextBox1.Text
            Cells(eRow, 2).Value = PTD.Text
            Cells(eRow, 3).Value = RackD.Text
            Cells(eRow, 4).Value = OperatorD.Text
            
            Else
            
            If cDelete = vbNo Then
            Unload Me
            End If
            End If
        End If
    End With
   
End Sub
 
Upvote 0
After your If cDelete = vbYes Then line, I'd add another IF statement nested within that one. Try:

Code:
If Sheets("Inventory").Columns(2).Find(What:=PTD.Text) Is Nothing Then
    MsgBox "No stock for this PT#"
    exit sub
End If

All together now:
Code:
Private Sub TrackOut_Click()Dim cDelete As VbMsgBoxResult
                   With Me
                            If Len(.TextBox1.Value) * Len(.PTD.Value) * Len(.RackD.Value) * _
                Len(.OperatorD.Value) = 0 Then
            MsgBox "Please Complete All Fields Before Submit"
        Else
        
        cDelete = MsgBox("Are you sure that you want to delete this record", vbYesNo + vbDefaultButton2, "Track Out")
        If cDelete = vbYes Then
           If Sheets("Inventory").Columns(2).Find(What:=PTD.Text) Is Nothing Then
              MsgBox "No stock for this PT#"
              Exit Sub
           End If
           eRow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
            Cells(eRow, 1).Value = TextBox1.Text
            Cells(eRow, 2).Value = PTD.Text
            Cells(eRow, 3).Value = RackD.Text
            Cells(eRow, 4).Value = OperatorD.Text
            
            Else
            
            If cDelete = vbNo Then
            Unload Me
            End If
            End If
        End If
    End With
   
End Sub
yes! is working. Thank you so much.
 
Upvote 0
Why don't you use a ComboBox filled with the PT#s from the Stock In worksheet? Then you won't have to validate the user's selection.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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