|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Option Explicit
- Private Function Cnn() As Object
- Set Cnn = CreateObject("adodb.connection")
- If Application.Version < 12 Then
- Cnn.Provider = "Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0"
- Else
- Cnn.Provider = "Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0"
- End If
- Cnn.Open ThisWorkbook.FullName
- End Function
- Sub 生成桌签()
- Dim i&, n%, SQL$, Rst As Object
- Dim Arr(1 To 30, 1 To 6), x%, y%, d As Object, k
- Set d = CreateObject("Scripting.Dictionary")
- Set Rst = CreateObject("adodb.Recordset")
-
- Rem 获取考场考场号
- If Rst.State = 1 Then Rst.Close
- Rst.Open "SELECT DISTINCT 考场号 FROM [考场库$]", Cnn, 1, 3
- Do While Not Rst.EOF
- d(Rst.Fields("考场号").Value) = ""
- Rst.MoveNext
- Loop
-
- Rem 开始编排生成文件
- Sheets("桌签模版").Select
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Range("A31:F65536").Clear
- For Each k In d.keys '按考场生成文件夹
- If Rst.State = 1 Then Rst.Close
- SQL = "SELECT * FROM [考场库$] WHERE 考场号='" & k & "' ORDER BY 座位号"
- Rst.Open SQL, Cnn, 1, 3
- If Rst.RecordCount > 30 Then
- MsgBox k & "考场 人数为 " & Rst.RecordCount, vbCritical, "系统警告"
- Exit Sub
- End If
- Rem 记录按5行6列写入数组
- n = 0: x = 0: y = 1: Erase Arr
- Do While Not Rst.EOF
- n = n + 1
- x = (((n - 1) Mod 5)) * 6 + 1
- Arr(x + 1, y) = "座位号:" & Rst.Fields("座位号").Value
- Arr(x + 2, y) = "姓名:" & Rst.Fields("姓名").Value
- Arr(x + 3, y) = "班级:" & Rst.Fields("班级").Value
- Arr(x + 4, y) = "准考证号:" & Rst.Fields("准考证号").Value
- Rst.MoveNext
- If n Mod 5 = 0 Then y = y + 1
- Loop
- Rem 输出到文件指定位置
- Range("A1:F30") = Arr
- With ActiveSheet.PageSetup '设置页脚
- .LeftHeader = ""
- .CenterHeader = k & " 考 场 学 生 信 息"
- .RightHeader = ""
- .LeftFooter = ""
- .CenterFooter = ""
- .RightFooter = ""
- End With
-
- Rem 直接打印输出或保存为PDF文件,根据需要自行选择
- 'Range("A1:F30").PrintOut '直接打印输出
- Rem 另存为PDF文档
- Sheets("桌签模版").Copy
- ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=k & "考场学生信息.pdf", Quality:=xlQualityStandard, _
- IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
- ActiveWorkbook.Close False
- Next
-
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- Set d = Nothing
- Set Rst = Nothing
- End Sub
复制代码 |
|