0

关于将 VBA 表单中的复选框、单选和下拉值插入 Excel 电子表格,我有一些多方面的问题。我目前正在尝试在我的代码中适应几个案例场景。

我要插入的第一组项目是具有以下名称/值的复选框:

Name         Value
==========   =====
chk_week1    1
chk_week2    2
chk_week3    3
chk_week4    4
chk_week5    5
chk_week6    6
chk_week7    7
chk_week8    8
chk_week9    9
chk_week10   10
chk_week11   11
chk_week12   12
chk_week13   13
chk_week14   14
chk_week15   15

如果用户选择了几个复选框,那么它应该被插入到表单中1,2,4,5——例如,如果用户选择了chk_week1, chk_week2, chk_week4 and chk_week5.


我要插入的第二组项目是从无线电组中选择的一个,在框架内具有以下名称/值fr_Priority

Name         Value
==========   =====
priority_y   Yes
priority_n   No

因此,如果用户选择priority_y然后Yes插入到 Excel 电子表格中。


我要插入的第三组项目来自三个下拉菜单。这相当简单,但用户需要在所有三个下拉菜单中进行选择。如果他们对偏好不感兴趣,那么他们会选择'No Preference'。如果用户做出此决定,则不应将任何内容插入单元格。存在以下名称/值:

Name         
==========   
cbo_fac1   
cbo_fac2
cbo_fac3   

例如,如果用户111,222,No Preferencecbo_fac1cbo_fac2cbo_fac3中进行选择,则只会111,222被插入。如果111,No Preference,No Preference被选中,那么只会111被插入。


这是我现在使用的代码:

Private Sub btnSubmit_Click()

Dim ws As Worksheet
Dim rng1 As Range
Set ws = Worksheets("main")

' Copy the data to the database
' Get last empty cell in column A
Set rng1 = ws.Cells(Rows.Count, "a").End(xlUp)

deptCodeSplit = Split(cbo_deptCode.Value, " ")

rng1.Offset(1, 0) = deptCodeSplit(0)
rng1.Offset(1, 1) = cbo_moduleCode.Value
rng1.Offset(1, 2) = cbo_moduleName.Value
rng1.Offset(1, 3) = txt_studentNo.Value
rng1.Offset(1, 4) = cbo_day.Value
rng1.Offset(1, 5) = cbo_period.Value
' rng1.Offset(1, 6) = weeks
rng1.Offset(1, 7) = cbo_weeks.Value
rng1.Offset(1, 8) = cbo_semester.Value
rng1.Offset(1, 9) = cbo_rounds.Value
rng1.Offset(1, 10) = cbo_priority.Value
' rng1.Offset(1, 11) = lectureStyle
rng1.Offset(1, 12) = txt_noRooms.Value
rng1.Offset(1, 13) = cbo_park.Value
' rng1.Offset(1, 14) = fac
' rng1.Offset(1, 15) = pref
rng1.Offset(1, 16) = txt_specialReq.Value

End Sub

提前非常感谢!

4

1 回答 1

1

对您遇到的问题进行一些澄清将有助于我为您提供更好的答案。听起来您正在尝试填充大小未知的范围,因为用户可以在第一组中选择最多 15 个项目,在第二组中选择 1 个,在第三组中选择最多三个。所以,第一件事我会做一些数组来保存用户选择的项目,比如..

      Dim group1 as arraylist
      Dim group2 as arraylist
      Dim group3 as arraylist

      ' Go through checkboxes and add values if they are checked
      if checkbox1.checked = true then
       group1.add(1)
      end if ' do that for each checkbox or if you have a checklistbox do 

      for each thing in checklistbox1
        if thing.checked=true then
        group1.add(checklistbox1.indexof(thing)-1)
        end if
        next

对第 2 组和第 3 组执行相同类型的程序

        if not dropdown1.selecteditem = "no preference" then
          group3.add(...)
         end if ' so an and so forth

现在,您可以将 group1、group2 和 group3 添加到工作表中,例如

         Dim StartingRange as range = ws.Cells(Rows.Count, "a").End(xlUp)
         Dim total_items as integer = (group1.items.count + group2.items.count + group3.items.count)

我不知道您要以哪种方式添加项目,在行中还是在列中,但您可以通过...调整起始范围的大小

           startrange.resize(totalitems, 1) ' resizes range to 1 column and total items in rows

然后

           Dim cell as variant

          for each cell in startrange
            cell.value = group1.item(0)
            group1.removeat(0)   ' this effectively moves the item at index 1, to index 0
             next 

为其他团体做

如果那没有帮助,请告诉我。我的 VBA 有点生疏了,所以我可能遗漏了代码中的一些东西,比如“设置”范围等,所以我对此深表歉意。

于 2012-10-29T03:15:24.967 回答