好文档 - 专业文书写作范文服务资料分享网站

网优257个Excel宏常用语句

天下 分享 时间: 加入收藏 我要投稿 点赞

257个Excel宏常用语句

1、打开全部隐藏工作表 ......................................................................................................... 7 2、循环宏 ................................................................................................................................. 7 3、录制宏时调用“停止录制”工具栏 ................................................................................. 7 4、高级筛选5列不重复数据至指定表 ................................................................................. 7 5、双击单元执行宏(工作表代码) .......................................................................................... 8 6、双击指定区域单元执行宏(工作表代码) .......................................................................... 8 7、进入单元执行宏(工作表代码) .......................................................................................... 8 8、进入指定区域单元执行宏(工作表代码) .......................................................................... 9 9、在多个宏中依次循环执行一个(控件按钮代码) ......................................................... 9 10、在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码) ................... 9 11、在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码) ................. 10 12、根据A1单元文本隐藏/显示按钮(控件按钮代码) ................................................. 10 13、当前单元返回按钮名称(控件按钮代码) ................................................................. 10 14、当前单元内容返回到按钮名称(控件按钮代码) ..................................................... 11 15、奇偶页分别打印 ............................................................................................................. 11 16、自动打印多工作表第一页 ............................................................................................. 11 17、查找A列文本循环插入分页符 .................................................................................... 12 18、将A列最后数据行以上的所有B列图片大小调整为所在单元大小 ......................... 12 19、返回光标所在行数 ......................................................................................................... 13 20、在A1返回当前选中单元格数量 .................................................................................. 13 21、返回当前工作簿中工作表数量 ..................................................................................... 13 22、返回光标选择区域的行数和列数 ................................................................................. 13 23、工作表中包含数据的最大行数 ..................................................................................... 13 24、返回A列数据的最大行数 ............................................................................................ 13 25、将所选区域文本插入新建文本框 ................................................................................. 14 26、批量插入地址批注 ......................................................................................................... 14 27、批量插入统一批注 ......................................................................................................... 14 28、以A1单元内容批量插入批注 ...................................................................................... 15 29、不连续区域插入当前文件名和表名及地址 ................................................................. 15 30、不连续区域录入当前单元地址 ..................................................................................... 15 31、连续区域录入当前单元地址 ......................................................................................... 15 32、返回当前单元地址 ......................................................................................................... 16 33、不连续区域录入当前日期 ............................................................................................. 16 34、不连续区域录入当前数字日期 ..................................................................................... 16 35、不连续区域录入当前日期和时间 ................................................................................. 16 36、不连续区域录入对勾 ..................................................................................................... 16 37、不连续区域录入当前文件名 ......................................................................................... 16 38、不连续区域添加文本 ..................................................................................................... 16 39、不连续区域插入文本 ..................................................................................................... 17 40、从指定位置向下同时录入多单元指定内容 ................................................................. 17 41、按aa工作表A列的内容排列工作表标签顺序 ........................................................... 17 42、以A1单元文本作表名插入工作表 .............................................................................. 17

43、删除全部未选定工作表 ................................................................................................. 18 44、工作表标签排序 ............................................................................................................. 18 45、定义指定工作表标签颜色 ............................................................................................. 19 46、在目录表建立本工作簿中各表链接目录 ..................................................................... 19 47、建立工作表文本目录 ..................................................................................................... 20 48、查另一文件的全部表名 ................................................................................................. 20 49、当前单元录入计算机名 ................................................................................................. 20 50、当前单元录入计算机用户名 ......................................................................................... 20 51、解除全部工作表保护 ..................................................................................................... 21 52、为指定工作表加指定密码保护表 ................................................................................. 21 53、在有密码的工作表执行代码 ......................................................................................... 21 54、执行前需要验证密码的宏(控件按钮代码) .................................................................. 21 55、执行前需要验证密码的宏() .......................................................................................... 21 56、拷贝A1公式和格式到A2 ............................................................................................. 22 57、复制单元数值 ................................................................................................................. 22 58、插入数值条件格式 ......................................................................................................... 22 59、插入透明批注 ................................................................................................................. 22 60、添加文本 ......................................................................................................................... 23 61、光标定位到指定工作表A列最后数据行下一单元 ..................................................... 23 62、定位选定单元格式相同的全部单元格 ......................................................................... 23 63、按当前单元文本定位 ..................................................................................................... 24 64、按固定文本定位 ............................................................................................................. 24 65、删除包含固定文本单元的行或列 ................................................................................. 25 66、定位数据及区域以上的空值 ......................................................................................... 25 67、右侧单元自动加5(工作表代码) ................................................................................... 25 68、当前单元加2 .................................................................................................................. 25 69、A列等于A列减B列 ..................................................................................................... 26 70、用于光标选定多区域跳转指定单元(工作表代码) ...................................................... 26 71、将A1单元录入的数据累加到B1单元(工作表代码) ............................................ 26 72、在指定颜色区域选择单元时添加/取消\√\(工作表代码) .................................... 26 73、在指定区域选择单元时添加/取消\√\(工作表代码) ............................................ 27 74、双击指定单元,循环录入文本(工作表代码) ......................................................... 27 75、双击指定单元,循环录入文本(工作表代码) ......................................................... 27 76、单元区域引用(工作表代码) ..................................................................................... 27 77、在指定区域选择单元时数值加1(工作表代码) ...................................................... 28 78、混合文本的编号 ............................................................................................................. 28 79、指定区域单元双击数据累加(工作表代码) ............................................................. 28 80、选择单元区域触发事件(工作表代码) ..................................................................... 28 81、当修改指定单元内容时自动执行宏(工作表代码) ................................................. 28 82、被指定单元内容限制执行宏 ......................................................................................... 29 83、双击单元隐藏该行(工作表代码) ............................................................................. 29 84、高亮显示行(工作表代码) ......................................................................................... 29 85、高亮显示行和列(工作表代码) ................................................................................. 29 86、为指定工作表设置滚动范围(工作簿代码) ............................................................. 29

87、在指定单元记录打印和预览次数(工作簿代码) ..................................................... 29 88、自动数字金额转大写(工作表代码) ......................................................................... 30 89、将全部工作表的A1单元作为单击按钮(工作簿代码) ........................................... 30 90、闹钟——到指定时间执行宏(工作簿代码) ............................................................. 30 91、改变Excel界面标题的宏(工作簿代码) ................................................................... 30 92、在指定工作表的指定单元返回光标当前多选区地址(工作簿代码) ..................... 30 93、B列录入数据时在A列返回记录时间(工作表代码) ............................................. 31 94、当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码) ..... 31 95、指定单元显示光标位置内容(工作表代码) ............................................................. 31 96、每编辑一个单元保存文件 ............................................................................................. 31 97、指定允许编辑区域 ......................................................................................................... 31 98、解除允许编辑区域限制 ................................................................................................. 32 99、删除指定行 ..................................................................................................................... 32 100、删除A列为指定内容的行 .......................................................................................... 32 101、删除A列非数字单元行 .............................................................................................. 32 102、有条件删除当前行 ....................................................................................................... 32 103、选择下一行 ................................................................................................................... 33 104、选择第5行开始所有数据行 ....................................................................................... 33 105、选择光标或选区所在行 ............................................................................................... 33 106、选择光标或选区所在列 ............................................................................................... 33 107、光标定位到名称指定位置 ........................................................................................... 33 108、选择名称定义的数据区 ............................................................................................... 33 109、选择到指定列的最后行 ............................................................................................... 34 110、将Sheet1的A列的非空值写到Sheet2的A列 ........................................................ 34 111、将名称1的数据写到名称2 ........................................................................................ 34 112、单元反选....................................................................................................................... 34 113、调整选中对象中的文字 ............................................................................................... 34 114、去除指定范围内的对象 ............................................................................................... 35 115、更新透视表数据项 ....................................................................................................... 35 116、将全部工作表名称写到A列 ...................................................................................... 35 117、为当前选定的多单元插入指定名称 ........................................................................... 36 118、删除全部名称 ............................................................................................................... 36 119、以指定区域为表目录补充新表 ................................................................................... 36 120、按A列数据批量修改表名称 ...................................................................................... 37 121、按A列数据批量创建新表(控件按钮代码) ................................................................ 37 122、清除剪贴板 ................................................................................................................... 37 123、批量清除软回车 ........................................................................................................... 37 124、判断指定文件是否已经打开 ....................................................................................... 37 125、当前文件另存到指定目录 ........................................................................................... 38 126、另存指定文件名 ........................................................................................................... 38 127、以本工作表名称另存文件到当前目录 ....................................................................... 38 128、将本工作表单独另存文件到Excel当前默认目录 ..................................................... 38 129、以活动工作表名称另存文件到Excel当前默认目录 ................................................. 38 130、另存所有工作表为工作簿 ........................................................................................... 39

