关于将 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 Preference
在cbo_fac1、cbo_fac2、cbo_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
提前非常感谢!