Cyril Beki
Board Regular
- Joined
- Sep 18, 2021
- Messages
- 57
- Office Version
- 2016
- Platform
- Windows
Hello expert,
I need to populate data in the same column using different textbox (Textbox 1 & Textbox2). Textbox 1 or Textbox 2 will be disable and "gray out" according to the combo box(ComboBox1) selection. (Refer Image)
But when i click on the Add Button, the data doesn't populate inside the sheet especially when my Combobox Selection is "Selection" (Refer image). Below is my Code
I need to populate data in the same column using different textbox (Textbox 1 & Textbox2). Textbox 1 or Textbox 2 will be disable and "gray out" according to the combo box(ComboBox1) selection. (Refer Image)
But when i click on the Add Button, the data doesn't populate inside the sheet especially when my Combobox Selection is "Selection" (Refer image). Below is my Code
VBA Code:
Private Sub ComboBox1_Change()
If ComboBox1.Value = "Section" Then
TextBox1.Enabled = True
TextBox2.Enabled = False
TextBox2.BackColor = &H8000000F
TextBox1.BackColor = &H8000000E
Me.TextBox2.text = ""
TextBox1.text = "Please Enter New Section Here"
End If
If ComboBox1.Value = "Machine" Then
TextBox1.Enabled = False
TextBox2.Enabled = True
TextBox1.BackColor = &H8000000F
TextBox2.BackColor = &H8000000E
Me.TextBox1.text = ""
TextBox2.text = "Please Enter New Machine Here"
End If
End Sub
Private Sub Add_Click()
Dim text As Object
Set text = Sheet2.Range("A50000").End(xlUp)
'Add Data
text.Offset(1, 0).Value = Me.ComboBox1.Value
text.Offset(1, 1).Value = Me.TextBox1.Value
text.Offset(1, 1).Value = Me.TextBox2.Value
'Clear form after add
Me.ComboBox1.Value = ""
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
On Error GoTo Error
With TextBox1
If .text = "Please Enter New Section Here" Then
.ForeColor = &H80000008 '<~~ Black Color
.text = ""
End If
End With
Me.ComboBox1.Value = Me.ListBox1.Column(0)
Me.TextBox1.Value = Me.ListBox1.Column(1)
With TextBox2
If .text = "Please Enter New Machine Here" Then
.ForeColor = &H80000008 '<~~ Black Color
.text = ""
End If
End With
Me.ComboBox1.Value = Me.ListBox1.Column(0)
Me.TextBox2.Value = Me.ListBox1.Column(1)
Exit Sub
Error:
End Sub
Private Sub TextBox1_Enter()
If ComboBox1.Value = "Section" Then
With TextBox1
If .text = "Please Enter New Section Here" Then
.ForeColor = &H80000008 '<~~ Black Color
.text = ""
End If
End With
End If
End Sub
Private Sub TextBox2_Enter()
If ComboBox1.Value = "Machine" Then
With TextBox2
If .text = "Please Enter New Machine Here" Then
.ForeColor = &H80000008 '<~~ Black Color
.text = ""
End If
End With
End If
End Sub
Private Sub TextBox1_AfterUpdate()
With TextBox1
If .text = "" Then
.ForeColor = &HFF0000
.text = "Please Enter New Section Here"
End If
End With
End Sub
Private Sub TextBox2_AfterUpdate()
With TextBox2
If .text = "" Then
.ForeColor = &HFF0000
.text = "Please Enter New Machine Here"
End If
End With
End Sub
Private Sub UserForm_Initialize()
'Instructional Text Inside Textbox
TextBox1.ForeColor = &HFF0000
TextBox2.ForeColor = &HFF0000 '<~~ Blue Color
TextBox1.text = "Please Enter New Section Here"
TextBox2.text = "Please Enter New Machine Here"
'Content in normal combobox
With ComboBox1
.AddItem "Section"
.AddItem "Machine"
.AddItem "Module"
.AddItem "Problem Category"
.AddItem "Problem"
End With
'Populate Listbox
On Error Resume Next
Me.ListBox1.RowSource = Sheet2.Range("Problem").Address(external:=True)
End Sub