ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[原创] (表格)简易编号(三个宏)v1

[复制链接]

TA的精华主题

TA的得分主题

发表于 2015-9-20 14:41 | 显示全部楼层 |阅读模式
* 功能:请执行《简易编号热键》宏,提示热键已经设置,在表格中选定任意一个单元格,按 F3 键:原样复制/F4 键:自动编号(向下编号至当前表格末行,编号格式有两种:纯数字/类似ZD2015037这样的<前缀+数字>。)
  1. Sub 简易编号热键()
  2.     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF3), KeyCategory:=wdKeyCategoryMacro, Command:="原样复制"
  3.     KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyF4), KeyCategory:=wdKeyCategoryMacro, Command:="自动编号"
  4.     MsgBox "F3:原样复制  F4:自动编号", vbOKOnly + vbExclamation, "简易编号热键"
  5. End Sub
  6. Sub 原样复制()
  7.     If Selection.Information(wdWithInTable) = False Then MsgBox "请将光标放在表格中!", vbOKOnly + vbCritical, "原样复制": End

  8.     Dim m As Long, n As Long, h As Long, i As String, c As Cell
  9.     m = Selection.Information(wdStartOfRangeRowNumber)
  10.     n = Selection.Information(wdStartOfRangeColumnNumber)
  11.     h = Selection.Tables(1).Rows.Count
  12.     Selection.SelectCell: Selection.MoveEnd Unit:=wdCharacter, Count:=-1
  13.     If Asc(Selection) = 13 Then End 'Empty
  14.     i = Selection.Text
  15.     ActiveDocument.Range(Start:=Selection.Tables(1).Cell(m + 1, n).Range.Start, End:=Selection.Tables(1).Cell(h, n).Range.End).Select
  16.     For Each c In Selection.Cells
  17.         c.Range.Text = i
  18.     Next
  19.     Selection.Tables(1).Cell(m, n).Select
  20. End Sub
  21. Sub 自动编号()
  22.     If Selection.Information(wdWithInTable) = False Then MsgBox "请将光标放在表格中!", vbOKOnly + vbCritical, "自动编号": End
  23.     Dim m As Long, n As Long, h As Long, i As String, j As String, k As Long, v As Long, c As Cell, s As String
  24.     m = Selection.Information(wdStartOfRangeRowNumber)
  25.     n = Selection.Information(wdStartOfRangeColumnNumber)
  26.     h = Selection.Tables(1).Rows.Count
  27.     Selection.SelectCell: Selection.MoveEnd Unit:=wdCharacter, Count:=-1
  28.     If Asc(Selection) = 13 Then End 'Empty
  29.     j = Selection.Text: k = Len(Selection)
  30.     Selection.MoveStart Unit:=wdCharacter, Count:=k - 1
  31.     If Selection Like "[!0-90-9]" Then Selection.EndKey Unit:=wdLine: End
  32.     Do
  33.         If Len(Selection) = k Then Exit Do
  34.         Selection.MoveStart Unit:=wdCharacter, Count:=-1
  35.     Loop Until Selection.Characters.First Like "[!0-90-9]"
  36.     If Selection.Characters.First Like "[!0-90-9]" Then Selection.MoveStart Unit:=wdCharacter, Count:=1
  37.     Selection.Range.CharacterWidth = wdWidthHalfWidth
  38.     If Len(Selection) = k Then i = j: s = "" Else i = Selection.Text: s = Left(j, Len(j) - Len(i))
  39.     i = i + 1
  40.     ActiveDocument.Range(Start:=Selection.Tables(1).Cell(m + 1, n).Range.Start, End:=Selection.Tables(1).Cell(h, n).Range.End).Select
  41.     For Each c In Selection.Cells
  42.         c.Range.Text = s & i: i = i + 1
  43.     Next
  44.     Selection.Tables(1).Cell(m, n).Select
  45. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2015-9-24 10:08 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2015-9-24 11:16 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2025-1-15 21:55 , Processed in 0.017146 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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