在用户表单的列表框中添加多个复选框标题

Add multiple checkbox caption in List box in User Form

enter image description here

非常感谢您的回复,请在附件中找到用户表单的图片我通过其他方式在列表框中获取了数据否我遇到更新和编辑数据的问题。我正在尝试通过以下代码将数据从列表框调用到文本框和复选框以进行编辑。

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

为了在编辑后更新 excel sheet 中的数据,我使用以下代码:

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

因评论而增加:

"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."

但我无法得到完美的结果,请您检查上面的代码,让我知道我错在哪里。

我们将不胜感激您的友好回应。

由于您总是为每个所选项目显示六行(对应于 Small,Medium,...,XXXL 的六种尺寸),并且项目信息仅在第一行,因此主要问题是通过以下方式获得正确的 .ListIndex双击列表框中的任意行。

  • 1. 起始行索引 p(包含序列号和产品名称)可以使用 int( eger) 除法乘以六行得到第一行(见第 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. 为了避免无休止的赋值,我定义了两个变体数组(chkboxestxtboxes),其中包含复选框和文本框名称(请参阅第 2 节)。 - 另一种常用的方法是枚举控件名称,以便在以后的循环中进行分配。

  • 3. 第 3 步将列表框的主要信息 (3a) 和与大小相关的值 (3b) 分配给所有单个控件;后一个动作在 loop 中执行,通过 Me.Controls(chkboxes(i)).ValueMe.Controls(txtboxes(i)).Value.

    引用控件

下面的代码示例应该给你一个 start 并允许你自己完成第二个过程 (提醒:不要重载 post 独立问题太多,关注一个问题:-;)

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