Excel VBA用户窗体ListBox添加多选框标题及数据编辑更新代码问题求助

非常感谢您的回复,附件为我所用用户窗体的截图。我已通过其他方式完成了ListBox的数据加载,目前遇到数据更新编辑的问题:我尝试通过下方代码实现双击ListBox时,将选中行的数据读取到对应TextBox和CheckBox中以便编辑:
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工作表中:
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
针对评论的补充说明:
"我尝试通过上述第一段代码将ListBox中的数据加载到6个CheckBox和6个TextBox中,目前遇到的问题是运行代码后,所有TextBox和CheckBox都只显示ListBox第一行的数据。第二段代码的作用是将编辑后的数据更新到Excel工作表中。"
目前我始终无法得到正确的运行结果,恳请帮忙排查上述代码中的错误,万分感谢您的回复。
问题排查与修正方案
双击加载事件错误修复
- 循环逻辑错误:
For p = 0 To Me.ListBox1.ListCount < 1是布尔判断,列表行数大于1时条件结果为0,循环仅执行p=0的情况,所以永远只加载第一行数据。 - 不需要循环:直接读取选中行(
ListIndex对应行)即可,不需要遍历所有行。 - 列索引错误:所有控件都读取同一列内容,导致值全部相同,需根据你ListBox的实际列对应关系调整索引。以下示例假设ListBox列规则为:第0列ID、第1列项目名、第2列预留、第3列Small勾选、第4列Small数量、第5列Medium勾选、第6列Medium数量、第7列Large勾选、第8列Large数量、第9列XL勾选、第10列XL数量、第11列XXL勾选、第12列XXL数量、第13列XXXL勾选、第14列XXXL数量,可按实际调整列号。
修正后代码:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) 'UPDATE LISTBOX DATA ' 未选中有效行直接退出 If Me.ListBox1.ListIndex = -1 Then Exit Sub Dim selRow As Long selRow = Me.ListBox1.ListIndex Me.ComboBoxitem.Value = Me.ListBox1.List(selRow, 1) Me.TextBox1.Value = Me.ListBox1.List(selRow, 0) ' 按列对应赋值 Me.CheckBoxSmall.Value = Me.ListBox1.List(selRow, 3) Me.CheckBoxMedium.Value = Me.ListBox1.List(selRow, 5) Me.CheckBoxLarge.Value = Me.ListBox1.List(selRow, 7) Me.CheckBoXL.Value = Me.ListBox1.List(selRow, 9) Me.CheckBoXXL.Value = Me.ListBox1.List(selRow, 11) Me.CheckBoXXXL.Value = Me.ListBox1.List(selRow, 13) Me.txtsmallqty.Value = Me.ListBox1.List(selRow, 4) Me.TextBoxmedium.Value = Me.ListBox1.List(selRow, 6) Me.TextBoxlarge.Value = Me.ListBox1.List(selRow, 8) Me.TextBoXL.Value = Me.ListBox1.List(selRow, 10) Me.TextBoxxL.Value = Me.ListBox1.List(selRow, 12) Me.TextBoxxxL.Value = Me.ListBox1.List(selRow, 14) End Sub
数据更新事件错误修复
所有控件值都写入同一个D、E列单元格,后续赋值会覆盖之前的内容,仅保留最后一个控件的值,需每个控件对应独立的工作表列。以下示例假设Data表列规则为:A列ID、B列项目名、C列预留、D列Small勾选、E列Small数量、F列Medium勾选、G列Medium数量、H列Large勾选、I列Large数量、J列XL勾选、K列XL数量、L列XXL勾选、M列XXL数量、N列XXXL勾选、O列XXXL数量,可按实际调整列号。
修正后代码:
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("F" & L) = Me.CheckBoxMedium.Value th.Range("H" & L) = Me.CheckBoxLarge.Value th.Range("J" & L) = Me.CheckBoXL.Value th.Range("L" & L) = Me.CheckBoXXL.Value th.Range("N" & L) = Me.CheckBoXXXL.Value th.Range("E" & L) = Me.txtsmallqty.Value th.Range("G" & L) = Me.TextBoxmedium.Value th.Range("I" & L) = Me.TextBoxlarge.Value th.Range("K" & L) = Me.TextBoXL.Value th.Range("M" & L) = Me.TextBoxxL.Value th.Range("O" & 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
内容的提问来源于stack exchange,提问作者Mubashir Ijaz
相关产品推荐
相关产品推荐

