Need Help adding code to userform

Status
Not open for further replies.

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi, I need help adding another option button to my userform, I have one (opt_In) and would like to add another (opt_Out). At the moment my userform consist of:
1 option button (opt_In)
1 combobox
7 textboxes
1 date picker
2 command buttons (submit & exit)
I have 2 sheets (IngData & IngMaster)

When the userform if filled in the data is tranfered to IngData and textbox5 is also transferred to IngMaster which adds the value back in to stock what I would like to do is add another option button to the userform (opt_Out) which would also transfer to its data to IngData and deduct from the stock in IngMaster.

UserForm Data Into IngData
column A combobox 1
column B textbox 1
column I textbox 2
column C textbox 3
column D textbox 4
column E textbox 5 this also puts the value into IngMaster column D
column F textbox 6
column H textbox 7
column G date picker
column K date & time
column J replaces the true or false value with In. This would need to change to (Out) when opt_Out is selected.
Use link to see userform and sheets. https://www.dropbox.com/s/m95u5skdxnjgj0x/TESTER1.xlsb?dl=0

Below is the code I have in my userform with just one option button (opt_In) which works fine.

Code:
Dim wsIngMaster As Worksheet, wsIngData As Worksheet
 
Private Sub UserForm_Initialize()
    Dim LastRow As Long
   
    Set wsIngMaster = ThisWorkbook.Worksheets("IngMaster")
    Set wsIngData = ThisWorkbook.Worksheets("IngData")
   
    Me.DTPicker1.Value = Date
   
    LastRow = wsIngMaster.Range("A" & wsIngMaster.Rows.count).End(xlUp).Row
   
    Me.ComboBox1.List = wsIngMaster.Cells(3, 1).Resize(LastRow, 1).Value2
   
    Me.CommandButton1.Enabled = False
   
    With Application
        .WindowState = xlMaximized
        Me.Zoom = Int(.width / Me.width * 80)
        Me.width = .width
        Me.height = .height
    End With
End Sub
Private Sub ComboBox1_Change()
    With Me.ComboBox1
        Me.TextBox1.Value = wsIngMaster.Cells(.ListIndex + 3, 2).Value
        Me.CommandButton1.Enabled = CBool(Len(.Text) > 0)
        TextBox2.SetFocus
    End With
End Sub
 
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  Call matchText
End Sub
Sub matchText()
        If ComboBox1.Value = "" Or TextBox3.Value = "" Then Exit Sub
        If TextBox3.Value = ComboBox1.Value Then
        TextBox1.BackColor = RGB(51, 255, 51)
        TextBox2.BackColor = RGB(51, 255, 51)
        TextBox3.BackColor = RGB(51, 255, 51)
        ComboBox1.BackColor = RGB(51, 255, 51)
        Else
        TextBox1.BackColor = RGB(255, 0, 0)
        TextBox2.BackColor = RGB(255, 0, 0)
        TextBox3.BackColor = RGB(255, 0, 0)
        ComboBox1.BackColor = RGB(255, 0, 0)
        result = MsgBox("NO MATCH Please Try Again", vbOKOnly)
        If result = vbOK Then
        Dim LastRow As Long, ws As Worksheet
    Set ws = Sheets("IngData")
    LastRow = ws.Range("A" & Rows.count).End(xlUp).Row + 1 'Finds the last blank row
    ws.Range("B" & LastRow).Value = TextBox1.Text
    ws.Range("I" & LastRow).Value = TextBox2.Text
    ws.Range("C" & LastRow).Value = TextBox3.Text
    ws.Range("E" & LastRow).Value = TextBox4.Text
    ws.Range("A" & LastRow).Value = ComboBox1.Text
    ws.Range("J" & LastRow).Value = opt_In.Value
    Columns("J").Select
Selection.Replace What:="True", Replacement:="FAIL", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Opt_In.Value = False
Range("K" & Rows.count).End(xlUp).Offset(1).Value = Now
        ComboBox1.Value = ""
        TextBox1.Value = ""
        TextBox2.Value = ""
        TextBox3.Value = ""
        TextBox1.BackColor = RGB(255, 255, 255)
        TextBox2.BackColor = RGB(255, 255, 255)
        TextBox3.BackColor = RGB(255, 255, 255)
        ComboBox1.BackColor = RGB(255, 255, 255)
        End If
    End If
   End Sub
Private Sub CommandButton1_Click()
    Dim NewRow As Long
    Dim m As Variant
   
    Application.DisplayAlerts = False
   
    With wsIngData
        NewRow = .Range("A" & .Rows.count).End(xlUp).Row + 1
        .Range("B" & NewRow).Value = Me.TextBox1.Text
        .Range("I" & NewRow).Value = Me.TextBox2.Text
        .Range("C" & NewRow).Value = Me.TextBox3.Text
        .Range("D" & NewRow).Value = Me.TextBox4.Text
        .Range("E" & NewRow).Value = Me.TextBox5.Text
        .Range("F" & NewRow).Value = Me.TextBox6.Text
        .Range("H" & NewRow).Value = Me.TextBox7.Text
        .Range("A" & NewRow).Value = Me.ComboBox1.Text
        .Range("J" & NewRow).Value = Me.opt_In.Value
        .Range("G" & NewRow).Value = Me.DTPicker1.Value
        .Range("K" & NewRow).Value = Now
        
        .Columns("J").Replace What:="True", Replacement:="IN", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
    End With
'update master
    With wsIngMaster
        m = Application.Match(Val(Me.ComboBox1.Text), .Columns(1), 0)
        If Not IsError(m) Then
            With .Cells(CLng(m), "D")
                .Value = .Value + Val(Me.TextBox5.Value)
            End With
        End If
    End With
    opt_In.Value = False
    Application.DisplayAlerts = True
    Unload Me
End Sub
 
Private Sub commandbutton2_click()
    Unload Me
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Status
Not open for further replies.

Forum statistics

Threads
1,223,155
Messages
6,170,405
Members
452,325
Latest member
BlahQz

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