Textbox Not Populated well after double click listbox

Cyril Beki

Board Regular
Joined
Sep 18, 2021
Messages
53
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: 15
  • Not sorted.PNG
    Not sorted.PNG
    19.6 KB · Views: 15
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,251
Messages
5,836,266
Members
430,414
Latest member
ayla

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
Top