本宏命令专为使用WPS表格管理题库的用户设计,可快速将单选题、多选题的正确选项内容自动插入题干括号中,解决手动复制粘贴效率低、易出错的问题。适用于教师、培训师、题库编辑者等需批量整理标准化试题的场景,或者便于学生更好的熟记试题内容,减少还需按照选项选取正确答案的时间。
核心功能- 智能识别选项列
自动扫描表头(支持A-F列),识别选项内容所在列,无需手动指定列号。
示例:表头为A、B、C、D、E、F的选项列均可被识别。
- 多题型兼容
单选题:直接插入单个选项内容(如(正确答案))。
多选题:自动合并多个选项,按字母排序并用顿号分隔(如(答案A、答案C))。
- 灵活括号处理
支持中英文括号()、(),以及[]、【】等特殊括号格式。
若无括号,自动在题干末尾追加括号并填充答案。
- 答案验证与清洗
自动过滤非法字符(如数字、符号),仅保留有效选项字母(A-F)。
错误提示机制:检测到无效答案或缺失选项列时,弹窗提示具体错误位置。
- 自动化排序
无论答案输入顺序如何,最终按字母顺序排列(如DB → BD)。
使用条件- 环境要求
软件:WPS Office(需启用VBA宏功能)。
系统:Windows/macOS(需配置VBA环境)。
- 数据格式规范
列位置:
A列:题干(需包含括号占位符,如( ))。
B列:正确答案(如A、BC)。
C列及之后:选项内容,表头需为字母A-F(如C列表头为A,对应选项A内容)。
答案格式:
单选:单个字母(如D)。
多选:连续字母,无需分隔符(如ACE)。
- 其他要求
表头必须为纯字母(如A,而非A选项)。
至少存在一个有效选项列(如A列)。
使用步骤- 准备数据
按格式要求整理题库,确保题干在A列,答案在B列。
示例:
A列(题干)B列(答案)C列(A)D列(B)
题目...( ) BC 内容A 内容B
- 导入宏命令
按 Alt F11 打开VBA编辑器 → 新建模块 → 粘贴完整代码 → 保存。
- 执行宏
返回表格界面 → 按 Alt F8 → 选择智能合并答案_支持多选项 → 点击【运行】。
- 验证结果
题干括号内自动填充答案内容,如:
题目...(内容B、内容C)
- 数据备份
运行前务必备份原始数据,防止误操作导致信息丢失。
- 表头规范
选项列的表头必须为单个字母(A-F),否则无法识别。
- 答案清洗规则
若答案含非法字符(如A1B),将自动清洗为AB。
若答案字母在选项列中不存在(如答案含G但无G列),会触发错误提示。
- 特殊符号处理
题干中的括号需为成对符号(如(需对应)),否则可能插入位置错误。
适用场景- 从外部系统导出的题库标准化(如将PDF题库转为结构化表格)。
- 生成带答案的练习卷或考试样题。
- 快速验证题库答案与选项的匹配性。
通过此工具,用户可节省80%以上的手动操作时间,尤其适合处理数百至数千题的批量任务,显著提升题库管理效率。
附:宏命令代码,可粘贴后试一试。
Sub 智能合并答案_支持多选项()
Dim ws As Worksheet
Dim colMap As Object
Dim lastRow As Long
Dim rowNum As Long
Set ws = ActiveSheet
Set colMap = CreateObject("Scripting.Dictionary")
' 构建选项列映射(支持A-F)
For Each cell In ws.Rows(1).Cells
Dim headerChar As String
headerChar = UCase(Trim(cell.Value))
If Len(headerChar) = 1 And headerChar >= "A" And headerChar <= "F" Then
colMap(headerChar) = cell.Column
End If
Next
If colMap.Count = 0 Then
MsgBox "未找到任何有效的选项列(A-F)!", vbCritical
Exit Sub
End If
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
On Error GoTo ErrorHandler
For rowNum = 2 To lastRow
Dim question As String, answers As String
question = ws.Cells(rowNum, 1).Value
answers = UCase(Trim(ws.Cells(rowNum, 2).Value))
' 清洗答案字符串
answers = CleanAnswerString(answers)
If Len(answers) < 1 Then
MsgBox "第" & rowNum & "行缺少正确答案!", vbExclamation
Exit Sub
End If
' 验证并处理答案
Dim validAnswers As String
validAnswers = GetValidAnswers(colMap, answers)
If Len(validAnswers) = 0 Then
MsgBox "第" & rowNum & "行答案无效:" & answers, vbCritical
Exit Sub
End If
' 构建正确答案文本
Dim correctText As String
correctText = BuildCorrectText(ws, rowNum, colMap, validAnswers)
' 更新题目内容
UpdateQuestion ws, rowNum, question, correctText
Next
MsgBox "成功处理 " & lastRow - 1 & " 道题目!", vbInformation
Exit Sub
ErrorHandler:
MsgBox "错误 " & Err.Number & ":" & Err.Description & vbCrLf & _
"发生在第 " & rowNum & " 行", vbCritical
End Sub
' 清洗答案字符串(支持A-F)
Private Function CleanAnswerString(ByVal ansStr As String) As String
Dim result As String, i As Integer, char As String
result = ""
For i = 1 To Len(ansStr)
char = UCase(Mid(ansStr, i, 1))
If char >= "A" And char <= "F" Then
result = result & char
End If
Next
CleanAnswerString = result
End Function
' 验证答案有效性(支持A-F)
Private Function GetValidAnswers(ByRef colMap As Object, ByVal answers As String) As String
Dim result As String, char As String, i As Integer
result = ""
For i = 1 To Len(answers)
char = Mid(answers, i, 1)
If colMap.exists(char) Then
If InStr(result, char) = 0 Then
result = result & char
End If
End If
Next
GetValidAnswers = SortAnswerString(result)
End Function
' 答案排序(支持A-F)
Private Function SortAnswerString(ByVal ansStr As String) As String
Dim arr() As String, i As Long, j As Long
ReDim arr(Len(ansStr) - 1)
For i = 1 To Len(ansStr)
arr(i - 1) = Mid(ansStr, i, 1)
Next
' 改进的冒泡排序
For i = LBound(arr) To UBound(arr) - 1
For j = i 1 To UBound(arr)
If arr(i) > arr(j) Then
Swap arr(i), arr(j)
End If
Next
Next
SortAnswerString = Join(arr, "")
End Function
Private Sub Swap(ByRef a As String, ByRef b As String)
Dim temp As String
temp = a
a = b
b = temp
End Sub
' 构建正确答案文本(支持A-F)
Private Function BuildCorrectText(ws As Worksheet, ByVal rowNum As Long, _
ByRef colMap As Object, ByVal answers As String) As String
Dim result As String, i As Integer, ans As String
result = ""
For i = 1 To Len(answers)
ans = Mid(answers, i, 1)
If colMap.exists(ans) Then
result = result & ws.Cells(rowNum, colMap(ans)).Value & "、"
End If
Next
If Len(result) > 0 Then result = Left(result, Len(result) - 1)
BuildCorrectText = result
End Function
' 更新题目内容(优化括号处理)
Private Sub UpdateQuestion(ws As Worksheet, ByVal rowNum As Long, _
question As String, answerText As String)
Const BRACKETS = "()()[]【】"
Dim pos As Long, closePos As Long
Dim openChar As String, closeChar As String
' 查找首个开括号
For i = 1 To Len(BRACKETS) Step 2
openChar = Mid(BRACKETS, i, 1)
closeChar = Mid(BRACKETS, i 1, 1)
pos = InStr(question, openChar)
If pos > 0 Then Exit For
Next
If pos > 0 Then
' 查找对应的闭括号
closePos = InStr(pos, question, closeChar)
If closePos > pos Then
' 替换括号内容
ws.Cells(rowNum, 1).Value = Left(question, pos) & answerText & Mid(question, closePos)
Else
' 修复不匹配括号
ws.Cells(rowNum, 1).Value = Left(question, pos - 1) & openChar & answerText & closeChar & Mid(question, pos 1)
End If
Else
' 无括号时添加新括号
ws.Cells(rowNum, 1).Value = question & "(" & answerText & ")"
End If
End Sub
,