131、以指定单元内容为新文件名另存文件 ....................................................................... 39 132、以当前日期为新文件名另存文件 ............................................................................... 39 133、以当前日期和时间为新文件名另存文件 ................................................................... 39 134、另存本表为TXT文件 ................................................................................................... 40 135、引用指定位置单元内容为部分文件名另存文件 ....................................................... 40 136、将A列数据排序到D列 .............................................................................................. 40 137、将指定范围的数据排列到D列 .................................................................................. 40 138、光标所在行上移一行 ................................................................................................... 41 139、加数据有效限制 ........................................................................................................... 41 140、取消数据有效限制 ....................................................................................................... 42 141、重排窗口....................................................................................................................... 42 142、按当前单元文本选择打开指定文件单元 ................................................................... 42 143、回车光标向右 ............................................................................................................... 42 144、回车光标向下 ............................................................................................................... 43 145、保护工作表时取消选定锁定单元 ............................................................................... 43 146、保存并退出Excel ......................................................................................................... 43 147、隐藏/显示指定列空值行 ............................................................................................. 43 148、深度隐藏指定工作表 ................................................................................................... 43 149、隐藏指定工作表 ........................................................................................................... 43 150、隐藏当前工作表 ........................................................................................................... 43 151、返回当前工作表名称 ................................................................................................... 44 152、获取上一次所进入工作簿的工作表名称 ................................................................... 44 153、按光标选定颜色隐藏本列其他颜色行 ....................................................................... 44 154、打开工作簿自动隐藏录入表以外的其他表 ............................................................... 44 155、除最左边工作表外深度隐藏所有表 ........................................................................... 45 156、关闭文件时自动隐藏指定工作表(ThisWorkbook) ..................................................... 45 157、打开文件时提示指定工作表是保护状态(ThisWorkbook) ......................................... 45 158、插入10行 .................................................................................................................... 45 159、全选固定范围内小于0的单元 ................................................................................... 45 160、全选选定范围内小于0的单元 ................................................................................... 46 161、固定区域单元分类变色 ............................................................................................... 46 162、A列半角内容变红 ....................................................................................................... 47 163、单元格录入数据时运行宏的代码 ............................................................................... 47 164、根据B列最后数据快速合并A列单元格的控件代码 .............................................. 47 165、在F1单元显示光标位置批注内容的代码 ................................................................. 47 166、显示光标所在单元的批注的代码 ............................................................................... 48 167、使单元内容保持不变的工作表代码 ........................................................................... 48 168、有条件执行宏 ............................................................................................................... 48 169、有条件执行不同的宏 ................................................................................................... 48 170、提示确定或取消执行宏 ............................................................................................... 49 171、提示开始和结束 ........................................................................................................... 49 172、拷贝指定表不相邻多列数据到新位置 ....................................................................... 49 173、选择2至4行............................................................................................................... 49 174、在当前选区有条件替换数值为文本 ........................................................................... 49

175、自动筛选全部显示指定列 ........................................................................................... 50 176、自动筛选第2列值为A的行 ...................................................................................... 50 177、取消自动筛选() ............................................................................................................ 50 178、全部显示指定表的自动筛选 ....................................................................................... 50 179、强行合并单元 ............................................................................................................... 50 180、设置单元区域格式 ....................................................................................................... 50 181、在所有工作表的A1单元返回顺序号 ........................................................................ 51 182、根据A1单元内容返回C1数值 .................................................................................. 51 183、根据A1内容选择执行宏 ............................................................................................ 51 184、删除A列空行 .............................................................................................................. 51 185、在A列产生不重复随机数 .......................................................................................... 52 186、将A列数据随机排列到F列 ....................................................................................... 52 187、取消选定区域的公式只保留值(假空转真空) ............................................................ 52 188、处理导入的显示为科学计数法样式的身份证号 ....................................................... 53 189、返回指定单元的行高和列宽 ....................................................................................... 53 190、指定行高和列宽 ........................................................................................................... 53 191、指定单元的行高和列宽与A1单元相同 .................................................................... 53 191、填公式........................................................................................................................... 53 192、建立当前工作表的副本为001表 ............................................................................... 54 193、在第一个表前插入多工作表 ....................................................................................... 54 194、清除A列再插入序号 .................................................................................................. 54 195、反方向文本(自定义函数) ............................................................................................ 54 196、指定选择单元区域弹出消息 ....................................................................................... 54 197、将B列数据添加超链接到K列 .................................................................................. 55 198、删除B列数据的超链接 .............................................................................................. 55 199、分离临时表A列数据的文本和超链接并整理到数据库表 ....................................... 55 200、分离临时表A列数据的文本和超链接并会同其他数据整理到数据库表 ............... 55 201、返回A列最后一个非空单元行号 .............................................................................. 56 202、返回表中第一个非空单元地址(行搜索) .................................................................... 56 203、返回表中各非空单元区域地址(行搜索) .................................................................... 56 204、返回第一个数值行号 ................................................................................................... 56 205、返回第1行最右边非空单元的列号 ........................................................................... 57 206、返回连续数值单元的数量 ........................................................................................... 57 207、统计指定范围和内容的单元数量 ............................................................................... 57 208、统计不同颜色的数字的和(自定义函数) ............................................................... 57 209、返回非空单元数量 ....................................................................................................... 57 210、返回A列非空单元数量 .............................................................................................. 58 211、返回圆周率π ............................................................................................................... 58 212、定义指定单元内容为页眉/页脚 ................................................................................. 58 213、提示并全部清除当前选择区域 ................................................................................... 58 214、全部清除当前选择区域 ............................................................................................... 58 215、清除指定区域数值 ....................................................................................................... 58 216、对指定工作表执行取消隐藏》打印》隐藏工作表 ................................................... 59 217、打开文件时执行指定宏(工作簿代码) ........................................................................ 59

