Textbox Not Populated well after double click listbox

Cyril Beki

Board Regular
Joined
Sep 18, 2021
Messages
57
Office Version
  1. 2016
Platform
  1. Windows
When i double click the listbox, the data shown in textbox is not sorted well (Refer Image).
Based on the image 1st image, textbox 1 should be blank , only textbox 2 is populated
And for Image 2, it should doing the same things whereby Textbox 2 should be blank, only textbox 1 is populated

Combobox Change 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

Add Data Code
VBA Code:
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 & Me.TextBox2.Value

On Error Resume Next
Me.ListBox1.RowSource = Sheet2.Range("Problem").Address(external:=True)

MsgBox ("Data is added succesfully")
'Clear form after add
Me.ComboBox1.Value = ""
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
End Sub

Populate Data from listbox to textbox Code
VBA Code:
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)
 

Attachments

  • Machine.PNG
    Machine.PNG
    14 KB · Views: 27
  • Not sorted.PNG
    Not sorted.PNG
    19.6 KB · Views: 26
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,224,813
Messages
6,181,115
Members
453,021
Latest member
Justyna P

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