设想有一天你想做一个问卷调查,有不想太浪费纸,可以用Excel做一个简单地问卷,答卷者只需在excel上填写即可。或者收集了纸质问卷,有我们自行手动选择,进行统计。
在表格中设计问卷如图,我是直接从word中导进来的。
分别在“确认提交”和“RESET”控件下编写如下代码。
'''Option Base 1
Dim T1(1 To 36)
Private Sub CommandButton1_Click()msg = MsgBox("确定要提交你的答卷码?提交操作不可回退,请慎重考虑!", 1, "提交确认")If msg = 2 ThenGoTo overEnd IfFor k = 1 To 9If t(k, 1) + t(k, 2) + t(k, 3) + t(k, 4) = 0 ThenMsgBox "第" & k & "题未选择"GoTo overEnd IfNext kFor i = 1 To 9For j = 1 To 4T1((i - 1) * 4 + j) = t(i, j)Next jNext iActiveSheet.Unprotects = Sheet1.Range("C1").ValueSheet1.Range("C1").Value = Sheet1.Range("C1").Value + 1ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=FalseSheet2.Range("B" & s + 1, "AK" & s + 1).Value = T1Sheet2.Range("A" & s + 1) = s
over:
End SubPrivate Sub CommandButton2_Click()ActiveSheet.UnprotectSheet1.Range("C1").Value = 1ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=FalseSheet2.Range("A2", "AK100").ClearContents
End Sub
- 编写选项按钮宏代码,并将分组框中对应选项按钮点击事件处理指定到对应的宏上。选项按钮命名数字表示题号和选项号。
Option Base 1
Public t(9, 4) As IntegerSub 选项按钮11_Click()t(1, 1) = 1t(1, 2) = 0t(1, 3) = 0t(1, 4) = 0
End Sub
Sub 选项按钮12_Click()t(1, 1) = 0t(1, 2) = 1t(1, 3) = 0t(1, 4) = 0
End Sub
Sub 选项按钮13_Click()t(1, 1) = 0t(1, 2) = 0t(1, 3) = 1t(1, 4) = 0
End Sub
Sub 选项按钮14_Click()t(1, 1) = 0t(1, 2) = 0t(1, 3) = 0t(1, 4) = 1
End Sub
Sub 选项按钮21_Click()t(2, 1) = 1t(2, 2) = 0t(2, 3) = 0t(2, 4) = 0
End Sub
Sub 选项按钮22_Click()t(2, 1) = 0t(2, 2) = 1t(2, 3) = 0t(2, 4) = 0
End Sub
Sub 选项按钮23_Click()t(2, 1) = 0t(2, 2) = 0t(2, 3) = 1t(2, 4) = 0
End Sub
Sub 选项按钮24_Click()t(2, 1) = 0t(2, 2) = 0t(2, 3) = 0t(2, 4) = 1
End Sub
Sub 选项按钮31_Click()t(3, 1) = 1t(3, 2) = 0t(3, 3) = 0t(3, 4) = 0
End Sub
Sub 选项按钮32_Click()t(3, 1) = 0t(3, 2) = 1t(3, 3) = 0t(3, 4) = 0
End Sub
Sub 选项按钮33_Click()t(3, 1) = 0t(3, 2) = 0t(3, 3) = 1t(3, 4) = 0
End Sub
Sub 选项按钮34_Click()t(3, 1) = 0t(3, 2) = 0t(3, 3) = 0t(3, 4) = 1
End Sub
Sub 选项按钮41_Click()t(4, 1) = 1t(4, 2) = 0t(4, 3) = 0t(4, 4) = 0
End Sub
Sub 选项按钮42_Click()t(4, 1) = 0t(4, 2) = 1t(4, 3) = 0t(4, 4) = 0
End Sub
Sub 选项按钮43_Click()t(4, 1) = 0t(4, 2) = 0t(4, 3) = 1t(4, 4) = 0
End Sub
Sub 选项按钮44_Click()t(4, 1) = 0t(4, 2) = 0t(4, 3) = 0t(4, 4) = 1
End Sub
Sub 选项按钮51_Click()t(5, 1) = 1t(5, 2) = 0t(5, 3) = 0t(5, 4) = 0
End Sub
Sub 选项按钮52_Click()t(5, 1) = 0t(5, 2) = 1t(5, 3) = 0t(5, 4) = 0
End Sub
Sub 选项按钮53_Click()t(5, 1) = 0t(5, 2) = 0t(5, 3) = 1t(5, 4) = 0
End Sub
Sub 选项按钮54_Click()t(5, 1) = 0t(5, 2) = 0t(5, 3) = 0t(5, 4) = 1
End Sub
Sub 选项按钮61_Click()t(6, 1) = 1t(6, 2) = 0t(6, 3) = 0t(6, 4) = 0
End Sub
Sub 选项按钮62_Click()t(6, 1) = 0t(6, 2) = 1t(6, 3) = 0t(6, 4) = 0
End Sub
Sub 选项按钮63_Click()t(6, 1) = 0t(6, 2) = 0t(6, 3) = 1t(6, 4) = 0
End Sub
Sub 选项按钮64_Click()t(6, 1) = 0t(6, 2) = 0t(6, 3) = 0t(6, 4) = 1
End Sub
Sub 选项按钮71_Click()t(7, 1) = 1t(7, 2) = 0t(7, 3) = 0t(7, 4) = 0
End Sub
Sub 选项按钮72_Click()t(7, 1) = 0t(7, 2) = 1t(7, 3) = 0t(7, 4) = 0
End Sub
Sub 选项按钮73_Click()t(7, 1) = 0t(7, 2) = 0t(7, 3) = 1t(7, 4) = 0
End Sub
Sub 选项按钮74_Click()t(7, 1) = 0t(7, 2) = 0t(7, 3) = 0t(7, 4) = 1
End Sub
Sub 选项按钮81_Click()t(8, 1) = 1t(8, 2) = 0t(8, 3) = 0t(8, 4) = 0
End Sub
Sub 选项按钮82_Click()t(8, 1) = 0t(8, 2) = 1t(8, 3) = 0t(8, 4) = 0
End Sub
Sub 选项按钮83_Click()t(8, 1) = 0t(8, 2) = 0t(8, 3) = 1t(8, 4) = 0
End Sub
Sub 选项按钮84_Click()t(8, 1) = 0t(8, 2) = 0t(8, 3) = 0t(8, 4) = 1
End Sub
Sub 选项按钮91_Click()t(9, 1) = 1t(9, 2) = 0t(9, 3) = 0t(9, 4) = 0
End Sub
Sub 选项按钮92_Click()t(9, 1) = 0t(9, 2) = 1t(9, 3) = 0t(9, 4) = 0
End Sub
Sub 选项按钮93_Click()t(9, 1) = 0t(9, 2) = 0t(9, 3) = 1t(9, 4) = 0
End Sub
Sub 选项按钮94_Click()t(9, 1) = 0t(9, 2) = 0t(9, 3) = 0t(9, 4) = 1
End Sub
- 对表2中“统计分析”的处理代码如下。主要生成统计图。
Private Sub CommandButton1_Click()Dim g(1 To 9, 1 To 4) As Integerm = Sheet1.Range("C1").Value - 1Sheet2.Range("A" & m + 2) = "Total"Sheet2.Range("B" & m + 2, "AK" & m + 2).Formula = "=Sum(R[" & -m & "]C:R[-1]C)"For i = 1 To 9For j = 1 To 4g(i, j) = Sheet2.Cells(m + 2, (i - 1) * 4 + j + 1)Next jNext iSheet2.Range("B" & m + 5, "E" & m + 13).Value = gFor n = 1 To 9Sheet2.Cells(m + 5 + n - 1, 1).Value = nNext nFor l = 1 To 4Sheet2.Cells(m + 4, l + 1) = Chr(64 + l)Next lRange("A" & m + 4, "E" & m + 13).SelectActiveSheet.Shapes.AddChart.SelectActiveChart.ChartType = xlColumnClusteredActiveChart.SetSourceData Source:=Range("A" & m + 4, "E" & m + 13)End Sub
具体的就不多说了,提几点注意的地方:
1、为了避免问卷回退的引起的误操作,点击确认提交后,在当前表中不可再修改已提交问卷,即只能对自己的问卷进行操作。请务必在确认无误后提交。若要修改,请到第二张表中找到相应题号,进行修改。此举目的在于避免你答问卷时,将别人的问卷搞得一团糟。
2、份数从1开始依次递增,若小于1,警告框提示。
3、第二张表中,点击统计分析,生成统计图。这里以柱状图为例。其他类似。
4、之所以不用弹窗显示,单题选择的形式,是因为这种方式不利于提交问卷前做最后的修改。
缺点:很多代码都是重复性的工作,虽然是以复制粘贴小修改的形式完成的,其实,可以想办法用更简短的代码表示的。
1、
Sub 选项按钮1_Click()
MsgBox “呵呵哒”
End Sub
表示:按按钮1,弹出窗体提示。
2、
Sheet2.Range(“A1”).Value = 100
表示相应格中的值设置为100。
3、
Sheet1.Range(“A” & (Sheet0.Range(“I5”).Value + 1)).Value =1
这种写法表示Sheet1的AI单元格的值设置为1,其中(Sheet0.Range(“I5”).Value + 1)表示的是A对应的行标。可能是”A1”,可能是”A2”,以此类推。如果是范围的话,中间用冒号隔开。
4、
表单控件和ACTIVEX控件的区别:一个主要区别就是表单控件可以和单元格关联,操作控件可以修改单元格的值,所以用于工作表,而ACTIVEX控件虽然属性强大,可控性强,但不能和单元格关联,所以用于表单Form。
5、
ActiveSheet.Unprotect “12345”
”’Cells.Locked = False
Sheet1.Range(“C3”).Locked = False
Sheet1.Range(“C3”).Locked = True
ActiveSheet.Protect “12345”
对C3单元格进行加锁保护。
4、
Sheet2.Range(“B” & m + 2, “AK” & m + 2).Formula = “=Sum(R[” & -m & “]C:R[-1]C)”
表示对m+2行的B到AK列使用求和公式,求和区域为向上m行到向上一行。m为变量,要单独写到字符串外,用&连接。
最后分享一个小技巧,当你想实现某种功能,却不知语法如何的时候,可以通过录制宏的方式,来进行查看、和修改代码。另外,貌似选中某个东西摁F1是查看帮助,表示没用过。其次,就是百度了。