218、关闭文件时执行指定宏(工作簿代码) ........................................................................ 59 219、弹出提示A1单元内容 ................................................................................................ 59 220、延时15秒执行重排窗口宏 ......................................................................................... 59 221、撤消工作表保护并取消密码 ....................................................................................... 60 222、重算指定表 ................................................................................................................... 60 223、将第5行移到窗口的最上面 ....................................................................................... 60 224、对第一张工作表的指定区域进行排序 ....................................................................... 60 225、显示指定工作表的打印预览 ....................................................................................... 60 226、用单元格A1的内容作为文件名另存当前工作簿..................................................... 60 227、[禁用/启用]保存和另存的代码 ................................................................................... 60 228、在A和B列返回当前选区的名称和公式 .................................................................. 61 229、朗读朗读A列,按ESC键中止...................................................................................... 61 230、朗读固定语句,请按ESC键终止 ............................................................................... 61 231、在M和N列的14行以下选择单元时显示调用日历控件(工作表代码) ........... 62 232、添加自定义序列 ........................................................................................................... 62 233、弹出打印对话框 ........................................................................................................... 62 234、返回总页码 ................................................................................................................... 62 235、合并各工作表内容 ....................................................................................................... 63 236、合并指定目录中所有文件中相同格式工作表的数据 ............................................... 64 237、隐藏指定工作表的指定列 ........................................................................................... 64 238、把a列不重复值取到e列 ........................................................................................... 65 239、当前选区的行列数 ....................................................................................................... 65 240、单元格录入1位字符就跳转(工作表代码) ................................................................ 65 241、当指定日期(每月10日)打开文件执行宏 ............................................................. 65 242、提示并清空单元区域 ................................................................................................... 66 243、返回光标所在行号 ....................................................................................................... 66 244、按照当前行A列的图片名称插入图片到H列 .......................................................... 66 245、当前行下插入1行 ....................................................................................................... 66 246、取消指定行或列的隐藏 ............................................................................................... 67 247、复制单元格所在行 ....................................................................................................... 67 248、复制单元格所在列 ....................................................................................................... 67 249、新建一个工作表 ........................................................................................................... 67 250、新建一个工作簿 ........................................................................................................... 67 251、选择多表为工作组 ....................................................................................................... 67 252、在当前工作组各表中分别执行指定宏 ....................................................................... 68 253、复制当前工作簿的报表到临时工作簿 ....................................................................... 69 254、删除指定文件 ............................................................................................................... 70 255、合并A1至C1的内容写到D15单元的批注中 .......................................................... 70 256、自动重算....................................................................................................................... 70 257、手动重算....................................................................................................................... 70

1、打开全部隐藏工作表

Sub 打开全部隐藏工作表() Dim i As Integer

For i = 1 To Sheets.Count Sheets(i).Visible = True Next i End Sub

2、循环宏

Sub 循环()

