ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 377|回复: 1

[分享] 证书表格合并(宏)更新!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2024-1-13 00:52 | 显示全部楼层 |阅读模式
* 证书数据源一个字段:姓名、课题、单位、等级、证书编号。
* 请将代码复制到空白文档后,全选,剪切,再粘贴到 VBE 中,以防乱码!
  1. Sub CertificateTablesMerge()
  2. '证书表格合并
  3.     Dim t As Table, c As Cell, arr, i&, oRow&, oCol&, xRow&

  4.     arr = Split("证书编号/课题/姓名/单位/等级", "/")

  5.     With ActiveDocument
  6.         For Each t In .Tables
  7.             t.Rows.WrapAroundText = False
  8.             For Each c In t.Rows(1).Cells
  9.                 With c.Range
  10.                     If .Text Like "*编*号*" Then
  11.                         .Text = "证书编号"
  12.                     ElseIf .Text Like "*[课题]*[题目]*" Then
  13.                         .Text = "课题"
  14.                     ElseIf .Text Like "*姓*名*" Then
  15.                         .Text = "姓名"
  16.                     ElseIf .Text Like "*[单学]*[位校]*" Then
  17.                         .Text = "单位"
  18.                     ElseIf .Text Like "*[等奖]*[级项次]*" Then
  19.                         .Text = "等级"
  20.                     End If
  21.                 End With
  22.             Next
  23.         Next

  24.         If Not .Characters(1).Information(12) Then .Range(0, .Tables(1).Range.Start).Delete
  25.         If Not .Characters(1).Information(12) Then .Range(0, .Tables(1).Range.Start).Delete
  26.         Selection.HomeKey 6
  27.         Selection.SplitTable
  28.         .Tables.Add .Range(0, 0), 2, 5, 1
  29.         Set t = .Tables(1)
  30.         With t.Rows(1)
  31.             For i = 0 To UBound(arr)
  32.                 .Cells(i + 1).Range.Text = arr(i)
  33.             Next
  34.         End With

  35.         Do While .Tables.Count > 1
  36.             xRow = t.Range.Information(wdMaximumNumberOfRows)
  37.             For i = 0 To UBound(arr)
  38.                 For Each c In .Tables(2).Rows(1).Cells
  39.                     If c.Range.Text Like arr(i) & "*" Then
  40.                         c.Select
  41.                         With Selection
  42.                             oCol = .Information(wdEndOfRangeColumnNumber)
  43.                             oRow = .Information(wdMaximumNumberOfRows)
  44.                             ActiveDocument.Range(Start:=.Tables(1).Cell(2, oCol).Range.Start, _
  45.                                 End:=.Tables(1).Cell(oRow, oCol).Range.End).Select
  46.                             .Cut
  47.                             t.Cell(xRow, i + 1).Range.Select
  48.                             .HomeKey
  49.                             .Paste
  50.                         End With
  51.                     End If
  52.                 Next
  53.             Next
  54.             .Tables(2).Delete
  55.             t.Select
  56.             Selection.InsertRowsBelow 1
  57.         Loop

  58.         .Range(Start:=t.Range.End, End:=.Content.End).Delete
  59.     End With

  60.     TableProcess
  61.     TableDeleteBlankRows
  62. End Sub

  63. Sub TableProcess()
  64. '表格处理
  65.     Dim t As Table, c As Cell, i As Paragraph, a&

  66.     If Selection.Information(wdWithInTable) = True Then a = 1

  67.     For Each t In ActiveDocument.Tables
  68.         If a = 1 Then Set t = Selection.Tables(1)
  69.         With t
  70.             '取消环绕/左对齐/左缩进
  71.             With .Rows
  72.                 .WrapAroundText = False
  73.                 .Alignment = wdAlignRowLeft
  74.                 .LeftIndent = CentimetersToPoints(0)
  75.                 .HeightRule = wdRowHeightAtLeast
  76.                 .Height = CentimetersToPoints(0.9)
  77.             End With

  78.             '清除格式
  79.             With .Range
  80.                 .Next.InsertParagraphBefore
  81.                 .MoveEnd
  82.                 .Select
  83.                 Selection.ClearFormatting
  84.                 CommandBars.FindControl(ID:=122).Execute
  85.                 .MoveEnd 1, -1
  86.                 .Next.Delete
  87.                 With .Font
  88. '                    If Not ActiveDocument.Characters.Last.Font.Color = wdColorAutomatic Then
  89.                     .Color = wdColorBlue
  90.                     .Kerning = 0
  91.                     .DisableCharacterSpaceGrid = True
  92.                 End With
  93.                 With .ParagraphFormat
  94.                     .AutoAdjustRightIndent = False
  95.                     .DisableLineHeightGrid = True
  96.                 End With
  97.                 .Cells.VerticalAlignment = wdCellAlignVerticalCenter
  98.             End With

  99.             '删除空段
  100.             For Each c In .Range.Cells
  101.                 For Each i In c.Range.Paragraphs
  102.                     If Asc(i.Range) = 13 And Len(i.Range) = 1 Then i.Range.Delete
  103.                 Next
  104.                 With c.Range.Paragraphs
  105.                     If .Count > 1 And Len(.Last.Range) = 2 Then .Last.Previous.Range.Characters.Last.Delete
  106.                 End With
  107.             Next

  108.             '表头加粗
  109.             .Cell(1, 1).Select
  110.             With Selection
  111.                 .SelectRow
  112.                 With .Font
  113.                     .NameFarEast = "黑体"
  114.                     .Bold = True
  115. '                    If Not ActiveDocument.Characters.Last.Font.Color = wdColorAutomatic Then
  116.                     .Color = wdColorPink
  117.                 End With
  118.                 .Range.Find.Execute "[  ^s^t]", , , 1, , , , , , "", 2
  119.             End With

  120.             .LeftPadding = CentimetersToPoints(0.19)
  121.             .RightPadding = CentimetersToPoints(0.19)
  122.             .AutoFitBehavior (wdAutoFitContent)
  123.             .Select
  124.             .AutoFitBehavior (wdAutoFitWindow)
  125.         End With
  126.         If a = 1 Then Exit Sub
  127.     Next
  128.     Selection.HomeKey Unit:=wdStory
  129. End Sub

  130. Sub TableDeleteBlankRows()
  131. '表格删除空行
  132.     Dim r As Row
  133.     For Each r In Selection.Tables(1).Rows
  134.         If Len(Replace(Replace(r.Range, vbCr, ""), Chr(7), "")) = 0 Then r.Delete
  135.     Next
  136. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2024-2-22 13:30 | 显示全部楼层
有个对应的文档附件就更方便理解了。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-10-23 18:25 , Processed in 0.038023 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表