Search code examples
excelvbauserform

Add multiple checkbox caption in List box in User Form


enter image description here

Many thanks for your reply, Please find attached a picture of the user form I Got the data in the list box by some other ways no I am facing an issue to update and edit the data. I am trying to call the data from Listbox to textbox and checkboxes by below code for Editing.

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 'UPDATE LISBOX DATA
Dim p As Integer

Me.ComboBoxitem.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 1)

For p = 0 To Me.ListBox1.ListCount < 1
      
Me.CheckBoxSmall.Value = Me.ListBox1.List(p, 3)
Me.CheckBoxMedium.Value = Me.ListBox1.List(p, 3)
Me.CheckBoxLarge.Value = Me.ListBox1.List(p, 3)
Me.CheckBoXL.Value = Me.ListBox1.List(p, 3)
Me.CheckBoXXL.Value = Me.ListBox1.List(p, 3)
Me.CheckBoXXXL.Value = Me.ListBox1.List(p, 3)

Me.txtsmallqty.Value = Me.ListBox1.List(p, 4)
Me.TextBoxmedium.Value = Me.ListBox1.List(p, 4)
Me.TextBoxlarge.Value = Me.ListBox1.List(p, 4)
Me.TextBoXL.Value = Me.ListBox1.List(p, 4)
Me.TextBoxxL.Value = Me.ListBox1.List(p, 4)
Me.TextBoxxxL.Value = Me.ListBox1.List(p, 4)

Next
Me.TextBox1.Value = Me.ListBox1.List(Me.ListBox1.ListIndex, 0)

End Sub

and for update the data in excel sheet after editing , I am using below code :

Private Sub CommandButton1_Click() ' Update Data
Dim L As Long
Dim th As Worksheet
Set th = ThisWorkbook.Sheets("Data")
L = Application.WorksheetFunction.Match(CLng(Me.TextBox1.Value), th.Range("A1:A1000"), 0)

th.Range("B" & L) = Me.ComboBoxitem.Value
th.Range("D" & L) = Me.CheckBoxSmall.Value
th.Range("D" & L) = Me.CheckBoxMedium.Value
th.Range("D" & L).Value = Me.CheckBoxLarge.Value
th.Range("D" & L).Value = Me.CheckBoXL.Value
th.Range("D" & L).Value = Me.CheckBoXXL.Value
th.Range("D" & L).Value = Me.CheckBoXXXL.Value

th.Range("E" & L) = Me.txtsmallqty.Value
th.Range("E" & L) = Me.TextBoxmedium.Value
th.Range("E" & L) = Me.TextBoxlarge.Value
th.Range("E" & L) = Me.TextBoXL.Value
th.Range("E" & L) = Me.TextBoxxL.Value
th.Range("E" & L) = Me.TextBoxxxL.Value
     
Me.CheckBoxSmall.Value = False
Me.CheckBoxMedium.Value = False
Me.CheckBoxLarge.Value = False
Me.CheckBoXL.Value = False

Me.CheckBoXXL.Value = False
Me.CheckBoXXXL.Value = False
Me.txtsmallqty.Value = ""
Me.TextBoxmedium.Value = ""
Me.TextBoxlarge.Value = ""
Me.TextBoXL.Value = ""
Me.TextBoxxL.Value = ""
Me.TextBoxxxL.Value = ""
Me.TextBox1.Value = ""

End Sub

Addition due to comment:

"I am trying to pull Listbox data in 6 checkboxes and 6 text boxes from the first code mention above, the Issue I am facing from this code, shows only data from the first line of Listbox to all text boxes and checkboxes.
By the mean of the second code I have to update data in excel sheet."

But I am not able to get the perfect result, you are requested to please review the above Code and let me know where I am Mistaking.

Your Kind Response will be Highly Appreciated.


Solution

  • As you are displaying always six rows per chosen item (corresponding to six sizes of Small,Medium,...,XXXL) with item info only in the 1st row, a main issue is to get the correct .ListIndex by doubleclicking to any row within the listbox.

    • 1. The start row index p (containing the serial# and product name) can be calculated from the currently double clicked .ListIndex using an int(eger) division multiplied by six rows to get to the first row (see section 1):

      p = (Me.ListBox1.ListIndex \ 6) * 6
      

    Example: a double click into .ListIndex of 0..5 results in the start row index p = 0, of 6..11 in 6, ... - i.e. always returning the first row of a bundle of six rows containing sizes.

    • 2. To avoid endless assignments I defined two variant arrays (chkboxes and txtboxes) containing the checkbox and textbox names (see section 2). - Another frequently used method consists in enumerating the control names facilitating assignments in a later loop.

    • 3. The 3rd step assigns the listbox'es main info (3a) and the size-related values (3b) to all single controls; the latter action is executed in a loop referring to the controls via Me.Controls(chkboxes(i)).Value and Me.Controls(txtboxes(i)).Value.

    The following code example should give you a start and allow to finish the 2nd procedure by yourself (remind: don't overload a post by too many independant questions, focus to one issue :-;)

    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 'UPDATE LISBOX DATA
    
    '1. get the start row containing the serial code,
    '  (even if doubleclicked in one of the five following rows)
        Dim p As Long                                ' instead of Integer
        p = (Me.ListBox1.ListIndex \ 6) * 6          ' each item has 6 rows (sizes available)
    
    '2. define arrays containing checkbox|textbox names
        Dim chkboxes, txtboxes
        chkboxes = Split("CheckBoxSmall,CheckBoxMedium,CheckBoxLarge,CheckBoXL,CheckBoXXL,CheckBoXXXL", ",")
        txtboxes = Split("txtsmallqty,TextBoxmedium,TextBoxlarge,TextBoXL,TextBoXXL,TextBoxxxL", ",")
    
    '3. a) write item name & Serial# to corresponding userform controls
        Me.ComboBoxitem.Value = Me.ListBox1.List(p, 1) ' Item name
        Me.TextBox1.Value = Me.ListBox1.List(p, 0)   ' Serial number
    
    '3. b) loop through all six rows representing sizes
        Dim i As Long
        For i = 0 To 5                               ' listbox items and both ctrl arrays are 0-based!
            Me.Controls(chkboxes(i)).Value = CBool(Me.ListBox1.List(p + i, 3)) ' 4th column has index 3!
            Me.Controls(txtboxes(i)).Value = Me.ListBox1.List(p + i, 4) ' 5th column has index 3!
        Next i
    
    End Sub