AAA = Range(\ Dim i As Long

Dim times As Long times = AAA

'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To times Call 过滤一行

If Range(\完成标志\完成\ Exit For

'如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则只执行一次循环就退出

'If Sheets(\传送参数\完成\ '如果某列出现\完成\内容则退出循环 Next i End Sub

3、录制宏时调用“停止录制”工具栏

Sub 录制宏时调用停止录制工具栏()

Application.CommandBars(\End Sub

4、高级筛选5列不重复数据至指定表

Sub 高级筛选5列不重复数据至Sheet2()

Sheets(\清除Sheet2的A:D列

Range(\ \

Sheet2.Columns(\Header:=xlGuess, _

OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _

:=xlPinYin End Sub

5、双击单元执行宏(工作表代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range(\关闭\ Exit Sub

Select Case Target.Address Case \ Call 宏1 Cancel = True Case \ Call 宏2 Cancel = True Case \ Call 宏3 Cancel = True Case \ Call 宏4 Cancel = True End Select End Sub

6、双击指定区域单元执行宏(工作表代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Range(\关闭\

If Not Application.Intersect(Target, Range(\打开隐藏表

End Sub

7、进入单元执行宏(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) '以单元格进入代替按钮对象调用宏

If Range(\关闭\ Select Case Target.Address

Case \单元地址(Target.Address),或命名单元名字(Target.Name) Call 宏1 Case \ Call 宏2 Case \

Call 宏3 End Select End Sub

8、进入指定区域单元执行宏(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Range(\关闭\

If Not Application.Intersect(Target, Range(\打开隐藏表

End Sub

9、在多个宏中依次循环执行一个(控件按钮代码)

Private Sub CommandButton1_Click() Static RunMacro As Integer Select Case RunMacro Case 0 宏1

RunMacro = 1 Case 1 宏2

RunMacro = 2 Case 2 宏3

RunMacro = 0 End Select End Sub

10、在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)

Private Sub CommandButton1_Click() With CommandButton1

If .Caption = \保护工作表\ Call 保护工作表

.Caption = \取消工作表保护\ Exit Sub End If

If .Caption = \取消工作表保护\ Call 取消工作表保护 .Caption = \保护工作表\ Exit Sub End If

End With End Sub

11、在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)

Option Explicit

Private Sub CommandButton1_Click() With CommandButton1

If .Caption = \宏1\ Call 宏1

.Caption = \宏2\ Exit Sub End If

If .Caption = \宏2\ Call 宏2

.Caption = \宏3\ Exit Sub End If

If .Caption = \宏3\ Call 宏3

.Caption = \宏1\ Exit Sub End If End With End Sub

12、根据A1单元文本隐藏/显示按钮(控件按钮代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range(\

CommandButton1.Visible = 1 Else

CommandButton1.Visible = 0 End If End Sub

Private Sub CommandButton1_Click() 重排窗口 End Sub

13、当前单元返回按钮名称(控件按钮代码)

Private Sub CommandButton1_Click() ActiveCell = CommandButton1.Caption

End Sub

14、当前单元内容返回到按钮名称(控件按钮代码)

Private Sub CommandButton1_Click() CommandButton1.Caption = ActiveCell End Sub

15、奇偶页分别打印

Sub 奇偶页分别打印() Dim i%, Ps%

Ps = ExecuteExcel4Macro(\总页数 MsgBox \现在打印奇数页,按确定开始.\For i = 1 To Ps Step 2

ActiveSheet.PrintOut from:=i, To:=i Next i

MsgBox \现在打印偶数页,按确定开始.\For i = 2 To Ps Step 2

ActiveSheet.PrintOut from:=i, To:=i Next i End Sub

16、自动打印多工作表第一页

Sub 自动打印多工作表第一页() Dim sh As Integer Dim x Dim y Dim sy Dim syz

x = InputBox(\请输入起始工作表名字:\sy = InputBox(\请输入结束工作表名字:\y = Sheets(x).Index syz = Sheets(sy).Index For sh = y To syz Sheets(sh).Select

Sheets(sh).PrintOut from:=1, To:=1 Next sh End Sub

17、查找A列文本循环插入分页符

Sub 循环插入分页符()

' Selection = Workbooks(\临时表\表2\调用指定地址内容

Dim i As Long

Dim times As Long

times = Application.WorksheetFunction.CountIf(Sheet1.Range(\分页\

'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To times Call 插入分页符 Next i End Sub

Sub 插入分页符()

Cells.Find(What:=\分页\

xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .Activate

ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell End Sub

Sub 取消原分页() Cells.Select

ActiveSheet.ResetAllPageBreaks End Sub

18、将A列最后数据行以上的所有B列图片大小调整为所在单元大小

Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小() Dim Pic As Picture, i&

i = [A65536].End(xlUp).Row For Each Pic In Sheet1.Pictures

If Not Application.Intersect(Pic.TopLeftCell, Range(\ Pic.Top = Pic.TopLeftCell.Top Pic.Left = Pic.TopLeftCell.Left

Pic.Height = Pic.TopLeftCell.Height Pic.Width = Pic.TopLeftCell.Width End If Next End Sub

19、返回光标所在行数

Sub 返回光标所在行数() x = ActiveCell.Row Range(\End Sub

20、在A1返回当前选中单元格数量

Sub 在A1返回当前选中单元格数量() [A1] = Selection.Count End Sub

21、返回当前工作簿中工作表数量

Sub 返回当前工作簿中工作表数量() t = Application.Sheets.Count MsgBox t End Sub

22、返回光标选择区域的行数和列数

Sub 返回光标选择区域的行数和列数() x = Selection.Rows.Count y = Selection.Columns.Count Range(\ Range(\End Sub

23、工作表中包含数据的最大行数

Sub 包含数据的最大行数()

n = Cells.Find(\ MsgBox n End Sub

24、返回A列数据的最大行数

Sub 返回A列数据的最大行数()

n = Range(\ Range(\End Sub

25、将所选区域文本插入新建文本框

Sub 将所选区域文本插入新建文本框() For Each rag In Selection

n = n & rag.Value & Chr(10) Next

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).Select Selection.Characters.Text = \问题:\

With Selection.Characters(Start:=1, Length:=3).Font .Name = \黑体\ .FontStyle = \常规\ .Size = 12 End With End Sub

ActiveCell.Left +

26、批量插入地址批注

Sub 批量插入地址批注() On Error Resume Next Dim r As Range

If Selection.Cells.Count > 0 Then For Each r In Selection r.Comment.Delete r.AddComment

r.Comment.Visible = False

r.Comment.Text Text:=\本单元格:\ Next End If End Sub

27、批量插入统一批注

Sub 批量插入统一批注()

Dim r As Range, msg As String

msg = InputBox(\请输入欲批量插入的批注\提示\随便输点什么吧\ If Selection.Cells.Count > 0 Then For Each r In Selection r.AddComment

r.Comment.Visible = False r.Comment.Text Text:=msg Next End If

End Sub

28、以A1单元内容批量插入批注

Sub 以A1单元内容批量插入批注() Dim r As Range

If Selection.Cells.Count > 0 Then For Each r In Selection r.AddComment

r.Comment.Visible = False

r.Comment.Text Text:=[a1].Text Next End If End Sub

29、不连续区域插入当前文件名和表名及地址

Sub 批量插入当前文件名和表名及地址() For Each mycell In Selection

mycell.FormulaR1C1 = \+ ActiveWorkbook.Name + \+ ActiveSheet.Name + \+ mycell.Address Next End Sub

30、不连续区域录入当前单元地址

Sub 区域录入当前单元地址() For Each mycell In Selection

mycell.FormulaR1C1 = mycell.Address Next End Sub

31、连续区域录入当前单元地址

Sub 连续区域录入当前单元地址()

Selection = \ Selection.Copy

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End Sub

32、返回当前单元地址

Sub 返回当前单元地址() d = ActiveCell.Address [A1] = d End Sub

33、不连续区域录入当前日期

Sub 区域录入当前日期()

Selection.FormulaR1C1 = Format(Now(), \End Sub

34、不连续区域录入当前数字日期

Sub 区域录入当前数字日期()

Selection.FormulaR1C1 = Format(Now(), \End Sub

35、不连续区域录入当前日期和时间

Sub 区域录入当前日期和时间()

Selection.FormulaR1C1 = Format(Now(), \End Sub

36、不连续区域录入对勾

Sub 批量录入对勾()

Selection.FormulaR1C1 = \√\End Sub

37、不连续区域录入当前文件名

Sub 批量录入当前文件名()

Selection.FormulaR1C1 = ThisWorkbook.Name End Sub

38、不连续区域添加文本

Sub 批量添加文本()

Dim s As Range

For Each s In Selection s = s & \文本内容\ Next End Sub

39、不连续区域插入文本

Sub 批量插入文本() Dim s As Range

For Each s In Selection s = \文本内容\ Next End Sub

40、从指定位置向下同时录入多单元指定内容

Sub 从指定位置向下同时录入多单元指定内容() Dim arr

arr = Array(\

[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub

41、按aa工作表A列的内容排列工作表标签顺序

Sub 按aa工作表A列的内容排列工作表标签顺序() Dim I%, str1$ I = 1

Sheets(\

Do While Cells(I, 1).Value <> \ str1 = Trim(Cells(I, 1).Value) Sheets(str1).Select

Sheets(str1).Move after:=Sheets(I) I = I + 1

Sheets(\ Loop End Sub

42、以A1单元文本作表名插入工作表

Sub 以A1单元文本作表名插入工作表() Dim nm As String nm = [a1]

Sheets.Add

ActiveSheet.Name = nm End Sub

43、删除全部未选定工作表

Sub 删除全部未选定工作表()

Dim sht As Worksheet, n As Integer, iFlag As Boolean Dim ShtName() As String

n = ActiveWindow.SelectedSheets.Count ReDim ShtName(1 To n) n = 1

For Each sht In ActiveWindow.SelectedSheets ShtName(n) = sht.Name n = n + 1 Next

Application.DisplayAlerts = False For Each sht In Sheets iFlag = False For i = 1 To n - 1

If ShtName(i) = sht.Name Then iFlag = True Exit For End If Next

If Not iFlag Then sht.Delete Next

Application.DisplayAlerts = True End Sub

44、工作表标签排序

Sub 工作表标签排序()

Dim i As Long, j As Long, nums As Long, msg As Long

msg = MsgBox(\工作表按升序排列请选 '是[Y]'. \工作表按降序排列请选 '否[N]'\工作表排序\ If msg = vbCancel Then Exit Sub nums = Sheets.Count

If msg = vbYes Then 'Sort ascending For i = 1 To nums For j = i To nums

If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i) End If

Next j Next i

Else 'Sort descending For i = 1 To nums For j = i To nums

If UCase(Sheets(j).Name) > UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i) End If Next j Next i End If End Sub

45、定义指定工作表标签颜色

Sub 定义指定工作表标签颜色()

Sheets(\End Sub

46、在目录表建立本工作簿中各表链接目录

Sub 在目录表建立本工作簿中各表链接目录() Dim s%, Rng As Range On Error Resume Next Sheets(\目录\ If Err = 0 Then

Sheets(\目录\ Else

Sheets.Add

ActiveSheet.Name = \目录\ End If

For i = 1 To Sheets.Count

If Sheets(i).Name <> \目录\ s = s + 1

Set Rng = Sheets(\目录\ Rng = Format(s, \

ActiveSheet.Hyperlinks.Add Rng, \& Sheets(i).Name & ScreenTip:=Sheets(i).Name End If Next

Sheets(\目录\End Sub

\

47、建立工作表文本目录

Sub 建立工作表文本目录()

Sheets.Add before:=Sheets(1) Sheets(1).Name = \目录\ For i = 2 To Sheets.Count

Cells(i - 1, 1) = Sheets(i).Name

'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), \ '添加超链接 Next End Sub

48、查另一文件的全部表名

Sub 查另一文件的全部表名() On Error Resume Next Dim i%

Dim sh As Worksheet

Application.ScreenUpdating = False

Workbooks.Open Filename:=ThisWorkbook.Path & \ Windows(\当前文件名称 Sheets(\ '当前表名称

i = 1 '将表名称返回到第1行 For Each sh In Workbooks(\

Cells(i, 1) = sh.Name '将表名称返回到第1列 i = i + 1 '返回每个表名称向下移动1行 Next sh

Windows(\ '关闭对象文件 Application.ScreenUpdating = True End Sub

49、当前单元录入计算机名

Sub 当前单元录入计算机名()

Selection = Environ(\

'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub

50、当前单元录入计算机用户名

Sub 当前单元录入计算机用户名() Selection = Environ(\

'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub

51、解除全部工作表保护

Sub 解除全部工作表保护() Dim n As Integer

For n = 1 To Sheets.Count Sheets(n).Unprotect Next n End Sub

52、为指定工作表加指定密码保护表

Sub 为指定工作表加指定密码保护表() Sheet10.Protect Password:=\End Sub

53、在有密码的工作表执行代码

Sub 在有密码的工作表执行代码()

Sheets(\假定表名为“1”,密码为“123”打开工作表 Range(\ '隐藏C列空值行 Sheets(\ '重新用密码保护工作表 End Sub

54、执行前需要验证密码的宏(控件按钮代码)

Private Sub CommandButton1_Click()

If InputBox(\请输入密码:\密码是123 MsgBox \密码错误,按确定退出!\提示\ Exit Sub End If

Cells(1, 1) = 10 End Sub

55、执行前需要验证密码的宏()

Sub 执行前需要验证密码的宏()

If InputBox(\请输入您的使用权限:\系统提示\重排窗口 '要执行的宏代码或宏名称 Else

MsgBox \对不起,您没有使用该宏的权限,按确定键后退出!\ End If End Sub

56、拷贝A1公式和格式到A2

Sub 拷贝A1公式到A2()

Workbooks(\临时表\表1\

Workbooks(\临时表\表2\End Sub

57、复制单元数值

Sub 复制数值()

s = Workbooks(\ Workbooks(\End Sub

58、插入数值条件格式

Sub 插入数值条件格式()

Selection.FormatConditions.Delete

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:=\

Selection.FormatConditions(1).Interior.ColorIndex = 45

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:=\

Selection.FormatConditions(2).Interior.ColorIndex = 39

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:=\

Selection.FormatConditions(3).Interior.ColorIndex = 34 End Sub

59、插入透明批注

Sub 插入透明批注()

Selection.AddComment

Selection.Comment.Visible = False Dim XS As Worksheet

For i = 1 To ActiveSheet.Comments.Count

ActiveSheet.Comments(i).Text \透明批注\

ActiveSheet.Comments(i).Shape.Fill.Visible = msoFalse Next End Sub

60、添加文本

Sub 添加文本()

Selection = Selection + \×\不可在数字后添加文本

'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub

61、光标定位到指定工作表A列最后数据行下一单元

Sub 光标定位到指定工作表A列最后数据行下一单元() a = Sheets(\数据库\ Sheets(\数据库\ Range(\End Sub

62、定位选定单元格式相同的全部单元格

Sub 定位选定单元格式相同的全部单元格() Dim FirstCell As Range, FoundCell As Range Dim AllCells As Range

With Application.FindFormat .Clear

.NumberFormatLocal = Selection.NumberFormatLocal .HorizontalAlignment = Selection.HorizontalAlignment .VerticalAlignment = Selection.VerticalAlignment .WrapText = Selection.WrapText .Orientation = Selection.Orientation .AddIndent = Selection.AddIndent .IndentLevel = Selection.IndentLevel .ShrinkToFit = Selection.ShrinkToFit .MergeCells = Selection.MergeCells .Font.Name = Selection.Font.Name

.Font.FontStyle = Selection.Font.FontStyle .Font.Size = Selection.Font.Size

.Font.Strikethrough = Selection.Font.Strikethrough .Font.Subscript = Selection.Font.Subscript .Font.Underline = Selection.Font.Underline .Font.ColorIndex = Selection.Font.ColorIndex

.Interior.ColorIndex = Selection.Interior.ColorIndex .Interior.Pattern = Selection.Interior.Pattern .Locked = Selection.Locked

.FormulaHidden = Selection.FormulaHidden End With

Set FirstCell = ActiveSheet.UsedRange.Find(what:=\ If FirstCell Is Nothing Then Exit Sub End If

Set AllCells = FirstCell

Set FoundCell = FirstCell Do

Set FoundCell = ActiveSheet.UsedRange.Find(After:=FoundCell, what:=\searchformat:=True)

If FoundCell Is Nothing Then Exit Do Set AllCells = Union(FoundCell, AllCells)

If FoundCell.Address = FirstCell.Address Then Exit Do Loop AllCells.Select End Sub

63、按当前单元文本定位

Sub 按当前单元文本定位() ABC = Selection Dim aa As Range

For Each a In ActiveSheet.UsedRange If a Like ABC Then

If aa Is Nothing Then Set aa = a.Cells Else

Set aa = Union(aa, a.Cells) End If End If Next aa.Select End Sub

64、按固定文本定位

Sub 文本定位()

Dim aa As Range

For Each a In ActiveSheet.UsedRange If a Like \合计*\ If aa Is Nothing Then Set aa = a.Cells Else

Set aa = Union(aa, a.Cells) End If

End If Next aa.Select End Sub

65、删除包含固定文本单元的行或列

Sub 删除包含固定文本单元的行或列() Do

Cells.Find(what:=\哈哈\

Selection.EntireRow.Delete '删除行 ' Selection.EntireColumn.Delete '删除列

Loop Until Cells.Find(what:=\哈哈\End Sub

66、定位数据及区域以上的空值

Sub 定位数据及区域以上的空值() Dim aa As Range

For Each a In ActiveSheet.UsedRange If a Like 〈0 Then If aa Is Nothing Then Set aa = a.Cells Else

Set aa = Union(aa, a.Cells) End If End If Next aa.Select End Sub

67、右侧单元自动加5(工作表代码)

Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False Target.Offset(0, 1) = Target + 5 Application.EnableEvents = True End Sub

68、当前单元加2

Sub 当前单元加2()

Selection = Selection + 2

'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub

69、A列等于A列减B列

Sub A列等于A列减B列() For i = 1 To 23

Cells(i, 1) = Cells(i, 1) - Cells(i, 2) Next End Sub

70、用于光标选定多区域跳转指定单元(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal T As Range) a = Array([b6:b7], [e6], [h6]) For i = 0 To 2

If Not Application.Intersect(T, a(i)) Is Nothing Then [a1].Select: Exit For End If Next End Sub

71、将A1单元录入的数据累加到B1单元(工作表代码)

Private Sub Worksheet_Change(ByVal Target As Range) Dim t As Long

If Target.Address = \t = Sheet1.Range(\

Sheet1.Range(\End If End Sub

72、在指定颜色区域选择单元时添加/取消\√\(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim myrg As Range For Each myrg In Target

If myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> \√\√\ Next End Sub

73、在指定区域选择单元时添加/取消\√\(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim Rng As Range

If Target.Count <= 15 Then

If Not Application.Intersect(Target, Range(\ For Each Rng In Selection With Rng

If .Value = \ .Value = \√\ Else

.Value = \ End If End With Next End If End If End Sub

74、双击指定单元,循环录入文本(工作表代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean) If T.Address <> \Cancel = True

T = IIf(T = \好\中\中\差\好\End Sub

75、双击指定单元,循环录入文本(工作表代码)

Dim nums As Byte

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = \nums = nums Mod 3 + 1

Target = Mid(\上中下\Target.Offset(1, 0).Select End If End Sub

76、单元区域引用(工作表代码)

Private Sub Worksheet_Activate()

Sheet1.Range(\End Sub

77、在指定区域选择单元时数值加1(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Application.Intersect([a1:e10], Target) Is Nothing Then Target = Val(Target) + 1 End If End Sub

78、混合文本的编号

Sub 混合文本的编号()

Worksheets(1).Range(\北京\End Sub

79、指定区域单元双击数据累加(工作表代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not Application.Intersect([A1:Y100], Target) Is Nothing Then oldvalue = Val(Target.Value)

inputvalue = InputBox(\请输入数量,按ENTER键确认!\数值累加器\Target.Value = oldvalue + inputvalue End If End Sub

80、选择单元区域触发事件(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Address = \ MsgBox \你选择了$A$1:$B$2单元\End If End Sub

81、当修改指定单元内容时自动执行宏(工作表代码)

Private Sub Worksheet_Change(ByVal Target As Range) If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then 重排窗口 End If End Sub

82、被指定单元内容限制执行宏

Sub 被指定单元限制执行宏()

If Range(\关闭\窗口 End Sub

83、双击单元隐藏该行(工作表代码)

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Rows(Target.Row).Hidden = True End Sub

84、高亮显示行(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Cells.Interior.ColorIndex = 2

Rows(\ '保持1至2行的颜色推荐39,22,40,

Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15 End Sub

85、高亮显示行和列(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Cells.Interior.ColorIndex = xlNone

Rows(Target.Row).Interior.ColorIndex = 34

Columns(Target.Column).Interior.ColorIndex = 34 End Sub

86、为指定工作表设置滚动范围(工作簿代码)

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Sheet1.ScrollArea = \End Sub

87、在指定单元记录打印和预览次数(工作簿代码)

Private Sub Workbook_BeforePrint(Cancel As Boolean) Range(\End Sub

88、自动数字金额转大写(工作表代码)

Private Sub Worksheet_Change(ByVal M As Range) On Error Resume Next

y = Int(Round(100 * Abs(M)) / 100)

j = Round(100 * Abs(M) + 0.00001) - y * 100 f = (j / 10 - Int(j / 10)) * 10

A = IIf(y < 1, \元\

b = IIf(j > 9.5, Application.Text(Int(j / 10), \角\零\ c = IIf(f < 1, \整\分\ M = IIf(Abs(M) < 0.005, \负\End Sub

89、将全部工作表的A1单元作为单击按钮(工作簿代码)

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Target.Address = \ Call 宏名 End If End Sub

90、闹钟——到指定时间执行宏(工作簿代码)

Private Sub Workbook_Open()

Application.OnTime (\提示1\ '宏名字 Application.OnTime (\提示2\ '宏名字 End Sub

91、改变Excel界面标题的宏(工作簿代码)

Private Sub Workbook_Open() Application.Caption = \春节快乐\End Sub

92、在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets(\表2\End Sub

93、B列录入数据时在A列返回记录时间(工作表代码)

Public Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 Then Target.Offset(, -1) = Now End If End Sub

94、当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)

Public Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Target, [A1:A1000]) Is Nothing Then If Target.Column = 1 Then Target.Offset(, 1) = Date Target.Offset(, 2) = Time End If End If End Sub

Public Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Target, [A1:A1000]) Is Nothing Then If Target.Column = 1 Then

Target.Offset(, 1) = Format(Now(), \Target.Offset(, 2) = Format(Now(), \End If End If End Sub

95、指定单元显示光标位置内容(工作表代码)

Private Sub Worksheet_SelectionChange(ByVal T As Range) Sheets(1).Range(\End Sub

96、每编辑一个单元保存文件

Private Sub Worksheet_Change(ByVal Target As Range) ThisWorkbook.Save End Sub

97、指定允许编辑区域

Sub 指定允许编辑区域()

ActiveSheet.ScrollArea = \End Sub

98、解除允许编辑区域限制

Sub 解除允许编辑区域限制() ActiveSheet.ScrollArea = \End Sub

99、删除指定行

Sub 删除指定行()

Workbooks(\临时表\表2\End Sub

100、删除A列为指定内容的行

Sub 删除A列为指定内容的行() Dim a, b As Integer

a = Sheet1.[a65536].End(xlUp).Row For b = a To 2 Step -1

If Cells(b, 1).Value = \删除\ Rows(b).Delete End If Next End Sub

101、删除A列非数字单元行

Sub 删除A列非数字单元行() i = [a65536].End(xlUp).Row

Range(\End Sub

102、有条件删除当前行

Sub 有条件删除当前行()

If [A1] = 2 Or [B1] = \删除\Selection.Delete Shift:=xlUp End If End Sub

103、选择下一行

Sub 选择下一行()

ActiveCell.Offset(1, 0).Rows(\End Sub

104、选择第5行开始所有数据行

Sub 选择第5行开始所有数据行A() Dim i% i = Cells.Find(\SearchOrder:=xlByRows, SearchDirection:=xlPrevious).EntireRow.Row Rows(\End Sub

Sub 选择第5行开始所有数据行B()

Rows(\End Sub

105、选择光标或选区所在行

Sub 选择光标或选区所在行() Selection.EntireRow.Select End Sub

106、选择光标或选区所在列

Sub 选择光标或选区所在列() Selection.EntireColumn.Select End Sub

107、光标定位到名称指定位置

Sub 定位()

Application.Goto Range(Evaluate(\名称\End Sub

108、选择名称定义的数据区

Sub 选择名称定义的数据区()

[数据区].Select '插入名称要使用INDIRECT函数 'Range(\数据区\ 或者

LookIn:=xlValues,

'Sheet1.Range(\数据区\或者 End Sub

109、选择到指定列的最后行

Sub 选择到指定列的最后行()

Range(\End Sub

110、将Sheet1的A列的非空值写到Sheet2的A列

Sub 将Sheet1的A列的非空值写到Sheet2的A列()

Sheet1.Columns(\End Sub

111、将名称1的数据写到名称2

Sub Macro2()

Range(\位置2\位置1\End Sub

112、单元反选

Sub 单元反选()

Application.DisplayAlerts = False Application.ScreenUpdating = False

Dim raddress As String, taddress As String raddress = Selection.Address

taddress = ActiveSheet.UsedRange.Address With Sheets.Add .Range(taddress) = 0 .Range(raddress) = \

raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address .Delete End With

ActiveSheet.Range(raddress).Select Application.ScreenUpdating = True End Sub

113、调整选中对象中的文字

Sub 调整选中对象中的文字()

'文字居中、自动调整大小 With Selection

.HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .ReadingOrder = xlContext .Orientation = xlHorizontal .AutoSize = True .AddIndent = False End With End Sub

114、去除指定范围内的对象

Sub 去除指定范围内的对象() Dim p As Shape

Set My = Worksheets(\工作表名\ For Each p In My.Shapes

If Not Application.Intersect(p.TopLeftCell, Range(\范围\ Next End Sub

115、更新透视表数据项

Sub DeleteMissingItems2002All()

'防止数据透视表中显示无用的数据项 '在 Excel 2002 或更高版本中 '如果无用的数据项已经存在, '运行这个宏可以更新 Dim pt As PivotTable Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets For Each pt In ws.PivotTables

pt.PivotCache.MissingItemsLimit = xlMissingItemsNone Next pt Next ws End Sub

116、将全部工作表名称写到A列

Sub 将全部表名称写到A列() k = 1

For Each Sht In Sheets

Cells(k + 1, 1) = Sht.Name '指定写入的行和列

k = k + 1 Next End Sub

117、为当前选定的多单元插入指定名称

Sub 为当前选定的多单元插入指定名称() Selection.Name = \临时\

ActiveWorkbook.Names.Add Name:=\临时\ '或者换用这行代码也可以 End Sub

118、删除全部名称

Sub 删除全部名称() On Error Resume Next Dim l As Integer

l = ActiveWorkbook.Names.Count For i = l To 1 Step -1

ActiveWorkbook.Names(i).Delete Next End Sub

119、以指定区域为表目录补充新表

Sub 以指定区域为表目录补充新表() Dim dic As Object, sh As Worksheet Dim arr, item

arr = Range(\

Set dic = CreateObject(\ For Each sh In ThisWorkbook.Worksheets dic.Add sh.Name, \ Next

For Each item In arr

If item <> \ With ThisWorkbook.Worksheets.Add .Name = item End With End If Next

Set dic = Nothing End Sub

120、按A列数据批量修改表名称

Sub 按A列数据批量修改表名称() Dim i%

For i = 1 To Sheets.Count - 1

Sheets(i).Name = Cells(i + 1, 1).Text Next End Sub

121、按A列数据批量创建新表(控件按钮代码)

Private Sub CommandButton1_Click() On Error Resume Next Dim i%, j%

For i = 1 To [a65536].End(xlUp).Row For j = 2 To Sheets.Count

If Cells(i, 1) = Sheets(j).Name Then Exit For End If Next

Sheets.Add(after:=Sheets(Sheets.Count)).Name = Cells(i, 1) Next End Sub

122、清除剪贴板

Sub 清除剪贴板()

Application.CutCopyMode = False

Application.CommandBars(\End Sub

123、批量清除软回车

Sub 批量清除软回车()

'也可直接使用Alt+10或13替换

Cells.Replace What:=Chr(10), Replacement:=\ xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False End Sub

124、判断指定文件是否已经打开

Sub 判断指定文件是否已经打开()

Dim x As Integer

For x = 1 To Workbooks.Count

If Workbooks(x).Name = \函数.xls\ '文件名称 MsgBox \文件已打开\ Exit Sub End If Next

MsgBox \文件未打开\End Sub

125、当前文件另存到指定目录

Sub 当前激活文件另存到指定目录()

ActiveWorkbook.SaveAs Filename:=\信件\\\End Sub

126、另存指定文件名

Sub 另存指定文件名()

ActiveWorkbook.SaveAs ThisWorkbook.Path & \别名.xls\End Sub

127、以本工作表名称另存文件到当前目录

Sub 以本工作表名称另存文件到当前目录()

ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & \End Sub

128、将本工作表单独另存文件到Excel当前默认目录

Sub 将本工作表单独另存文件到Excel当前默认目录() ActiveSheet.Copy

ActiveWorkbook.SaveAs Filename:=ActiveSheet.Name & \End Sub

129、以活动工作表名称另存文件到Excel当前默认目录

Sub 以活动工作表名称另存文件到Excel当前默认目录()

ActiveWorkbook.SaveAs Filename:=ActiveSheet.Name & \

xlNormal, Password:=\ , CreateBackup:=False End Sub

130、另存所有工作表为工作簿

Sub 另存所有工作表为工作簿() Dim sht As Worksheet

Application.ScreenUpdating = False ipath = ThisWorkbook.Path & \For Each sht In Sheets sht.Copy

ActiveWorkbook.SaveAs ipath & sht.Name & \工作表名称为文件名)

'ActiveWorkbook.SaveAs ipath & sht.Name & Trim(sht.[d15]) & \(文件名称& D15单元内容)

'ActiveWorkbook.SaveAs ipath & Trim(sht.[d15]) & \ '(文件名称为D15单元内容) ActiveWorkbook.Close Next

Application.ScreenUpdating = True End Sub

131、以指定单元内容为新文件名另存文件

Sub 以指定单元内容为新文件名另存文件()

ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & \End Sub

132、以当前日期为新文件名另存文件

Sub 以当前日期为新文件名另存文件()

ThisWorkbook.SaveAs ThisWorkbook.Path & \End Sub

Sub 以当前日期为名称另存文件()

ActiveWorkbook.SaveAs Filename:=Date & \End Sub

133、以当前日期和时间为新文件名另存文件

Sub 以当前日期和时间为新文件名另存文件()

ThisWorkbook.SaveAs ThisWorkbook.Path & \年\月\& \日\时\分\秒\End Sub

134、另存本表为TXT文件

Sub 另存本表为TXT文件() Dim s As String

Dim FullName As String, rng As Range Application.ScreenUpdating = False

FullName = (ActiveSheet.Name & \ '以当前表名为TXT文件名

' FullName = Replace(ThisWorkbook.FullName, \以当前文件名为TXT文件名 ' FullName = Replace(ThisWorkbook.FullName, \以文件名&表名为TXT文件名

Open FullName For Output As #1 '以读写方式打开文件,每次写内容都会覆盖原先的内容

'参考帮助,fullname为文件全名

For Each rng In Range(\ s = s & IIf(s = \

If rng.Column = Range(\ Print #1, s & \ '把数据写到文本文件里 s = \ End If Next

Close #1 '关闭文件

Application.ScreenUpdating = True MsgBox \数据已导入文本\End Sub

135、引用指定位置单元内容为部分文件名另存文件

Sub 引用指定位置单元内容为部分文件名另存文件()

ActiveWorkbook.SaveAs Filename:=\信件\\\解答\郎雀.xls\End Sub

136、将A列数据排序到D列

Sub 将A列数据排序到D列() [d:d] = [a:a].Value

[d:d].Sort Key1:=Range(\End Sub

137、将指定范围的数据排列到D列

Sub 将指定范围的数据排列到D列() Dim arr1, arr2, i%, x arr1 = Range(\

ReDim arr2(1 To UBound(arr1, 1) * UBound(arr1, 2), 1 To 1) For Each x In Application.Transpose(arr1) i = i + 1 arr2(i, 1) = x Next x

Range(\End Sub 光标移动

Sub 光标移动()

ActiveCell.Offset(1, 2).Select '向下移动1行,向右移动2列 End Sub

138、光标所在行上移一行

Sub 光标所在行上移一行() Dim i%

i = Split(ActiveCell.Address, \ If i > 1 Then Rows(i).Cut

Rows(i - 1).Insert Shift:=xlDown End If End Sub

139、加数据有效限制

Sub 加数据有效限制()

With Selection.Validation .Delete

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:=\ .IgnoreBlank = False .InCellDropdown = False .InputTitle = \ .ErrorTitle = \ .InputMessage = \

.ErrorMessage = \要奋斗就会有牺牲,死人的事是经常发生的。\ .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End Sub

140、取消数据有效限制

Sub 取消数据有效限制() With Selection.Validation .Delete

.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _ :=xlBetween

.IgnoreBlank = False .InCellDropdown = False .InputTitle = \ .ErrorTitle = \ .InputMessage = \ .ErrorMessage = \

.IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End Sub

141、重排窗口

Sub 重排窗口()

Application.CommandBars(\ Application.CommandBars(\我的工具\ Windows.Arrange ArrangeStyle:=xlCascade End Sub

142、按当前单元文本选择打开指定文件单元

Sub 选择打开文件单元() Dim a

a = ActiveCell.Value

Range(a).Worksheet.Activate Range(a).Select End Sub

143、回车光标向右

Sub 录入光标向右()

Application.MoveAfterReturnDirection = xlToRight End Sub

144、回车光标向下

Sub 录入光标向下()

Application.MoveAfterReturnDirection = xlDown End Sub

145、保护工作表时取消选定锁定单元

Sub 取消选定锁定单元()

ActiveSheet.EnableSelection = xlUnlockedCells '用于2000版End Sub

146、保存并退出Excel

Sub 保存并退出Excel()

Application.SendKeys (\ActiveWorkbook.Save End Sub

147、隐藏/显示指定列空值行

Sub 隐藏显示E列空值行()

Range(\(Range(\End Sub

148、深度隐藏指定工作表

Sub 深度隐藏指定工作表()

Sheets(\用户名密码\End Sub

149、隐藏指定工作表

Sub 隐藏指定工作表()

Sheets(\用户名密码\End Sub

150、隐藏当前工作表

Sub 隐藏当前工作表()

= Not

ActiveWindow.SelectedSheets.Visible = false End Sub

151、返回当前工作表名称

Sub 返回当前工作表名称() wsName = ActiveSheet.Name MsgBox \当前工作表为:\End Sub

152、获取上一次所进入工作簿的工作表名称

Sub 获取上一次所进入工作簿的工作表名称() MsgBox Workbooks(2).ActiveSheet.Name End Sub

153、按光标选定颜色隐藏本列其他颜色行

Sub 按颜色筛选() '思路就是:其它背景色之行全部隐藏

Dim UseRow, AC, i '首先选择一个有颜色之单元格,然后动行宏,其它颜色所在行隐藏

UseRow = Cells.SpecialCells(xlCellTypeLastCell).Row 'SpecialCells(xlCellTypeLastCell)表示已用区域最后一个单元格

If ActiveCell.Row > UseRow Then

MsgBox \请在要筛选的区域选择一个有颜色之单元格!\错误\Else

AC = ActiveCell.Column

Cells.EntireRow.Hidden = False '显示所有行 For i = 2 To UseRow

If Cells(i, AC).Interior.ColorIndex <> ActiveCell.Interior.ColorIndex Then

Cells(i, AC).EntireRow.Hidden = True '如果2至已用行之单元格的有列之颜色不等于当前单元格颜色则隐藏整行 End If Next End If End Sub

154、打开工作簿自动隐藏录入表以外的其他表

Private Sub Workbook_Open() Dim i

For i = 1 To Sheets.Count

If Sheets(i).Name <> \录入\Sheets(i).Visible = False

End If Next End Sub

155、除最左边工作表外深度隐藏所有表

Sub 除最左边工作表外深度隐藏所有表() For i = 2 To ThisWorkbook.Sheets.Count Sheets(i).Visible = xlSheetVeryHidden Next End Sub

156、关闭文件时自动隐藏指定工作表(ThisWorkbook)

Private Sub Workbook_BeforeClose(Cancel As Boolean) ActiveWorkbook.Unprotect

Sheets(\ Sheets(\

ActiveWorkbook.Protect Structure:=True, Windows:=False End Sub

157、打开文件时提示指定工作表是保护状态(ThisWorkbook)

Private Sub Workbook_Open()

If Worksheets(\ MsgBox \保护了.\End If End Sub

158、插入10行

Sub 插入10行()

Rows(ActiveCell.Row & \ Selection.Insert Shift:=xlDown End Sub

159、全选固定范围内小于0的单元

Sub 全选固定范围内小于0的单元() Dim rng As Range Dim yvhf

For Each rng In Range(\

If rng < 0 Then

yvhf = yvhf & rng.Address & \End If Next

Range(Left(yvhf, Len(yvhf) - 1)).Select End Sub

160、全选选定范围内小于0的单元

Sub 全选选定范围内小于0的单元() Dim rng As Range Dim yvhf

For Each rng In Selection If rng < 0 Then

yvhf = yvhf & rng.Address & \End If Next

Range(Left(yvhf, Len(yvhf) - 1)).Select End Sub

161、固定区域单元分类变色

Sub 单元分类变色() Dim rng As Range

For Each rng In Range(\If rng < 0 Then

rng.Interior.ColorIndex = 4 '小于0的单元变绿底色 End If Next

For Each rng In Range(\If rng > 0 Then

rng.Interior.ColorIndex = 3 '文本、假空和大于0的单元变红底色 End If Next

For Each rng In Range(\If rng = 0 Then

rng.Interior.ColorIndex = 2 '空值和等于0的单元变白底色 End If Next End Sub

162、A列半角内容变红

Sub A列半角内容变红() Dim rg As Range, i As Long

Application.ScreenUpdating = False

For Each rg In Cells.SpecialCells(xlCellTypeConstants, 3) For i = 1 To Len(rg)

If Asc(Mid(rg, i, 1)) > 0 Then rg.Characters(i).Font.ColorIndex = 3 Next Next

Application.ScreenUpdating = True End Sub

163、单元格录入数据时运行宏的代码

Private Sub Worksheet_Change(ByVal Target As Range) 重排窗口 End Sub

焦点到A列时运行宏的代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 1 Then 宏名

End If End Sub

164、根据B列最后数据快速合并A列单元格的控件代码

Private Sub CommandButton1_Click() For i = 1 To [b65536].End(xlUp).Row For j = i + 1 To [b65536].End(xlUp).Row If Range(\

Range(\ Else Exit For End If Next j Next i End Sub

165、在F1单元显示光标位置批注内容的代码

Private Sub Worksheet_SelectionChange(ByVal Target As Range) a = Selection.Address

b = Range(a).NoteText Cells(1, 6) = b End Sub

166、显示光标所在单元的批注的代码

Dim r As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range) On Error Resume Next r.Comment.Visible = False Set r = Target

r.Comment.Visible = True End Sub

167、使单元内容保持不变的工作表代码

Private Sub Worksheet_Change(ByVal Target As Range) [B2] = \不可更改的数据\End Sub

168、有条件执行宏

Sub 高级筛选()

If [J1] = 2 Or [K1] = \筛选\ Columns(\ Selection.Clear Range(\

Columns(\ \End If End Sub

169、有条件执行不同的宏

Sub 有条件执行不同的宏() If [b1].Value = \Application.Run \宏1\ElseIf [b1].Value = \Application.Run \宏2\End If End Sub

170、提示确定或取消执行宏

Sub 提示确定或取消执行宏()

If vbOK = MsgBox(\确定要复制吗?\Range(\Msgbox \复制结束\End If End Sub

171、提示开始和结束

Sub 提示结束() Msgbox \运行开始\过程??

Msgbox \运行结束\End Sub

172、拷贝指定表不相邻多列数据到新位置

Sub 拷贝指定表不相邻多列数据到新位置()

Sheets(\End Sub

173、选择2至4行

Sub 选择2至4行() Dim a As Integer Dim b As Integer a = 2 b = 4

Rows(a & \End Sub

174、在当前选区有条件替换数值为文本

Sub 在当前选区有条件替换数值为文本() For Each r In Selection

If r.Value > 18 And r.Value < 29.5 Then r.Value = \Next End Sub

175、自动筛选全部显示指定列

Sub 自动筛选全部显示指定列() Selection.AutoFilter Field:=1 Selection.AutoFilter Field:=2 Selection.AutoFilter Field:=3 Selection.AutoFilter Field:=4 Selection.AutoFilter Field:=5 Selection.AutoFilter Field:=6 End Sub

176、自动筛选第2列值为A的行

Sub 自动筛选第2列值为A的行() [a1].AutoFilter 2, \End Sub

177、取消自动筛选()

Sub 取消自动筛选()

ActiveSheet.AutoFilterMode = False End Sub

178、全部显示指定表的自动筛选

Sub 全部显示指定表的自动筛选() If Sheet1.FilterMode = True Then Sheet1.ShowAllData End If End Sub

179、强行合并单元

Sub 强行合并单元()

Application.DisplayAlerts = False '不出现对话框,按对话框默认选择 Range(\

Application.ScreenUpdating = True End Sub

180、设置单元区域格式

网优257个Excel宏常用语句

257个Excel宏常用语句1、打开全部隐藏工作表.........................................................................................................72、循环宏.........................................
推荐度:
点击下载文档文档为doc格式
9lhnh98e806i8st1cmtn
领取福利

微信扫码领取福利

微信扫码分享