execl中的259个常用宏
Excel宏大量简单常用宏
Excel宏大量简单常用宏代码目录链接类别打开全部隐藏工作表点击循环宏点击录制宏时调用“停止录制”工具栏点击高级筛选5列不重复数据至指定表点击双击单元执行宏(工作表代码)点击双击指定区域单元执行宏(工作表代码)点击进入单元执行宏(工作表代码)点击进入指定区域单元执行宏(工作表代码)点击在多个宏中依次循环执行一个(控件按钮代码)点击在两个宏中依次循环执行一个并相应修改按钮名称点击(控件按钮代码)在三个宏中依次循环执行一个并相应修改按钮名称点击(控件按钮代码)根据A1单元文本隐藏/显示按钮(控件按钮代码)点击当前单元返回按钮名称(控件按钮代码)点击当前单元内容返回到按钮名称(控件按钮代码)点击奇偶页分别打印点击打印自动打印多工作表第一页点击打印查找A列文本循环插入分页符点击打印将A列最后数据行以上的所有B列图片大小调整为所在点击对象单元大小返回光标所在行数点击查找和引用在A1返回当前选中单元格数量点击查找和引用返回当前工作簿中工作表数量点击查找和引用返回光标选择区域的行数和列数点击查找和引用工作表中包含数据的最大行数点击查找和引用返回A列数据的最大行数点击查找和引用将所选区域文本插入新建文本框点击对象批量插入地址批注点击批注批量插入统一批注点击批注以A1单元内容批量插入批注点击批注不连续区域插入当前文件名和表名及地址点击单元赋值不连续区域录入当前单元地址点击单元赋值连续区域录入当前单元地址点击单元赋值返回当前单元地址点击单元赋值不连续区域录入当前日期点击单元赋值不连续区域录入当前数字日期点击单元赋值不连续区域录入当前日期和时间点击单元赋值不连续区域录入对勾点击单元赋值不连续区域录入当前文件名点击单元赋值不连续区域添加文本点击单元赋值不连续区域插入文本点击单元赋值从指定位置向下同时录入多单元指定内容点击单元赋值按aa工作表A列的内容排列工作表标签顺序点击工作表工作表工作表工作表工作表文件管理工作表工作表点击单元赋值当前单元录入计算机用户名点击单元赋值解除全部工作表保护点击工作表为指定工作表加指定密码保护表点击密码在有密码的工作表执行代码点击密码执行前需要验证密码的宏(控件按钮代码)点击密码拷贝A1公式和格式到A2点击单元赋值复制单元数值点击单元赋值插入数值条件格式点击格式插入透明批注点击批注单元赋值定位定位定位定位定位定位点击单元赋值当前单元加2点击单元赋值单元赋值定位单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值事件事件事件事件其他其他定位打印单元赋值对象事件其他信息事件单元赋值单元赋值点击事件指定允许编辑区域点击编辑解除允许编辑区域限制点击编辑删除指定行点击行列操作删除A列为指定内容的行点击行列操作删除A列非数字单元行点击行列操作有条件删除当前行点击行列操作选择下一行点击定位选择第5行开始所有数据行点击定位选择光标或选区所在行点击定位定位名称点击名称选择到指定列的最后行点击定位将Sheet1的A列的非空值写到Sheet2的A列点击单元赋值将名称1的数据写到名称2点击名称定位格式对象点击数据单元赋值名称名称点击工作表按A列数据批量修改表名称点击工作表按A列数据批量创建新表(控件按钮代码)点击工作表清除剪贴板点击其他批量清除软回车点击其他判断指定文件是否已经打开点击事件当前文件另存到指定目录点击文件管理文件管理文件管理文件管理文件管理文件管理文件管理文件管理文件管理引用指定位置单元内容为部分文件名另存文件点击文件管理将A 列数据排序到D列点击单元赋值将指定范围的数据排列到D列点击单元赋值定位行列操作点击数据取消数据有效限制点击数据重排窗口点击窗口按当前单元文本选择打开指定文件单元点击定位回车光标向右点击定位回车光标向下点击定位保护工作表时取消选定锁定单元点击工作表文件管理行列操作工作表工作表工作表工作表格式工作表工作表点击工作表工作表行列操作定位点击定位固定区域单元分类变色点击格式格式事件事件数据点击其他显示光标所在单元的批注的代码点击其他单元赋值事件事件事件事件点击单元赋值选择2至4行点击定位在当前选区有条件替换数值为文本点击事件自动筛选全部显示指定列点击筛选自动筛选第2列值为A的行点击筛选取消自动筛选()点击筛选全部显示指定表的自动筛选点击筛选强行合并单元点击格式格式单元赋值事件事件点击行列操作在A列产生不重复随机数点击单元赋值单元赋值单元赋值点击其他返回指定单元的行高和列宽点击信息指定行高和列宽点击格式指定单元的行高和列宽与A1单元相同点击格式填公式点击单元赋值工作表工作表单元赋值自定义函数信息超链接超链接超链接超链接查找和引用点击查找和引用返回表中各非空单元区域地址(行搜索)点击查找和引用查找和引用查找和引用查找和引用查找和引用查找和引用点击查找和引用返回A列非空单元数量点击查找和引用返回圆周率π点击其他定义指定单元内容为页眉/页脚点击打印提示并全部清除当前选择区域点击单元赋值全部清除当前选择区域点击单元赋值清除指定区域数值点击单元赋值对指定工作表执行取消隐藏》打印》隐藏工作表点击打印打开文件时执行指定宏(工作簿代码)点击事件关闭文件时执行指定宏(工作簿代码)点击事件信息事件点击工作表重算指定表点击工作表将第5行移到窗口的最上面点击窗口对第一张工作表的指定区域进行排序点击单元赋值显示指定工作表的打印预览点击打印用单元格A1的内容作为文件名另存当前工作簿点击文件管理[禁用/启用]保存和另存的代码点击文件管理在A和B列返回当前选区的名称和公式点击单元赋值朗读朗读A列,按ESC键中止点击语音朗读固定语句,请按ESC键终止点击语音在M和N列的14行以下选择单元时显示调用日历控件点击对象(工作表代码)添加自定义序列点击其他弹出打印对话框点击打印打印事件事件点击工作表把a列不重复值取到e列点击查找和引用查找和引用工作表点击事件事件其他点击其他按照当前行A列的图片名称插入图片到H列点击图片当前行下插入1行点击工作表取消指定行或列的隐藏点击工作表复制单元格所在行点击其他复制单元格所在列点击其他新建一个工作表点击工作表新建一个工作簿点击工作簿工作表事件工作簿点击工作簿合并A1至C1的内容写到D15单元的批注中点击批注自动重算点击其他手动重算点击其他EH帖子地址net/dispbbs.asp?boardid=4&id=239820。
(完整)excel常用宏
1.拆分单元格赋值Sub 拆分填充()Dim x As RangeFor Each x In ActiveSheet。
UsedRange.CellsIf x。
MergeCells Thenx.Selectx.UnMergeSelection.Value = x。
ValueEnd IfNext xEnd Sub2.E xcel 宏按列拆分多个excelSub Macro1()Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i&Set rng = Range("A1:f1")Application。
ScreenUpdating = FalseApplication。
DisplayAlerts = Falsearr = Range("a1:a” & Range("b” & Cells.Rows。
Count).End(xlUp)。
Row)Set d = CreateObject("scripting。
dictionary")For i = 2 To UBound(arr)If Not d.Exists(arr(i, 1)) ThenSet d(arr(i, 1)) = Cells(i, 1)。
Resize(1, 13)ElseSet d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 13)) End IfNextk = d.Keyst = d.ItemsFor i = 0 To d。
Count - 1Set wb = Workbooks。
Add(xlWBATWorksheet)With wb。
Sheets(1)rng。
Copy 。
[A1]t(i)。
Copy 。
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、指定允许编辑区域 (32)98、解除允许编辑区域限制 (32)99、删除指定行 (32)100、删除A列为指定内容的行 (32)101、删除A列非数字单元行 (32)102、有条件删除当前行 (32)103、选择下一行 (33)104、选择第5行开始所有数据行 (33)105、选择光标或选区所在行 (33)106、选择光标或选区所在列 (33)107、光标定位到名称指定位置 (33)108、选择名称定义的数据区 (34)109、选择到指定列的最后行 (34)110、将Sheet1的A列的非空值写到Sheet2的A列 (34)111、将名称1的数据写到名称2 (34)112、单元反选 (34)113、调整选中对象中的文字 (35)114、去除指定范围内的对象 (35)115、更新透视表数据项 (35)116、将全部工作表名称写到A列 (36)117、为当前选定的多单元插入指定名称 (36)118、删除全部名称 (36)119、以指定区域为表目录补充新表 (36)120、按A列数据批量修改表名称 (37)121、按A列数据批量创建新表(控件按钮代码) (37)122、清除剪贴板 (37)123、批量清除软回车 (37)124、判断指定文件是否已经打开 (38)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 IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub2、循环宏Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" ThenExit For'如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则只执行一次循环就退出'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For'如果某列出现"完成"内容则退出循环Next iEnd Sub3、录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub4、高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _ "A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending,Header:=xlGuess, _OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub5、双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" ThenExit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub6、双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub7、进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub8、进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub9、在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub10、在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub11、在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"Exit SubEnd IfEnd WithEnd Sub12、根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub13、当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub14、当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub15、奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub16、自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub17、查找A列文本循环插入分页符Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页") 'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _.ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub18、将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小() Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub19、返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange("A1") = xEnd Sub20、在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub21、返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub22、返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub23、工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub24、返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd Sub25、将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).SelectSelection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub26、批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.Address NextEnd IfEnd Sub27、批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub28、以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub29、不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.AddressNextEnd Sub30、不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub31、连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub32、返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub33、不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub34、不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub35、不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss") End Sub36、不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub37、不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub38、不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub39、不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub40、从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub41、按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub42、以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub43、删除全部未选定工作表Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub44、工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选'是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选'否[N]'", vbYesNoCancel, "工作表排序")If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub45、定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub46、在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).NameEnd IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub47、建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub48、查另一文件的全部表名Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub49、当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub50、当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub51、解除全部工作表保护Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub52、为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub53、在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123”打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub54、执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End Sub55、执行前需要验证密码的宏()Sub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口'要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub56、拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub57、复制单元数值Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub58、插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub59、插入透明批注Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalse NextEnd Sub60、添加文本Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub61、光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub62、定位选定单元格式相同的全部单元格Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True)If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub63、按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub64、按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub65、删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is Nothing End Sub66、定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub67、右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub68、当前单元加2Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub69、A列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub70、用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub71、将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub72、在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "") NextEnd Sub73、在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing Then For Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub74、双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub75、双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub76、单元区域引用(工作表代码)Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub77、在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub78、混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub79、指定区域单元双击数据累加(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub80、选择单元区域触发事件(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub81、当修改指定单元内容时自动执行宏(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub。
Excel宏---259个常用宏
代码目录链接类别打开全部隐藏工作表点击循环宏点击录制宏时调用“停止录制”工具栏点击高级筛选5列不重复数据至指定表点击双击单元执行宏(工作表代码)点击双击指定区域单元执行宏(工作表代码)点击进入单元执行宏(工作表代码)点击进入指定区域单元执行宏(工作表代码)点击在多个宏中依次循环执行一个(控件按钮代码)点击在两个宏中依次循环执行一个并相应修改按钮名称点击(控件按钮代码)在三个宏中依次循环执行一个并相应修改按钮名称点击(控件按钮代码)根据A1单元文本隐藏/显示按钮(控件按钮代码)点击当前单元返回按钮名称(控件按钮代码)点击当前单元内容返回到按钮名称(控件按钮代码)点击奇偶页分别打印点击打印自动打印多工作表第一页点击打印查找A列文本循环插入分页符点击打印将A列最后数据行以上的所有B列图片大小调整为所在点击对象单元大小返回光标所在行数点击查找和引用在A1返回当前选中单元格数量点击查找和引用返回当前工作簿中工作表数量点击查找和引用返回光标选择区域的行数和列数点击查找和引用工作表中包含数据的最大行数点击查找和引用返回A列数据的最大行数点击查找和引用将所选区域文本插入新建文本框点击对象批量插入地址批注点击批注批量插入统一批注点击批注以A1单元内容批量插入批注点击批注不连续区域插入当前文件名和表名及地址点击单元赋值不连续区域录入当前单元地址点击单元赋值连续区域录入当前单元地址点击单元赋值返回当前单元地址点击单元赋值不连续区域录入当前日期点击单元赋值不连续区域录入当前数字日期点击单元赋值不连续区域录入当前日期和时间点击单元赋值不连续区域录入对勾点击单元赋值不连续区域录入当前文件名点击单元赋值不连续区域添加文本点击单元赋值不连续区域插入文本点击单元赋值从指定位置向下同时录入多单元指定内容点击单元赋值按aa工作表A列的内容排列工作表标签顺序点击工作表工作表工作表工作表工作表文件管理工作表工作表点击单元赋值当前单元录入计算机用户名点击单元赋值解除全部工作表保护点击工作表为指定工作表加指定密码保护表点击密码在有密码的工作表执行代码点击密码执行前需要验证密码的宏(控件按钮代码)点击密码拷贝A1公式和格式到A2点击单元赋值复制单元数值点击单元赋值插入数值条件格式点击格式插入透明批注点击批注单元赋值定位定位定位定位定位定位点击单元赋值当前单元加2点击单元赋值单元赋值定位单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值单元赋值事件事件事件事件其他其他定位打印单元赋值对象事件其他信息事件单元赋值单元赋值点击事件指定允许编辑区域点击编辑解除允许编辑区域限制点击编辑删除指定行点击行列操作删除A列为指定内容的行点击行列操作删除A列非数字单元行点击行列操作有条件删除当前行点击行列操作选择下一行点击定位选择第5行开始所有数据行点击定位选择光标或选区所在行点击定位定位名称点击名称选择到指定列的最后行点击定位将Sheet1的A列的非空值写到Sheet2的A列点击单元赋值将名称1的数据写到名称2点击名称定位格式对象点击数据单元赋值名称名称点击工作表按A列数据批量修改表名称点击工作表按A列数据批量创建新表(控件按钮代码)点击工作表清除剪贴板点击其他批量清除软回车点击其他判断指定文件是否已经打开点击事件当前文件另存到指定目录点击文件管理文件管理文件管理文件管理文件管理文件管理文件管理文件管理文件管理文件管理引用指定位置单元内容为部分文件名另存文件点击文件管理将A列数据排序到D列点击单元赋值将指定范围的数据排列到D列点击单元赋值定位行列操作点击数据取消数据有效限制点击数据重排窗口点击窗口按当前单元文本选择打开指定文件单元点击定位回车光标向右点击定位回车光标向下点击定位保护工作表时取消选定锁定单元点击工作表文件管理行列操作工作表工作表工作表工作表工作表格式工作表工作表点击工作表工作表行列操作定位点击定位固定区域单元分类变色点击格式格式事件事件数据点击其他显示光标所在单元的批注的代码点击其他单元赋值事件事件事件事件点击单元赋值选择2至4行点击定位在当前选区有条件替换数值为文本点击事件自动筛选全部显示指定列点击筛选自动筛选第2列值为A的行点击筛选取消自动筛选()点击筛选全部显示指定表的自动筛选点击筛选强行合并单元点击格式格式单元赋值事件事件点击行列操作在A列产生不重复随机数点击单元赋值单元赋值单元赋值点击其他返回指定单元的行高和列宽点击信息指定行高和列宽点击格式指定单元的行高和列宽与A1单元相同点击格式填公式点击单元赋值工作表工作表单元赋值自定义函数信息超链接超链接超链接超链接查找和引用点击查找和引用返回表中各非空单元区域地址(行搜索)点击查找和引用查找和引用查找和引用查找和引用查找和引用查找和引用点击查找和引用返回A列非空单元数量点击查找和引用返回圆周率π点击其他定义指定单元内容为页眉/页脚点击打印提示并全部清除当前选择区域点击单元赋值全部清除当前选择区域点击单元赋值清除指定区域数值点击单元赋值对指定工作表执行取消隐藏》打印》隐藏工作表点击打印打开文件时执行指定宏(工作簿代码)点击事件关闭文件时执行指定宏(工作簿代码)点击事件信息事件点击工作表重算指定表点击工作表将第5行移到窗口的最上面点击窗口对第一张工作表的指定区域进行排序点击单元赋值显示指定工作表的打印预览点击打印用单元格A1的内容作为文件名另存当前工作簿点击文件管理[禁用/启用]保存和另存的代码点击文件管理在A和B列返回当前选区的名称和公式点击单元赋值朗读朗读A列,按ESC键中止点击语音朗读固定语句,请按ESC键终止点击语音在M和N列的14行以下选择单元时显示调用日历控件点击对象(工作表代码)添加自定义序列点击其他弹出打印对话框点击打印打印事件事件点击工作表把a列不重复值取到e列点击查找和引用查找和引用工作表点击事件事件其他点击其他按照当前行A列的图片名称插入图片到H列点击图片当前行下插入1行点击工作表取消指定行或列的隐藏点击工作表复制单元格所在行点击其他复制单元格所在列点击其他新建一个工作表点击工作表新建一个工作簿点击工作簿工作表事件工作簿点击工作簿合并A1至C1的内容写到D15单元的批注中点击批注自动重算点击其他手动重算点击其他EH帖子地址net/dispbbs.asp?boardid=4&id=239820。
excel常用宏集合
65:删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()Do(what:="哈哈").Activate'删除行' '删除列Loop Until (what:="哈哈") Is NothingEnd Sub72:在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf = 37 Then myrg = IIf(myrg <> "√", "√", "")NextEnd Sub73:在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf <= 15 ThenIf Not (Target, Range("D6:D20")) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub74:双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub75:双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)(1, 0).SelectEnd IfEnd Sub76:单元区域引用(工作表代码)Private Sub Worksheet_Activate()("A1:B3").Value = ("A1:B3").ValueEnd Sub77:在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not ([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub259个常用宏-excelhome(3)2009-08-15 14:12:5878:混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1)79:指定区域单元双击数据累加(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not ([A1:Y100], Target) Is Nothing Thenoldvalue = Valinputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")= oldvalue + inputvalueEnd IfEnd Sub80:选择单元区域触发事件(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub81:当修改指定单元内容时自动执行宏(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)If Not (Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub82:被指定单元内容限制执行宏Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub83:双击单元隐藏该行(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Rows.Hidden = True84:高亮显示行(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)= 2Rows("1:2"). = 40 '保持1至2行的颜色推荐39,22,40,Rows. = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub85:高亮显示行和列(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)= xlNoneRows. = 34Columns. = 34End Sub86:为指定工作表设置滚动范围(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)= "A1:M30"End Sub87:在指定单元记录打印和预览次数(工作簿代码)Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub88:自动数字金额转大写(工作表代码)Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", (y, "[DBNum2]") & "元")b = IIf(j > , (Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", (Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < , "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub89:将所有工作表的A1单元作为单击按钮(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If = "$A$1" ThenCall 宏名End IfEnd Sub90:闹钟——到指定时间执行宏(工作簿代码)Private Sub Workbook_Open()("11:45:00"), "提示1" '宏名字("12:00:00"), "提示2" '宏名字End Sub91:改变Excel界面标题的宏(工作簿代码)Private Sub Workbook_Open()= "春节快乐"End Sub92:在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Worksheets("表2").Range("A1") = (0, 0)End Sub93:B列录入数据时在A列返回记录时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If = 2 Then(, -1) = NowEnd IfEnd Sub94:当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Not (Target, [A1:A1000]) Is Nothing ThenIf = 1 Then(, 1) = Date(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not (Target, [A1:A1000]) Is Nothing ThenIf = 1 Then(, 1) = Format(Now(), "yyyy-mm-dd")(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub95:指定单元显示光标位置内容(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub96:每编辑一个单元保存文件Private Sub Worksheet_Change(ByVal Target As Range)End Sub97:指定允许编辑区域Sub 指定允许编辑区域()= "B8:G15"98:解除允许编辑区域限制Sub 解除允许编辑区域限制()= ""End Sub99:删除指定行Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").Delete End Sub100:删除A列为指定内容的行Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub101:删除A列非数字单元行Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2). End Sub102:有条件删除当前行Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenShift:=xlUpEnd If103:选择下一行Sub 选择下一行()(1, 0).Rows("1:1").End Sub104:选择第5行开始所有数据行Sub 选择第5行开始所有数据行A()Dim i%i = ("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Rows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & ("*", , , , 1, 2).Row).SelectEnd Sub105:选择光标或选区所在行Sub 选择光标或选区所在行()Sub106:选择光标或选区所在列Sub 选择光标或选区所在列()Sub107:光标定位到名称指定位置Sub 定位()Range(Evaluate("名称"))End Sub108:选择名称定义的数据区Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'("数据区").Select 或者End Sub109:选择到指定列的最后行Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub110:将Sheet1的A列的非空值写到Sheet2的A列Sub 将Sheet1的A列的非空值写到Sheet2的A列()("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1]End Sub111:将名称1的数据写到名称2Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub112:单元反选Sub 单元反选()= False= FalseDim raddress As String, taddress As Stringraddress =taddress =.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address .DeleteEnd With(raddress).Select= TrueEnd Sub113:调整选中对象中的文字Sub 调整选中对象中的文字()'文字居中:自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub114:去除指定范围内的对象Sub 去除指定范围内的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p InIf Not , Range("范围")) Is Nothing Then NextEnd Sub115:更新透视表数据项Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'假如无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws InFor Each pt In= xlMissingItemsNoneNext ptNext wsEnd Sub116:将所有工作表名称写到A列Sub 将所有表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列k = k + 1NextEnd Sub117:为当前选定的多单元插入指定名称Sub 为当前选定的多单元插入指定名称()= "临时"Name:="临时", RefersTo:=Selection '或者换用这行代码也可以End Sub118:删除所有名称Sub 删除所有名称()On Error Resume NextDim l As Integerl = i = l To 1 Step -1(i).DeleteNextEnd Sub119:以指定区域为表目录补充新表Sub 以指定区域为表目录补充新表()Dim dic As Object, sh As WorksheetDim arr, itemarr = Range("B1:BB1")Set dic = CreateObject("")For Each sh In, ""NextFor Each item In arrIf item <> "" And Not (Trim(item)) ThenWith .Name = item End WithEnd IfNextSet dic = NothingEnd Sub120:按A列数据批量修改表名称Sub 按A列数据批量修改表名称()Dim i%For i = 1 To - 1Sheets(i).Name = Cells(i + 1, 1).Text NextEnd Sub121:按A列数据批量创建新表(控件按钮代码)Private Sub CommandButton1_Click()On Error Resume NextDim i%, j%For i = 1 To [a65536].End(xlUp).RowFor j = 2 ToIf Cells(i, 1) = Sheets(j).Name ThenExit ForEnd IfNext(after:=Sheets).Name = Cells(i, 1)NextEnd Sub122:清除剪贴板Sub 清除剪贴板()= False("Task Pane").Visible = FalseEnd Sub123:批量清除软回车Sub 批量清除软回车()'也可直接使用Alt+10或13替换What:=Chr(10), Replacement:="", LookAt:=xlPart, SearchOrder:= _xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False End Sub124:判断指定文件是否已经打开Sub 判断指定文件是否已经打开()Dim x As IntegerFor x = 1 ToIf Workbooks(x).Name = "函数.xls" Then '文件名称MsgBox "文件已打开"Exit SubEnd IfNextMsgBox "文件未打开"End Sub125:当前文件另存到指定目录Sub 当前激活文件另存到指定目录()Filename:="E:\信件\" &End Sub126:另存指定文件名Sub 另存指定文件名()& "\别名.xls"End Sub127:以本工作表名称另存文件到当前目录Sub 以本工作表名称另存文件到当前目录()Filename:= & "\" & & ".xls"End Sub128:将本工作表单独另存文件到Excel当前默认目录Sub 将本工作表单独另存文件到Excel当前默认目录()Filename:= & ".xls"End Sub129:以活动工作表名称另存文件到Excel当前默认目录Sub 以活动工作表名称另存文件到Excel当前默认目录()Filename:= & ".xls", FileFormat:= _xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _, CreateBackup:=FalseEnd Sub130:另存所有工作表为工作簿Sub 另存所有工作表为工作簿()Dim sht As Worksheet= Falseipath = & "\"For Each sht In Sheetsipath & & ".xls" '(工作表名称为文件名)' ipath & & Trim(sht.[d15]) & ".xls" '(文件名称 & D15单元内容)' ipath & Trim(sht.[d15]) & ".xls" '(文件名称为D15单元内容)Next= TrueEnd Sub131:以指定单元内容为新文件名另存文件Sub 以指定单元内容为新文件名另存文件()Filename:= & "\" & Sheet1.[A1]End Sub132:以当前日期为新文件名另存文件Sub 以当前日期为新文件名另存文件()& "\" & Format(Now(), "yyyymmdd") & ".xls"End SubSub 以当前日期为名称另存文件()Filename:=Date & ".xls"End Sub133:以当前日期和时间为新文件名另存文件Sub 以当前日期和时间为新文件名另存文件()& "\" & Format(Now(), "yyyy" & "年" & "mm" & "月" & "dd" & "日" & "h" & "时" & "mm" & "分" & "ss" & "秒") & ".xls"End Sub134:另存本表为TXT文件Sub 另存本表为TXT文件()Dim s As StringDim FullName As String, rng As Range= FalseFullName = & ".txt") '以当前表名为TXT文件名' FullName = Replace, ".xls", ".txt") '以当前文件名为TXT文件名' FullName = Replace, ".xls", & ".txt") '以文件名&表名为TXT文件名Open FullName For Output As #1 '以读写方式打开文件,每次写内容都会覆盖原先的内容'参考帮助,fullname为文件全名For Each rng In Range("a1").CurrentRegions = s & IIf(s = "", "", "|") &If = Range("a1"). ThenPrint #1, s & "|" '把数据写到文本文件里s = ""End IfNextClose #1 '关闭文件= TrueMsgBox "数据已导入文本"End Sub135:引用指定位置单元内容为部分文件名另存文件Sub 引用指定位置单元内容为部分文件名另存文件()Filename:="E:\信件\" & "解答" & Range("sheet1!a1") & "郎雀.xls"End Sub136:将A列数据排序到D列Sub 将A列数据排序到D列()[d:d] = [a:a].Value[d:d].Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlYes End Sub137:将指定范围的数据排列到D列Sub 将指定范围的数据排列到D列()Dim arr1, arr2, i%, xarr1 = Range("A1:C3")ReDim arr2(1 To UBound(arr1, 1) * UBound(arr1, 2), 1 To 1)For Each x In (arr1)i = i + 1arr2(i, 1) = xNext xRange("D1").Resize(i, 1) = arr2End Sub光标移动Sub 光标移动()(1, 2).Select '向下移动1行,向右移动2列End Sub138:光标所在行上移一行Sub 光标所在行上移一行()Dim i%i = Split, "$")(2)If i > 1 ThenRows(i).CutRows(i - 1).Insert Shift:=xlDownEnd IfEnd Sub139:加数据有效限制Sub 加数据有效限制()With.Delete.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="".IgnoreBlank = False.InCellDropdown = False.InputTitle = "".ErrorTitle = "".InputMessage = "".ErrorMessage = "要奋斗就会有牺牲,死人的事是经常发生的。
Excel-259个常用宏
代码目录
链接 类别
1 打开全部隐藏工作表
点击
2 循环宏
点击
3 录制宏时调用“停止录制”工具栏
点击
4 高级筛选5列不重复数据至指定表
点击
5 双击单元执行宏(工作表代码)
点击
6 双击指定区域单元执行宏(工作表代码)
点击
7 进入单元执行宏(工作表代码)
点击
8 进入指定区域单元执行宏(工作表代码)
点击
9 在多个宏中依次循环执行一个(控件按钮代码)
点击
33 不连续区域录入当前日期
点击
34 不连续区域录入当前数字日期
点击
35 不连续区域录入当前日期和时间
点击
36 不连续区域录入对勾
点击
37 不连续区域录入当前文件名
点击
38 不连续区域添加文本
点击
39 不连续区域插入文本
点击
40 从指定位置向下同时录入多单元指定内容
点击
41 按aa工作表A列的内容排列工作表标签顺序
102 选择第5行开始所有数据行
103 选择光标或选区所在行
104 选择光标或选区所在列
105 光标定位到名称指定位置
106 选择名称定义的数据区
107 选择到指定列的最后行
108 将Sheet1的A列的非空值写到Sheet2的A列
109 将名称1的数据写到名称2
110 单元反选
111 调整选中对象中的文字
点击
52 为指定工作表加指定密码保护表
点击
53 在有密码的工作表执行代码
点击
54 执行前需要验证密码的宏(控件按钮代码)
点击
55 拷贝A1公式和格式到A2
_Exce宏大全(经典)
奇偶页分别打印自动打印多工作表第一页查找A列文本循环插入分页符将A列最后数据行以上的所有B列图片大小调整为所在单元大小返回光标所在行数在A1返回当前选中单元格数量返回当前工作簿中工作表数量返回光标选择区域的行数和列数工作表中包含数据的最大行数返回A列数据的最大行数将所选区域文本插入新建文本框批量插入地址批注批量插入统一批注以A1单元内容批量插入批注不连续区域插入当前文件名和表名及地址不连续区域录入当前单元地址连续区域录入当前单元地址返回当前单元地址不连续区域录入当前日期不连续区域录入当前数字日期不连续区域录入当前日期和时间不连续区域录入对勾不连续区域录入当前文件名不连续区域添加文本不连续区域插入文本当前单元录入计算机用户名解除全部工作表保护为指定工作表加指定密码保护表在有密码的工作表执行代码执行前需要验证密码的宏(控件按钮代码)拷贝A1公式和格式到A2复制单元数值插入数值条件格式插入透明批注当前单元加2当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)指定单元显示光标位置内容(工作表代码)每编辑一个单元保存文件指定允许编辑区域解除允许编辑区域限制删除指定行删除A列为指定内容的行删除A列非数字单元行有条件删除当前行选择下一行选择第5行开始所有数据行选择光标或选区所在行选择光标或选区所在列光标定位到名称指定位置选择名称定义的数据区选择到指定列的最后行将Sheet1的A列的非空值写到Sheet2的A列将名称1的数据写到名称2将全部工作表名称写到A列为当前选定的多单元插入指定名称删除全部名称以指定区域为表目录补充新表按A列数据批量修改表名称按A列数据批量创建新表(控件按钮代码)清除剪贴板批量清除软回车判断指定文件是否已经打开当前文件另存到指定目录将A列数据排序到D列将指定范围的数据排列到D列光标移动光标所在行上移一行加数据有效限制取消数据有效限制重排窗口按当前单元文本选择打开指定文件单元回车光标向右回车光标向下保护工作表时取消选定锁定单元打开文件时提示指定工作表是保护状态(ThisWorkbook)插入10行全选固定范围内小于0的单元全选选定范围内小于0的单元固定区域单元分类变色显示光标所在单元的批注的代码使单元内容保持不变的工作表代码有条件执行宏有条件执行不同的宏提示确定或取消执行宏提示开始和结束拷贝指定表不相邻多列数据到新位置选择2至4行在当前选区有条件替换数值为文本自动筛选全部显示指定列自动筛选第2列值为A的行取消自动筛选()全部显示指定表的自动筛选强行合并单元设置单元区域格式在A列产生不重复随机数将A列数据随机排列到F列取消选定区域的公式只保留值(假空转真空)处理导入的显示为科学计数法样式的身份证号返回指定单元的行高和列宽指定行高和列宽指定单元的行高和列宽与A1单元相同填公式建立当前工作表的副本为001表在第一个表前插入多工作表返回表中各非空单元区域地址(行搜索)返回第一个数值行号返回第1行最右边非空单元的列号返回连续数值单元的数量统计指定范围和内容的单元数量统计不同颜色的数字的和(自定义函数)返回非空单元数量返回A列非空单元数量返回圆周率π定义指定单元内容为页眉/页脚提示并全部清除当前选择区域全部清除当前选择区域清除指定区域数值对指定工作表执行取消隐藏》打印》隐藏工作表打开文件时执行指定宏(工作簿代码)关闭文件时执行指定宏(工作簿代码)弹出提示A1单元内容延时15秒执行重排窗口宏撤消工作表保护并取消密码重算指定表将第5行移到窗口的最上面对第一张工作表的指定区域进行排序显示指定工作表的打印预览用单元格A1的内容作为文件名另存当前工作簿[禁用/启用]保存和另存的代码在A和B列返回当前选区的名称和公式朗读朗读A列,按ESC键中止朗读固定语句,请按ESC键终止在M和N列的14行以下选择单元时显示调用日历控件(工作表代码)添加自定义序列弹出打印对话框把a列不重复值取到e列当前选区的行列数单元格录入1位字符就跳转(工作表代码)当指定日期(每月10日)打开文件执行宏提示并清空单元区域返回光标所在行号VBA返回公式结果按照当前行A列的图片名称插入图片到H列当前行下插入1行取消指定行或列的隐藏复制单元格所在行复制单元格所在列新建一个工作表新建一个工作簿选择多表为工作组在当前工作组各表中分别执行指定宏复制当前工作簿的报表到临时工作簿删除指定文件合并A1至C1的内容写到D15单元的批注中自动重算手动重算链接类别EH帖子地址net/dispbbs.asp?boardid登录点击点击宏管理点击其他点击筛选点击宏管理点击宏管理点击宏管理点击宏管理点击宏管理点击宏管理点击宏管理点击控件点击控件点击控件点击打印点击打印点击打印点击对象点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击对象点击批注点击批注点击批注点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击工作表点击工作表点击工作表点击工作表点击工作表点击文件管理点击工作表点击工作表点击单元赋值点击单元赋值点击工作表点击密码点击密码点击密码点击单元赋值点击单元赋值点击格式点击批注点击单元赋值点击定位点击定位点击定位点击定位点击定位点击定位点击单元赋值点击单元赋值点击单元赋值点击定位点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击单元赋值点击事件点击事件点击事件点击事件点击其他点击其他点击定位点击打印点击单元赋值点击对象点击事件点击其他点击信息点击事件点击单元赋值点击单元赋值点击事件点击编辑点击编辑点击行列操作点击行列操作点击行列操作点击行列操作点击定位点击定位点击定位点击定位点击名称点击名称点击定位点击单元赋值点击名称点击定位点击格式点击对象点击数据点击单元赋值点击名称点击名称点击工作表点击工作表点击工作表点击其他点击其他点击事件点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击文件管理点击单元赋值点击单元赋值点击定位点击行列操作点击数据点击数据点击窗口点击定位点击定位点击定位点击工作表点击文件管理点击行列操作点击工作表点击工作表点击工作表点击工作表点击工作表点击格式点击工作表点击工作表点击工作表点击工作表点击行列操作点击定位点击定位点击格式点击格式点击事件点击事件点击数据点击其他点击其他点击单元赋值点击事件点击事件点击事件点击事件点击单元赋值点击定位点击事件点击筛选点击筛选点击筛选点击筛选点击格式点击格式点击单元赋值点击事件点击事件点击行列操作点击单元赋值点击单元赋值点击单元赋值点击其他点击信息点击格式点击格式点击单元赋值点击工作表点击工作表点击单元赋值点击自定义函数点击信息点击超链接点击超链接点击超链接点击超链接点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击查找和引用点击其他点击打印点击单元赋值点击单元赋值点击单元赋值点击打印点击事件点击事件点击信息点击事件点击工作表点击工作表点击窗口点击单元赋值点击打印点击文件管理点击文件管理点击单元赋值点击语音点击语音点击对象点击其他点击打印点击打印点击事件点击事件点击工作表点击查找和引用点击查找和引用点击工作表点击事件点击事件点击其他点击其他点击图片点击工作表点击工作表点击其他点击其他点击工作表点击工作簿点击工作表点击事件点击工作簿点击工作簿点击批注点击其他点击其他。
EXCEL宏编辑命令
voko007259个常用宏-excelhome(1)2009-08-15 14:10:24目录1、打开全部隐藏工作表2、循环宏3、录制宏时调用“停止录制”工具栏4、高级筛选5列不重复数据至指定表5、双击单元执行宏(工作表代码)6、双击指定区域单元执行宏(工作表代码)7、进入单元执行宏(工作表代码)8、进入指定区域单元执行宏(工作表代码)9、在多个宏中依次循环执行一个(控件按钮代码)10、在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)11、在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)12、根据A1单元文本隐藏/显示按钮(控件按钮代码)13、当前单元返回按钮名称(控件按钮代码)14、当前单元内容返回到按钮名称(控件按钮代码)15、奇偶页分别打印16、自动打印多工作表第一页17、查找A列文本循环插入分页符18、将A列最后数据行以上的所有B列图片大小调整为所在单元大小19、返回光标所在行数20、在A1返回当前选中单元格数量21、返回当前工作簿中工作表数量22、返回光标选择区域的行数和列数23、工作表中包含数据的最大行数24、返回A列数据的最大行数25、将所选区域文本插入新建文本框26、批量插入地址批注27、批量插入统一批注28、以A1单元内容批量插入批注29、不连续区域插入当前文件名和表名及地址30、不连续区域录入当前单元地址31、连续区域录入当前单元地址32、返回当前单元地址33、不连续区域录入当前日期34、不连续区域录入当前数字日期35、不连续区域录入当前日期和时间36、不连续区域录入对勾37、不连续区域录入当前文件名38、不连续区域添加文本39、不连续区域插入文本40、从指定位置向下同时录入多单元指定内容41、按aa工作表A列的内容排列工作表标签顺序42、以A1单元文本作表名插入工作表43、删除全部未选定工作表44、工作表标签排序45、定义指定工作表标签颜色46、在目录表建立本工作簿中各表链接目录47、建立工作表文本目录48、查另一文件的全部表名49、当前单元录入计算机名50、当前单元录入计算机用户名51、解除全部工作表保护52、为指定工作表加指定密码保护表53、在有密码的工作表执行代码54、执行前需要验证密码的宏(控件按钮代码)55、执行前需要验证密码的宏()56、拷贝A1公式和格式到A257、复制单元数值58、插入数值条件格式59、插入透明批注60、添加文本61、光标定位到指定工作表A列最后数据行下一单元62、定位选定单元格式相同的全部单元格63、按当前单元文本定位64、按固定文本定位65、删除包含固定文本单元的行或列66、定位数据及区域以上的空值67、右侧单元自动加5(工作表代码)68、当前单元加269、A列等于A列减B列70、用于光标选定多区域跳转指定单元(工作表代码)71、将A1单元录入的数据累加到B1单元(工作表代码)72、在指定颜色区域选择单元时添加/取消"√"(工作表代码)73、在指定区域选择单元时添加/取消"√"(工作表代码)74、双击指定单元,循环录入文本(工作表代码)75、双击指定单元,循环录入文本(工作表代码)76、单元区域引用(工作表代码)77、在指定区域选择单元时数值加1(工作表代码)78、混合文本的编号79、指定区域单元双击数据累加(工作表代码)80、选择单元区域触发事件(工作表代码)81、当修改指定单元内容时自动执行宏(工作表代码)82、被指定单元内容限制执行宏83、双击单元隐藏该行(工作表代码)84、高亮显示行(工作表代码)85、高亮显示行和列(工作表代码)86、为指定工作表设置滚动范围(工作簿代码)87、在指定单元记录打印和预览次数(工作簿代码)88、自动数字金额转大写(工作表代码)89、将全部工作表的A1单元作为单击按钮(工作簿代码)90、闹钟——到指定时间执行宏(工作簿代码)91、改变Excel界面标题的宏(工作簿代码)92、在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)93、B列录入数据时在A列返回记录时间(工作表代码)94、当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)95、指定单元显示光标位置内容(工作表代码)96、每编辑一个单元保存文件97、指定允许编辑区域98、解除允许编辑区域限制99、删除指定行100、删除A列为指定内容的行101、删除A列非数字单元行102、有条件删除当前行103、选择下一行104、选择第5行开始所有数据行105、选择光标或选区所在行106、选择光标或选区所在列107、光标定位到名称指定位置108、选择名称定义的数据区109、选择到指定列的最后行110、将Sheet1的A列的非空值写到Sheet2的A列111、将名称1的数据写到名称2112、单元反选113、调整选中对象中的文字114、去除指定范围内的对象115、更新透视表数据项116、将全部工作表名称写到A列117、为当前选定的多单元插入指定名称118、删除全部名称119、以指定区域为表目录补充新表120、按A列数据批量修改表名称121、按A列数据批量创建新表(控件按钮代码)122、清除剪贴板123、批量清除软回车124、判断指定文件是否已经打开125、当前文件另存到指定目录126、另存指定文件名127、以本工作表名称另存文件到当前目录128、将本工作表单独另存文件到Excel当前默认目录129、以活动工作表名称另存文件到Excel当前默认目录130、另存所有工作表为工作簿131、以指定单元内容为新文件名另存文件133、以当前日期和时间为新文件名另存文件134、另存本表为TXT文件135、引用指定位置单元内容为部分文件名另存文件136、将A列数据排序到D列137、将指定范围的数据排列到D列138、光标所在行上移一行139、加数据有效限制140、取消数据有效限制141、重排窗口143、回车光标向右144、回车光标向下146、保存并退出Excel147、隐藏/显示指定列空值行148、深度隐藏指定工作表149、隐藏指定工作表150、隐藏当前工作表151、返回当前工作表名称152、获取上一次所进入工作簿的工作表名称153、按光标选定颜色隐藏本列其他颜色行154、打开工作簿自动隐藏录入表以外的其他表155、除最左边工作表外深度隐藏所有表156、关闭文件时自动隐藏指定工作表(ThisWorkbook) 157、打开文件时提示指定工作表是保护状态(ThisWorkbook) 158、插入10行159、全选固定范围内小于0的单元160、全选选定范围内小于0的单元161、固定区域单元分类变色162、A列半角内容变红163、单元格录入数据时运行宏的代码164、根据B列最后数据快速合并A列单元格的控件代码165、在F1单元显示光标位置批注内容的代码166、显示光标所在单元的批注的代码167、使单元内容保持不变的工作表代码168、有条件执行宏169、有条件执行不同的宏170、提示确定或取消执行宏171、提示开始和结束172、拷贝指定表不相邻多列数据到新位置173、选择2至4行174、在当前选区有条件替换数值为文本175、自动筛选全部显示指定列176、自动筛选第2列值为A的行177、取消自动筛选()178、全部显示指定表的自动筛选179、强行合并单元180、设置单元区域格式181、在所有工作表的A1单元返回顺序号182、根据A1单元内容返回C1数值183、根据A1内容选择执行宏184、删除A列空行185、在A列产生不重复随机数186、将A列数据随机排列到F列187、取消选定区域的公式只保留值(假空转真空)188、处理导入的显示为科学计数法样式的身份证号189、返回指定单元的行高和列宽190、指定行高和列宽191、指定单元的行高和列宽与A1单元相同191、填公式192、建立当前工作表的副本为001表193、在第一个表前插入多工作表194、清除A列再插入序号195、反方向文本(自定义函数)196、指定选择单元区域弹出消息197、将B列数据添加超链接到K列198、删除B列数据的超链接199、分离临时表A列数据的文本和超链接并整理到数据库表200、分离临时表A列数据的文本和超链接并会同其他数据整理到数据库表201、返回A列最后一个非空单元行号202、返回表中第一个非空单元地址(行搜索)203、返回表中各非空单元区域地址(行搜索)204、返回第一个数值行号205、返回第1行最右边非空单元的列号206、返回连续数值单元的数量207、统计指定范围和内容的单元数量208、统计不同颜色的数字的和(自定义函数)209、返回非空单元数量210、返回A列非空单元数量211、返回圆周率π212、定义指定单元内容为页眉/页脚213、提示并全部清除当前选择区域214、全部清除当前选择区域215、清除指定区域数值216、对指定工作表执行取消隐藏》打印》隐藏工作表217、打开文件时执行指定宏(工作簿代码)218、关闭文件时执行指定宏(工作簿代码)219、弹出提示A1单元内容220、延时15秒执行重排窗口宏221、撤消工作表保护并取消密码222、重算指定表223、将第5行移到窗口的最上面224、对第一张工作表的指定区域进行排序225、显示指定工作表的打印预览226、用单元格A1的内容作为文件名另存当前工作簿227、[禁用/启用]保存和另存的代码228、在A和B列返回当前选区的名称和公式229、朗读朗读A列,按ESC键中止230、朗读固定语句,请按ESC键终止231、在M和N列的14行以下选择单元时显示调用日历控件(工作表代码)232、添加自定义序列233、弹出打印对话框234、返回总页码235、合并各工作表内容236、合并指定目录中所有文件中相同格式工作表的数据237、隐藏指定工作表的指定列238、把a列不重复值取到e列239、当前选区的行列数240、单元格录入1位字符就跳转(工作表代码)241、当指定日期(每月10日)打开文件执行宏242、提示并清空单元区域243、返回光标所在行号244、按照当前行A列的图片名称插入图片到H列245、当前行下插入1行246、取消指定行或列的隐藏247、复制单元格所在行248、复制单元格所在列249、新建一个工作表250、新建一个工作簿251、选择多表为工作组252、在当前工作组各表中分别执行指定宏253、复制当前工作簿的报表到临时工作簿254、删除指定文件255、合并A1至C1的内容写到D15单元的批注中256、自动重算257、手动重算1、打开全部隐藏工作表Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub2、循环宏Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" ThenExit For'如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则只执行一次循环就退出'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For'如果某列出现"完成"内容则退出循环Next iEnd Sub3、录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub4、高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub5、双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" ThenExit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub6、双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub7、进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub8、进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub9、在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub10、在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub11、在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"Exit SubEnd IfEnd WithEnd Sub12、根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub13、当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub14、当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub15、奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub16、自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub17、查找A列文本循环插入分页符Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页") 'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _.ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub18、将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小() Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub19、返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange("A1") = xEnd Sub20、在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub21、返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub22、返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub23、工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub24、返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd Sub25、将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.V alue & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).SelectSelection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub26、批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.Address NextEnd IfEnd Sub27、批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub28、以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub29、不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.AddressNextEnd Sub30、不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub31、连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteV alues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=FalseEnd Sub32、返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub33、不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub34、不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub35、不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss") End Sub36、不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub37、不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub38、不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub39、不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub40、从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub41、按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序() Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub42、以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub43、删除全部未选定工作表Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub44、工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选'是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选'否[N]'", vbYesNoCancel, "工作表排序")If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub259个常用宏-excelhome(2)2009-08-15 14:11:4545、定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub46、在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).NameEnd IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub47、建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub48、查另一文件的全部表名Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub49、当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub50、当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub51、解除全部工作表保护Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub52、为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub53、在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123”打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub54、执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End Sub55、执行前需要验证密码的宏()Sub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口'要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub56、拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub57、复制单元数值Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub58、插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub59、插入透明批注Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub60、添加文本Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub61、光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub62、定位选定单元格式相同的全部单元格Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True)If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub63、按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub64、按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub65、删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is Nothing End Sub66、定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub67、右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub68、当前单元加2Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub69、A列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub70、用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub71、将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub72、在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "") NextEnd Sub73、在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing Then For Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub74、双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean) If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub75、双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub76、单元区域引用(工作表代码)Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub77、在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub259个常用宏-excelhome(3)2009-08-15 14:12:5878、混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub79、指定区域单元双击数据累加(工作表代码)。
EXCEL233个常用宏汇总.
代码目录链接类别登录打开全部隐藏工作表点击工作表循环宏点击宏管理录制宏时调用“停止录制”工具栏点击其他高级筛选5列不重复数据至指定表点击筛选在多个宏中依次循环执行一个(控件按钮代码点击宏管理在两个宏中依次循环执行一个并相应修改按钮名称点击宏管理(控件按钮代码在三个宏中依次循环执行一个并相应修改按钮名称点击宏管理(控件按钮代码根据A1单元文本隐藏/显示按钮(控件按钮代码点击控件当前单元返回按钮名称(控件按钮代码点击控件当前单元内容返回到按钮名称(控件按钮代码点击控件奇偶页分别打印点击打印自动打印多工作表第一页点击打印查找A列文本循环插入分页符点击打印将A列最后数据行以上的所有B列图片大小调整为所在点击对象单元大小返回光标所在行数点击查找和引用返回光标选择区域的行数和列数点击查找和引用工作表中包含数据的最大行数点击查找和引用返回A列数据的最大行数点击查找和引用将所选区域文本插入新建文本框点击对象批量插入地址批注点击批注批量插入统一批注点击批注以A1单元内容批量插入批注点击批注以A1单元文本作表名插入工作表点击单元赋值批量插入当前文件名和表名及地址点击单元赋值区域录入当前单元地址点击单元赋值区域录入当前日期点击单元赋值区域录入当前数字日期点击单元赋值区域录入当前日期和时间点击单元赋值不连续区域录入对勾点击单元赋值不连续区域录入当前文件名点击单元赋值不连续区域添加文本点击单元赋值不连续区域插入文本点击单元赋值从指定位置向下同时录入多单元指定内容点击单元赋值按aa工作表A列的内容排列工作表标签顺序点击工作表删除全部未选定工作表点击工作表工作表标签排序点击工作表在目录表建立本工作簿中各表链接目录点击文件管理建立工作表文本目录点击工作表查另一文件的全部表名点击工作表当前单元录入计算机名点击单元赋值当前单元录入计算机用户名点击单元赋值解除全部工作表保护点击工作表为指定工作表加指定密码保护表点击密码在有密码的工作表执行代码点击密码执行前需要验证密码的宏(控件按钮代码点击密码拷贝A1公式和格式到A2点击单元赋值复制单元数值点击单元赋值插入数值条件格式点击格式插入透明批注点击批注添加文本点击单元赋值光标定位到指定工作表A列最后数据行下一单元点击定位定位选定单元格式相同的全部单元格点击定位按当前单元文本定位点击定位按固定文本定位点击定位删除包含固定文本单元的行或列点击定位定位数据及区域以上的空值点击定位右侧单元自动加5(工作表代码点击单元赋值当前单元加2点击单元赋值A列等于A列减B列点击单元赋值用于光标选定多区域跳转指定单元(工作表代码点击定位将A1单元录入的数据累加到B1单元(工作表代码点击单元赋值点击单元赋值在指定颜色区域选择单元时添加/取消"√"(工作表代码在指定区域选择单元时添加/取消"√"(工作表代码点击单元赋值双击指定单元,循环录入文本(工作表代码点击单元赋值单元区域引用(工作表代码点击单元赋值在指定区域选择单元时数值加1(工作表代码点击单元赋值选择单元区域触发事件(工作表代码点击事件当修改指定单元内容时自动执行宏(工作表代码点击事件双击单元隐藏该行(工作表代码点击事件高亮显示行(工作表代码点击其他高亮显示行和列(工作表代码点击其他为指定工作表设置滚动范围(工作簿代码点击定位在指定单元记录打印次数(工作簿代码点击打印自动数字金额转大写(工作表代码点击单元赋值将全部工作表的A1单元作为单击按钮(工作簿代码点击对象闹钟——到指定时间执行宏(工作簿代码点击事件改变Excel界面标题的宏(工作簿代码点击其他在指定工作表的指定单元返回光标当前多选区地址点击信息(工作簿代码B列录入数据时在A列返回记录时间(工作表代码点击事件指定单元显示光标位置内容(工作表代码点击单元赋值每编辑一个单元保存文件点击事件指定允许编辑区域点击编辑解除允许编辑区域限制点击编辑删除A列为指定内容的行点击行列操作删除A列非数字单元行点击行列操作有条件删除当前行点击行列操作选择下一行点击定位选择第5行开始所有数据行点击定位选择光标或选区所在行点击定位选择光标或选区所在列点击定位光标定位到名称指定位置点击名称选择名称定义的数据区点击名称选择到指定列的最后行点击定位将Sheet1的A列的非空值写到Sheet2的A列点击单元赋值将名称1的数据写到名称2点击名称单元反选点击定位调整选中对象中的文字点击格式去除指定范围内的对象点击对象更新透视表数据项点击数据将全部工作表名称写到A列点击单元赋值为当前选定的多单元插入指定名称点击名称以指定区域为表目录补充新表点击工作表按A列数据批量修改表名称点击工作表按A列数据批量创建新表(控件按钮代码点击工作表清除剪贴板点击其他批量清除软回车点击其他当前文件另存到指定目录点击文件管理另存指定文件名点击文件管理以本工作表名称另存文件到当前目录点击文件管理将本工作表单独另存文件到Excel当前默认目录点击文件管理以活动工作表名称另存文件到Excel当前默认目录点击文件管理另存所有工作表为工作簿点击文件管理以指定单元内容为新文件名另存文件点击文件管理以当前日期为新文件名另存文件点击文件管理以当前日期和时间为新文件名另存文件点击文件管理另存本表为TXT文件点击文件管理引用指定位置单元内容为部分文件名另存文件点击文件管理将A列数据排序到D列点击单元赋值将指定范围的数据排列到D列点击单元赋值光标移动点击定位光标所在行上移一行点击行列操作加数据有效限制点击数据取消数据有效限制点击数据重排窗口点击窗口按当前单元文本选择打开指定文件单元点击定位回车光标向右点击定位回车光标向下点击定位保护工作表时取消选定锁定单元点击工作表保存并退出Excel点击文件管理隐藏/显示指定列空值行点击行列操作深度隐藏指定工作表点击工作表隐藏指定工作表点击工作表隐藏当前工作表点击工作表按光标选定颜色隐藏本列其他颜色行点击格式打开工作簿自动隐藏录入表以外的其他表点击工作表除最左边工作表外深度隐藏所有表点击工作表关闭文件时自动隐藏指定工作表(ThisWorkbook点击工作表打开文件时提示指定工作表是保护状态(ThisWorkbook点击工作表插入10行点击行列操作全选固定范围内小于0的单元点击定位全选选定范围内小于0的单元点击定位固定区域单元分类变色点击格式A列半角内容变红点击格式单元格录入数据时运行宏的代码点击事件焦点到A列时运行宏的代码点击事件根据B列最后数据快速合并A列单元格的控件代码点击数据在F1单元显示光标位置批注内容的代码点击其他显示光标所在单元的批注的代码点击其他使单元内容保持不变的工作表代码点击单元赋值有条件执行宏点击事件有条件执行不同的宏点击事件提示确定或取消执行宏点击事件提示开始和结束点击事件拷贝指定表不相邻多列数据到新位置点击单元赋值选择2至4行点击定位在当前选区有条件替换数值为文本点击事件自动筛选全部显示指定列点击筛选自动筛选第2列值为A的行点击筛选取消自动筛选(点击筛选全部显示指定表的自动筛选点击筛选强行合并单元点击格式指定A列的日期格式点击格式在所有工作表的A1单元返回顺序号点击单元赋值根据A1单元内容返回C1数值点击事件根据A1内容选择执行宏点击事件删除A列空行点击行列操作在A列产生不重复随机数点击单元赋值将A列数据随机排列到F列点击单元赋值取消选定区域的公式只保留值(假空转真空点击单元赋值处理导入的显示为科学计数法样式的身份证号点击其他返回指定单元的行高和列宽点击信息指定行高和列宽点击格式指定单元的行高和列宽与A1单元相同点击格式填公式点击单元赋值建立当前工作表的副本为001表点击工作表插入新表点击工作表在第一个表前插入多工作表点击工作表清除A列再插入序号点击单元赋值反方向文本(自定义函数点击自定义函数指定选择单元区域弹出消息点击信息将B列数据添加超链接到K列点击超链接删除B列数据的超链接点击超链接分离临时表A列数据的文本和超链接并整理到数据库表点击超链接分离临时表A列数据的文本和超链接并会同其他数据整点击超链接理到数据库表返回A列最后一个非空单元行号点击查找和引用返回表中第一个非空单元地址(行搜索点击查找和引用返回表中各非空单元区域地址(行搜索点击查找和引用返回第1行最右边非空单元的列号点击查找和引用统计指定范围和内容的单元数量点击查找和引用返回非空单元数量点击查找和引用返回A列非空单元数量点击查找和引用返回圆周率π点击其他定义指定单元内容为页眉/页脚点击打印提示并全部清除当前选择区域点击单元赋值全部清除当前选择区域点击单元赋值清除指定单元数值点击单元赋值对指定工作表执行取消隐藏》打印》隐藏工作表点击打印打开excel就执行某个宏点击事件弹出提示A1单元内容点击信息延时15秒执行重排窗口宏点击事件撤消工作表保护并取消密码点击工作表重算指定表点击工作表将第5行移到窗口的最上面点击窗口对第一张工作表的指定区域进行排序点击单元赋值显示指定工作表的打印预览点击打印用单元格A1的内容作为文件名另存当前工作簿点击文件管理[禁用/启用]保存和另存的代码点击文件管理在A和B列返回当前选区的名称和公式点击单元赋值朗读朗读A列,按ESC键中止点击语音朗读固定语句,请按ESC键终止点击语音在M和N列的14行以下选择单元时显示调用日历控件点击对象(工作表代码添加自定义序列点击其他弹出打印对话框点击打印返回总页码点击打印合并各工作表内容点击事件隐藏指定工作表的指定列点击工作表把a列不重复值取到e列点击查找和引用当前选区的行列数点击查找和引用单元格录入1位字符就跳转(工作表代码点击工作表当指定日期(每月10日打开文件执行宏点击事件提示并清空单元区域点击事件返回光标所在行号点击其他VBA返回公式结果点击其他按照当前行A列的图片名称插入图片到H列点击图片当前行下插入1行点击工作表取消指定行或列的隐藏复制单元格所在行复制单元格所在列新建一个工作表新建一个工作簿选择多表为工作组复制当前工作簿的报表到临时工作簿删除指定文件点击点击点击点击点击点击点击点击工作表其他其他工作表工作簿工作表工作簿工作簿登录。
300个常用excel宏_成为excel高手
宏文件集▲打开全部隐藏工作表Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub▲循环宏Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" Then Exit For '如果名为'完成标志'的命名单元的值等于'完成',则退 'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For '如果某列出现"完成"内 Next iEnd Sub▲录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub▲高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub▲双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub▲双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then 打开End Sub▲进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub▲进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then C打开End Sub▲在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub▲在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub▲在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"Exit SubEnd IfEnd WithEnd Sub▲根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub▲当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub▲当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCell▲奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub▲自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub▲查找A列文本循环插入分页符Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页")'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub▲将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub▲返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange("A1") = xEnd Sub▲在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub▲返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub▲返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub▲工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub▲返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd Sub▲将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Wi Selection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub▲批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.AddressNextEnd IfEnd Sub▲批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub▲以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub▲不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.NextEnd Sub▲不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub▲连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub▲返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub▲不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub▲不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub▲不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss")End Sub▲不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub▲不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub▲不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub▲不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub▲从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub▲按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub▲以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub▲删除全部未选定工作表Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub▲工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选 '是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选 '否[N]' If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub▲定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub▲在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).End IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub▲建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub▲查另一文件的全部表名Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub▲当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲解除全部工作表保护Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub▲为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub▲在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123” 打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub▲执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口 '要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub▲拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub▲复制单元数值Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub▲插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub▲插入透明批注Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub▲添加文本Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容 End Sub▲光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub▲定位选定单元格式相同的全部单元格Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub▲按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is NothingEnd Sub▲定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub▲当前单元加2Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容 End Sub▲A列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub▲用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub▲将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub▲在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "") NextEnd Sub▲在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing Then For Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub▲双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub▲单元区域引用(工作表代码)Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub▲在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub▲混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub▲指定区域单元双击数据累加(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub▲选择单元区域触发事件(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub▲当修改指定单元内容时自动执行宏(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub▲被指定单元内容限制执行宏Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub▲双击单元隐藏该行(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)Rows(Target.Row).Hidden = TrueEnd Sub▲高亮显示行(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40,Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub▲高亮显示行和列(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub▲为指定工作表设置滚动范围(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Sheet1.ScrollArea = "A1:M30"End Sub▲在指定单元记录打印和预览次数(工作簿代码)Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub▲自动数字金额转大写(工作表代码)Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub▲将全部工作表的A1单元作为单击按钮(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub▲闹钟——到指定时间执行宏(工作簿代码)Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub▲改变Excel界面标题的宏(工作簿代码)Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub▲在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub▲B列录入数据时在A列返回记录时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub▲当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub▲指定单元显示光标位置内容(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub▲每编辑一个单元保存文件Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub▲指定允许编辑区域Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub▲解除允许编辑区域限制Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub▲删除指定行Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub▲删除A列为指定内容的行Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub▲删除A列非数字单元行Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.DeleteEnd Sub▲有条件删除当前行Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenSelection.Delete Shift:=xlUpEnd IfEnd Sub▲选择下一行Sub 选择下一行()ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.SelectEnd Sub▲选择第5行开始所有数据行Sub 选择第5行开始所有数据行A()Dim i%i = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlRows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & Cells.Find("*", , , , 1, 2).Row).SelectEnd Sub▲选择光标或选区所在行Sub 选择光标或选区所在行()Selection.EntireRow.SelectEnd Sub▲选择光标或选区所在列Sub 选择光标或选区所在列()Selection.EntireColumn.SelectEnd Sub▲光标定位到名称指定位置Sub 定位()Application.Goto Range(Evaluate("名称"))End Sub▲选择名称定义的数据区Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'Sheet1.Range("数据区").Select 或者End Sub▲选择到指定列的最后行Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub▲将Sheet1的A列的非空值写到Sheet2的A列Sub 将Sheet1的A列的非空值写到Sheet2的A列()Sheet1.Columns("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1] End Sub▲将名称1的数据写到名称2Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub▲单元反选Sub 单元反选()Application.DisplayAlerts = FalseApplication.ScreenUpdating = FalseDim raddress As String, taddress As Stringraddress = Selection.Addresstaddress = edRange.AddressWith Sheets.Add.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address.DeleteEnd WithActiveSheet.Range(raddress).SelectApplication.ScreenUpdating = TrueEnd Sub▲调整选中对象中的文字Sub 调整选中对象中的文字()'文字居中、自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub▲去除指定范围内的对象Sub 去除指定范围内的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p In My.ShapesIf Not Application.Intersect(p.TopLeftCell, Range("范围")) Is Nothing Then p.Delete NextEnd Sub▲更新透视表数据项Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'如果无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws In ActiveWorkbook.WorksheetsFor Each pt In ws.PivotTablespt.PivotCache.MissingItemsLimit = xlMissingItemsNoneNext ptNext wsEnd Sub▲将全部工作表名称写到A列Sub 将全部表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列k = k + 1NextEnd Sub。
excel常用宏集合
1:打开所有隐藏工作表2:循环宏3:录制宏时调用“停止录制”工具栏4:高级筛选5列不重复数据至指定表5:双击单元执行宏(工作表代码)6:双击指定区域单元执行宏(工作表代码)7:进入单元执行宏(工作表代码)8:进入指定区域单元执行宏(工作表代码)9:在多个宏中依次循环执行一个(控件按钮代码)10:在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)11:在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)12:根据A1单元文本隐藏/显示按钮(控件按钮代码)13:当前单元返回按钮名称(控件按钮代码)14:当前单元内容返回到按钮名称(控件按钮代码)15:奇偶页分别打印16:自动打印多工作表第一页17:查找A列文本循环插入分页符18:将A列最后数据行以上的所有B列图片大小调整为所在单元大小19:返回光标所在行数20:在A1返回当前选中单元格数量21:返回当前工作簿中工作表数量22:返回光标选择区域的行数和列数23:工作表中包含数据的最大行数24:返回A列数据的最大行数25:将所选区域文本插入新建文本框26:批量插入地址批注27:批量插入统一批注28:以A1单元内容批量插入批注29:不连续区域插入当前文件名和表名及地址30:不连续区域录入当前单元地址31:连续区域录入当前单元地址32:返回当前单元地址33:不连续区域录入当前日期34:不连续区域录入当前数字日期35:不连续区域录入当前日期和时间36:不连续区域录入对勾37:不连续区域录入当前文件名38:不连续区域添加文本39:不连续区域插入文本40:从指定位置向下同时录入多单元指定内容41:按aa工作表A列的内容排列工作表标签顺序42:以A1单元文本作表名插入工作表43:删除所有未选定工作表44:工作表标签排序45:定义指定工作表标签颜色46:在目录表建立本工作簿中各表链接目录47:建立工作表文本目录48:查另一文件的所有表名49:当前单元录入计算机名50:当前单元录入计算机用户名51:解除所有工作表保护52:为指定工作表加指定密码保护表53:在有密码的工作表执行代码54:执行前需要验证密码的宏(控件按钮代码)55:执行前需要验证密码的宏()56:拷贝A1公式和格式到A257:复制单元数值58:插入数值条件格式59:插入透明批注60:添加文本61:光标定位到指定工作表A列最后数据行下一单元62:定位选定单元格式相同的所有单元格63:按当前单元文本定位64:按固定文本定位65:删除包含固定文本单元的行或列66:定位数据及区域以上的空值67:右侧单元自动加5(工作表代码)68:当前单元加269:A列等于A列减B列70:用于光标选定多区域跳转指定单元(工作表代码)71:将A1单元录入的数据累加到B1单元(工作表代码)72:在指定颜色区域选择单元时添加/取消"√"(工作表代码)73:在指定区域选择单元时添加/取消"√"(工作表代码)74:双击指定单元,循环录入文本(工作表代码)75:双击指定单元,循环录入文本(工作表代码)76:单元区域引用(工作表代码)77:在指定区域选择单元时数值加1(工作表代码)78:混合文本的编号79:指定区域单元双击数据累加(工作表代码)80:选择单元区域触发事件(工作表代码)81:当修改指定单元内容时自动执行宏(工作表代码)82:被指定单元内容限制执行宏83:双击单元隐藏该行(工作表代码)84:高亮显示行(工作表代码)85:高亮显示行和列(工作表代码)86:为指定工作表设置滚动范围(工作簿代码)87:在指定单元记录打印和预览次数(工作簿代码)88:自动数字金额转大写(工作表代码)89:将所有工作表的A1单元作为单击按钮(工作簿代码)90:闹钟——到指定时间执行宏(工作簿代码)91:改变Excel界面标题的宏(工作簿代码)92:在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)93:B列录入数据时在A列返回记录时间(工作表代码)94:当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)95:指定单元显示光标位置内容(工作表代码)96:每编辑一个单元保存文件97:指定允许编辑区域98:解除允许编辑区域限制99:删除指定行100:删除A列为指定内容的行1:打开所有隐藏工作表Sub 打开所有隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub2:循环宏Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" ThenExit For'假如名为'完成标志'的命名单元的值等于'完成',则退出循环,假如一开始就等于'完成',则只执行一次循环就退出'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For'假如某列出现"完成"内容则退出循环End Sub3:录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub4:高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub5:双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" ThenExit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd Select6:双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub7:进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub8:进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub9:在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub10:在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub11:在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"Exit SubEnd IfEnd WithEnd Sub12:根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub13:当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub14:当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub15:奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub16:自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub17:查找A列文本循环插入分页符Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页") 'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _.ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub18:将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小() Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub19:返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange("A1") = xEnd Sub20:在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub21:返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub22:返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub23:工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub24:返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd Sub25:将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.V alue & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).SelectSelection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub26:批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.Address NextEnd IfEnd Sub27:批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub28:以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub29:不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.AddressNextEnd Sub30:不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub31:连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteV alues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=FalseEnd Sub32:返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub33:不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub34:不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub35:不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss") End Sub36:不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub37:不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub38:不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub39:不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub40:从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub41:按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序() Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub42:以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub43:删除所有未选定工作表Sub 删除所有未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub44:工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选'是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选'否[N]'", vbYesNoCancel, "工作表排序")If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub259个常用宏-excelhome(2)2009-08-15 14:11:4545:定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub46:在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).NameEnd IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub47:建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub48:查另一文件的所有表名Sub 查另一文件的所有表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub49:当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub50:当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub51:解除所有工作表保护Sub 解除所有工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub52:为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub53:在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123” 打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub54:执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End Sub55:执行前需要验证密码的宏()Sub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口'要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub56:拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub57:复制单元数值Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub58:插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub59:插入透明批注Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub60:添加文本Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub61:光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub62:定位选定单元格式相同的所有单元格Sub 定位选定单元格式相同的所有单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True)If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub63:按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub64:按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub65:删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is Nothing End Sub66:定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub67:右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub68:当前单元加2Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub69:A列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub70:用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub71:将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub72:在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "") NextEnd Sub73:在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing Then For Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub74:双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub75:双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub76:单元区域引用(工作表代码)Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub77:在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub259个常用宏-excelhome(3)2009-08-15 14:12:5878:混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub79:指定区域单元双击数据累加(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.V alue)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub80:选择单元区域触发事件(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub81:当修改指定单元内容时自动执行宏(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub82:被指定单元内容限制执行宏Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub83:双击单元隐藏该行(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Rows(Target.Row).Hidden = TrueEnd Sub84:高亮显示行(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40,Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15 End Sub85:高亮显示行和列(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub86:为指定工作表设置滚动范围(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Sheet1.ScrollArea = "A1:M30"End Sub87:在指定单元记录打印和预览次数(工作簿代码)Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub88:自动数字金额转大写(工作表代码)Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub89:将所有工作表的A1单元作为单击按钮(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub90:闹钟——到指定时间执行宏(工作簿代码)Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub91:改变Excel界面标题的宏(工作簿代码)Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub92:在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub93:B列录入数据时在A列返回记录时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub94:当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub95:指定单元显示光标位置内容(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub96:每编辑一个单元保存文件Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub97:指定允许编辑区域Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub98:解除允许编辑区域限制Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub99:删除指定行Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub100:删除A列为指定内容的行Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub101:删除A列非数字单元行Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.Delete End Sub102:有条件删除当前行Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" Then。
Excel宏大全Excel宏大全...
Excel宏大全EXCEL 宏命令大全 ?打开全部隐藏工作表Sub 打开全部隐藏工作表() Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = True Next iEnd Sub循环宏Sub 循环()AAA = Range(“C2″)Dim i As LongDim times As Longtimes = AAA‘times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range(“完成标志”) = “完成” Then Exit For ‘如果名为’完成标志’的命名单元的值等于’完成’,则退出循环,如果一开始就等于’完成’,则只执行一次循环就退出‘If Sheets(“传送参数”).Range(“A” & i).Text = “完成” Then Exit For ’如果某列出现”完成”内容则退出循环Next iEnd Sub录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()mandBars(“Stop Recording”).Visible = True End Sub高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets(“Sheet2″).Range(“A1:E65536″) = “” ‘清除Sheet2的A:D列Range(“A1:E65536″).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _“A1″), Unique:=TrueSheet2.Columns(“A:E”).Sort Key1:=Sheet2.Range(“A2″), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=1, MatchCase:=False,Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range(“$A$1″) = “关闭” Then Exit SubSelect Case Target.AddressCase “$A$4″Call 宏1Cancel = TrueCase “$B$4″Call 宏2Cancel = TrueCase “$C$4″Call 宏3Cancel = TrueCase “$E$4″Call 宏4Cancel = TrueEnd SelectEnd Sub双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range(“$A$1″) = “关闭” Then Exit SubIf Not Application.Intersect(Target, Range(“A4:A9″,“C4:C9″)) Is Nothing Then Call 打开隐藏表End Sub进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)‘以单元格进入代替按钮对象调用宏If Range(“$A$1″) = “关闭” Then Exit SubSelect Case Target.AddressCase “$A$5″ ‘单元地址(Target.Address),或命名单元名字()Call 宏1Case “$B$5″Call 宏2Case “$C$5″Call 宏3End SelectEnd Sub进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range(“$A$1″) = “关闭” Then Exit SubIf Not Application.Intersect(Target,Range(“A4:A9″,”C4:C9″)) Is Nothing Then Call 打开隐藏表End Sub在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = “保护工作表” ThenCall 保护工作表.Caption = “取消工作表保护”Exit SubEnd IfIf .Caption = “取消工作表保护” ThenCall 取消工作表保护.Caption = “保护工作表”Exit SubEnd IfEnd WithEnd Sub在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = “宏1″ ThenCall 宏1.Caption = “宏2″Exit SubEnd IfIf .Caption = “宏2″ ThenCall 宏2.Caption = “宏3″Exit SubEnd IfIf .Caption = “宏3″ ThenCall 宏3.Caption = “宏1″Exit SubEnd IfEnd WithEnd Sub根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range(“A1″) > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click() ActiveCell = CommandButton1.CaptionEnd Sub当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro(“GET.DOCUMENT(50)”) ‘总页数MsgBox “现在打印奇数页,按确定开始.”For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox “现在打印偶数页,按确定开始.”For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox(“请输入起始工作表名字:”) sy = InputBox(“请输入结束工作表名字:”) y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub查找A列文本循环插入分页符Sub 循环插入分页符()‘ Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容Dim i As LongDim times As Longtimes =Application.WorksheetFunction.CountIf(Sheet1.Range(“a:a”), “分页”)‘times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:=”分页”, After:=ActiveCell,LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows,SearchDirection:=xlNext, MatchCase:=False) _.ActivateActiveWindow.SelectedSheets.HPageBreaks.AddBefore:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range(“B1:B” & i)) Is Nothing ThenPic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange(“A1″) = xEnd Sub在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量() t = Application.Sheets.Count MsgBox tEnd Sub返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数() x = Selection.Rows.County = Selection.Columns.CountR ange(“A1″) = xRang e(“A2″) = yEnd Sub工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find(“*”, , , , 1, 2).RowMsgBox nEnd Sub返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range(“a65536″).End(xlUp).RowRange(“B1″) = nEnd Sub将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top +ActiveCell.Height, 250#, 100).SelectSelection.Characters.Text = “问题:” & nWith Selection.Characters(Start:=1, Length:=3).Font .Name = “黑体”.FontStyle = “常规”.Size = 12End WithEnd Sub批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:=”本单元格:” & r.Address & ” of ” & Selection.AddressNextEnd IfEnd Sub批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox(“请输入欲批量插入的批注”, “提示”, “随便输点什么吧”)If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 Then For Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = “[" + + "]” + ActiveS + “!” + mycell.AddressNextEnd Sub不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = “=ADDRESS(ROW(),COLUMN(),4,1)”Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), “yyyy-m-d”) End Sub不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), “yyyymmdd”) End Sub不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), “yyyy-m-d h:mm:ss”)End Sub不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = “√”End Sub不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & “文本内容”NextEnd Sub不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = “文本内容” & sNextEnd Sub从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array(“1″, “2″, “13″, “25″, “46″, “12″, “0″, “20″)[B2].Resize(8, 1) =Application.WorksheetFunction.Transpose(arr)End Sub按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets(“aa”).SelectDo While Cells(I, 1).Value <> “”str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets(“aa”).SelectLoopEnd Sub以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub删除全部未选定工作表Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As Boolean Dim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheets ShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n – 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox(“工作表按升序排列请选‘是[Y]‘. ” & vbCrLf & vbCrLf & “工作表按降序排列请选‘否[N]‘”, vbYesNoCancel, “工作表排序”)If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then ‘Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) <UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse ‘Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) >UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i) End IfNext jNext iEnd IfEnd Sub定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets(“Sheet1″).Tab.ColorIndex = 46 End Sub在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录() Dim s%, Rng As RangeOn Error Resume NextSheets(“目录”).ActivateIf Err = 0 ThenSheets(“目录”).UsedRange.Delete ElseSheets.Add = “目录”End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> “目录” Thens = s + 1Set Rng = Sheets(“目录”).Cells(((s –1) Mod 20) + 1, (s – 1) \ 20 + 1 + 1)Rng = Format(s, ” 0″) & “. ” & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, “#” &Sheets(i).Name & “!A1″, ScreenTip:=Sheets(i).NameEnd IfNextSheets(“目录”).Range(“b:iv”).EntireColumn.ColumnWidth = 20End Sub建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = “目录”For i = 2 To Sheets.CountCells(i – 1, 1) = Sheets(i).Name‘Sheets(1).Hyperlinks.Add Cells(i –1, 1), “#” & Sheets(i).Name & “!A1″ ‘添加超链接NextEnd Sub查另一文件的全部表名Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & “\2.xls”Windows(“1.xls“).Activate‘当前文件名称Sheets(“Sheet1“).Select ‘当前表名称i = 1 ’将表名称返回到第1行For Each sh In Workbooks(“2.xls”).WorksheetsCells(i, 1) = ‘将表名称返回到第1列i = i + 1 ‘返回每个表名称向下移动1行Next shWindows(“2.xls”).Close ‘关闭对象文件Application.ScreenUpdating = TrueEnd Sub当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ(“COMPUTERNAME”)‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ(“Username”)‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub解除全部工作表保护Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:=”123″End Sub在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets(“1″).Unprotect Password:=123 ‘假定表名为“1”,密码为“123” 打开工作表Range(“C:C”).SpecialCells(xlCellTypeBlanks).EntireRow.Hid den = True ‘隐藏C列空值行Sheets(“1″).Protect Password:=123 ‘重新用密码保护工作表End Sub执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox(“请输入密码:”) <> “123″ Then ‘密码是123 MsgBox “密码错误,按确定退出!”, 64, “提示”Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox(“请输入您的使用权限:”, “系统提示”) = 123 Then重排窗口‘要执行的宏代码或宏名称ElseMsgBox “对不起,您没有使用该宏的权限,按确定键后退出!”End IfEnd Sub拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks(“临时表”).Sheets(“表1″).Range(“A1″).CopyWorkbooks(“临时表”).Sheets(“表2″).Range(“A2″).PasteSpecialEnd Sub复制单元数值Sub 复制数值()s = Workbooks(“book1″).Sheets(“Sheet1″).Range(“A1:A2″) Workbooks(“book2″).Sheets(“Sheet1″).Range(“A1:A2″) = s End Sub插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:=”70″Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _Formula1:=”55″Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:=”60″Selection.FormatConditions(3).Interior.ColorIndex = 34 End Sub插入透明批注Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text “透明批注”ments(i).Shape.Fill.Visible = msoFalse NextEnd Sub添加文本Sub 添加文本()Selection = Selection + “×”‘不可在数字后添加文本‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets(“数据库”).[a65536].End(xlUp).RowSheets(“数据库”).SelectRange(“A” & a + 1).SelectEnd Sub定位选定单元格式相同的全部单元格Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:=”", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell =edRange.Find(After:=FoundCell, what:=”", searchformat:=True)If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRange If a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In edRange If a Like “*合计*” ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:=”哈哈”).ActivateSelection.EntireRow.Delete ‘删除行‘ Selection.EntireColumn.Delete ‘删除列Loop Until Cells.Find(what:=”哈哈”) Is Nothi ng End Sub定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub当前单元加2Sub 当前单元加2()Selection = Selection + 2‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End SubA列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) – Cells(i, 2)NextEnd Sub用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range) a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = “$A$1″ Thent = Sheet1.Range(“$B$1″).ValueSheet1.Range(“$B$1″).Value = t + Target.ValueEnd IfEnd Sub在指定颜色区域选择单元时添加/取消”√”(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> “√”, “√”, “”)NextEnd Sub在指定区域选择单元时添加/取消”√”(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target,Range(“D6:D20″)) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = “” Then.Value = “√”Else.Value = “”End IfEnd WithNextEnd IfEnd IfEnd Sub双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> “$A$1” Then Exit SubCancel = TrueT = IIf(T = “好”, “中”, IIf(T = “中”, “差”, “好”)) End Sub。
excel常用宏
1.拆分单元格赋值Sub 拆分填充()Dim x As RangeFor Each x In edRange.CellsIf x.MergeCells Thenx.Selectx.UnMergeSelection.Value = x.ValueEnd IfNext xEnd Sub2.E xcel 宏按列拆分多个excelSub Macro1()Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i& Set rng = Range("A1:f1")Application.ScreenUpdating = FalseApplication.DisplayAlerts = Falsearr = Range("a1:a" & Range("b" & Cells.Rows.Count).End(xlUp).Row)Set d = CreateObject("scripting.dictionary")For i = 2 To UBound(arr)If Not d.Exists(arr(i, 1)) ThenSet d(arr(i, 1)) = Cells(i, 1).Resize(1, 13)ElseSet d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 13)) End IfNextk = d.Keyst = d.ItemsFor i = 0 To d.Count - 1Set wb = Workbooks.Add(xlWBATWorksheet)With wb.Sheets(1)rng.Copy .[A1]t(i).Copy .[A2]End Withwb.SaveAs Filename:=ThisWorkbook.Path & "\" & k(i) & ".xlsx"wb.CloseNextApplication.DisplayAlerts = TrueApplication.ScreenUpdating = TrueMsgBox "完毕"End Sub3.E xcel 宏按列拆分多个sheet在一个工作表中是许多的公司订单记录,如何将它按公司名分拆成一个个工作表,用VBA 实现相当便捷。
excel259个常用宏
宏文件集▲打开全部隐藏工作表返回Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub▲循环宏返回Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" Then Exit For '如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则 'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For '如果某列出现"完成"内容则退出循环Next iEnd Sub▲录制宏时调用“停止录制”工具栏返回Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub▲高级筛选5列不重复数据至指定表返回Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub▲双击单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub▲双击指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲进入单元执行宏(工作表代码)返回'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub▲进入指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲在多个宏中依次循环执行一个(控件按钮代码)返回Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub▲在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub▲在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"End IfEnd WithEnd Sub▲根据A1单元文本隐藏/显示按钮(控件按钮代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub▲当前单元返回按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub▲当前单元内容返回到按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub▲奇偶页分别打印返回Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub▲自动打印多工作表第一页返回Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub▲查找A列文本循环插入分页符返回Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页")'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)Call 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub▲将A列最后数据行以上的所有B列图片大小调整为所在单元大小返回Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub▲返回光标所在行数返回Sub 返回光标所在行数()x = ActiveCell.RowEnd Sub▲在A1返回当前选中单元格数量返回Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub▲返回当前工作簿中工作表数量返回Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub▲返回光标选择区域的行数和列数返回Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub▲工作表中包含数据的最大行数返回Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub▲返回A列数据的最大行数返回Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd SubSub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Selection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub▲批量插入地址批注返回Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.AddressNextEnd IfEnd Sub▲批量插入统一批注返回Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub▲以A1单元内容批量插入批注返回Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub▲不连续区域插入当前文件名和表名及地址返回Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.Address NextEnd Sub▲不连续区域录入当前单元地址返回Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub▲连续区域录入当前单元地址返回Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub▲返回当前单元地址返回Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub▲不连续区域录入当前日期返回Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub▲不连续区域录入当前数字日期返回Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub▲不连续区域录入当前日期和时间返回Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss")End Sub▲不连续区域录入对勾返回Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub▲不连续区域录入当前文件名返回Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub▲不连续区域添加文本返回Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub▲不连续区域插入文本返回Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub▲从指定位置向下同时录入多单元指定内容返回Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr)End Sub▲按aa工作表A列的内容排列工作表标签顺序返回Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub▲以A1单元文本作表名插入工作表返回Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub▲删除全部未选定工作表返回Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub▲工作表标签排序返回Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选 '是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选 '否[N]'", vbYesNoCancel, "工作表排序") If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub▲定义指定工作表标签颜色返回Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub▲在目录表建立本工作簿中各表链接目录返回Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).Name End IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub▲建立工作表文本目录返回Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub▲查另一文件的全部表名返回Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub▲当前单元录入计算机名返回Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲当前单元录入计算机用户名返回 Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲解除全部工作表保护返回Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub▲为指定工作表加指定密码保护表返回Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub▲在有密码的工作表执行代码返回Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123” 打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub▲执行前需要验证密码的宏(控件按钮代码)返回Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口 '要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub▲拷贝A1公式和格式到A2返回Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub▲复制单元数值返回Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub▲插入数值条件格式返回Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub▲插入透明批注返回Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub▲添加文本返回Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲光标定位到指定工作表A列最后数据行下一单元返回Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub▲定位选定单元格式相同的全部单元格返回Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True) If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub▲按当前单元文本定位返回Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲按固定文本定位返回Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲删除包含固定文本单元的行或列返回Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is NothingEnd Sub▲定位数据及区域以上的空值返回Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲右侧单元自动加5(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub▲当前单元加2返回Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲A列等于A列减B列返回Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub▲用于光标选定多区域跳转指定单元(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub▲将A1单元录入的数据累加到B1单元(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub▲在指定颜色区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "")NextEnd Sub▲在指定区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub▲双击指定单元,循环录入文本(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub▲单元区域引用(工作表代码)返回Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub▲在指定区域选择单元时数值加1(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub▲混合文本的编号返回Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub▲指定区域单元双击数据累加(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub▲选择单元区域触发事件(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub▲当修改指定单元内容时自动执行宏(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub▲被指定单元内容限制执行宏返回Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub▲双击单元隐藏该行(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)Rows(Target.Row).Hidden = TrueEnd Sub▲高亮显示行(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40,Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub▲高亮显示行和列(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub▲为指定工作表设置滚动范围(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Sheet1.ScrollArea = "A1:M30"End Sub▲在指定单元记录打印和预览次数(工作簿代码)返回Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub▲自动数字金额转大写(工作表代码)返回Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub▲将全部工作表的A1单元作为单击按钮(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub▲闹钟——到指定时间执行宏(工作簿代码)返回Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub▲改变Excel界面标题的宏(工作簿代码)返回Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub▲在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub▲B列录入数据时在A列返回记录时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub▲当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub▲指定单元显示光标位置内容(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub▲每编辑一个单元保存文件返回Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub▲指定允许编辑区域返回Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub▲解除允许编辑区域限制返回Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub▲删除指定行返回Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub▲删除A列为指定内容的行返回Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub▲删除A列非数字单元行返回Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.DeleteEnd Sub▲有条件删除当前行返回Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenSelection.Delete Shift:=xlUpEnd IfEnd Sub▲选择下一行返回Sub 选择下一行()ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.SelectEnd Sub▲选择第5行开始所有数据行返回Sub 选择第5行开始所有数据行A()Dim i%i = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).EntireRow.RowRows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & Cells.Find("*", , , , 1, 2).Row).SelectEnd Sub▲选择光标或选区所在行返回Sub 选择光标或选区所在行()Selection.EntireRow.SelectEnd Sub▲选择光标或选区所在列返回Sub 选择光标或选区所在列()Selection.EntireColumn.SelectEnd Sub▲光标定位到名称指定位置返回Sub 定位()Application.Goto Range(Evaluate("名称"))End Sub▲选择名称定义的数据区返回Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'Sheet1.Range("数据区").Select 或者End Sub▲选择到指定列的最后行返回Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub▲将Sheet1的A列的非空值写到Sheet2的A列返回Sub 将Sheet1的A列的非空值写到Sheet2的A列()Sheet1.Columns("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1]End Sub▲将名称1的数据写到名称2返回Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub▲单元反选返回Sub 单元反选()Application.DisplayAlerts = FalseApplication.ScreenUpdating = FalseDim raddress As String, taddress As Stringraddress = Selection.Addresstaddress = edRange.AddressWith Sheets.Add.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address.DeleteEnd WithActiveSheet.Range(raddress).SelectApplication.ScreenUpdating = TrueEnd Sub▲调整选中对象中的文字返回Sub 调整选中对象中的文字()'文字居中、自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub▲去除指定范围内的对象返回Sub 去除指定范围内的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p In My.ShapesIf Not Application.Intersect(p.TopLeftCell, Range("范围")) Is Nothing Then p.Delete NextEnd Sub▲更新透视表数据项返回Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'如果无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws In ActiveWorkbook.WorksheetsFor Each pt In ws.PivotTablespt.PivotCache.MissingItemsLimit = xlMissingItemsNoneNext ptNext wsEnd Sub▲将全部工作表名称写到A列返回Sub 将全部表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列。
Excel办公用宏大全
宏文件集▲打开全部隐藏工作表返回Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub▲循环宏返回Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" Then Exit For '如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则 'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For '如果某列出现"完成"内容则退出循环Next iEnd Sub▲录制宏时调用“停止录制”工具栏返回Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub▲高级筛选5列不重复数据至指定表返回Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub▲双击单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub▲双击指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲进入单元执行宏(工作表代码)返回'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub▲进入指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲在多个宏中依次循环执行一个(控件按钮代码)返回Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub▲在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub▲在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"End IfEnd WithEnd Sub▲根据A1单元文本隐藏/显示按钮(控件按钮代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub▲当前单元返回按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub▲当前单元内容返回到按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub▲奇偶页分别打印返回Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub▲自动打印多工作表第一页返回Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub▲查找A列文本循环插入分页符返回Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页")'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)Call 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub▲将A列最后数据行以上的所有B列图片大小调整为所在单元大小返回Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub▲返回光标所在行数返回Sub 返回光标所在行数()x = ActiveCell.RowEnd Sub▲在A1返回当前选中单元格数量返回Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub▲返回当前工作簿中工作表数量返回Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub▲返回光标选择区域的行数和列数返回Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub▲工作表中包含数据的最大行数返回Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub▲返回A列数据的最大行数返回Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd SubSub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Selection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub▲批量插入地址批注返回Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.AddressNextEnd IfEnd Sub▲批量插入统一批注返回Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub▲以A1单元内容批量插入批注返回Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub▲不连续区域插入当前文件名和表名及地址返回Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.Address NextEnd Sub▲不连续区域录入当前单元地址返回Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub▲连续区域录入当前单元地址返回Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub▲返回当前单元地址返回Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub▲不连续区域录入当前日期返回Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub▲不连续区域录入当前数字日期返回Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub▲不连续区域录入当前日期和时间返回Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss")End Sub▲不连续区域录入对勾返回Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub▲不连续区域录入当前文件名返回Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub▲不连续区域添加文本返回Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub▲不连续区域插入文本返回Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub▲从指定位置向下同时录入多单元指定内容返回Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr)End Sub▲按aa工作表A列的内容排列工作表标签顺序返回Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub▲以A1单元文本作表名插入工作表返回Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub▲删除全部未选定工作表返回Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub▲工作表标签排序返回Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选 '是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选 '否[N]'", vbYesNoCancel, "工作表排序") If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub▲定义指定工作表标签颜色返回Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub▲在目录表建立本工作簿中各表链接目录返回Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).Name End IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub▲建立工作表文本目录返回Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub▲查另一文件的全部表名返回Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub▲当前单元录入计算机名返回Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲当前单元录入计算机用户名返回 Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲解除全部工作表保护返回Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub▲为指定工作表加指定密码保护表返回Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub▲在有密码的工作表执行代码返回Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123” 打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub▲执行前需要验证密码的宏(控件按钮代码)返回Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口 '要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub▲拷贝A1公式和格式到A2返回Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub▲复制单元数值返回Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub▲插入数值条件格式返回Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub▲插入透明批注返回Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub▲添加文本返回Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲光标定位到指定工作表A列最后数据行下一单元返回Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub▲定位选定单元格式相同的全部单元格返回Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True) If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub▲按当前单元文本定位返回Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲按固定文本定位返回Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲删除包含固定文本单元的行或列返回Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is NothingEnd Sub▲定位数据及区域以上的空值返回Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲右侧单元自动加5(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub▲当前单元加2返回Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲A列等于A列减B列返回Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub▲用于光标选定多区域跳转指定单元(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub▲将A1单元录入的数据累加到B1单元(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub▲在指定颜色区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "")NextEnd Sub▲在指定区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub▲双击指定单元,循环录入文本(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub▲单元区域引用(工作表代码)返回Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub▲在指定区域选择单元时数值加1(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub▲混合文本的编号返回Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub▲指定区域单元双击数据累加(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub▲选择单元区域触发事件(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub▲当修改指定单元内容时自动执行宏(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub▲被指定单元内容限制执行宏返回Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub▲双击单元隐藏该行(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)Rows(Target.Row).Hidden = TrueEnd Sub▲高亮显示行(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40,Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub▲高亮显示行和列(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub▲为指定工作表设置滚动范围(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Sheet1.ScrollArea = "A1:M30"End Sub▲在指定单元记录打印和预览次数(工作簿代码)返回Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub▲自动数字金额转大写(工作表代码)返回Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub▲将全部工作表的A1单元作为单击按钮(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub▲闹钟——到指定时间执行宏(工作簿代码)返回Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub▲改变Excel界面标题的宏(工作簿代码)返回Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub▲在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub▲B列录入数据时在A列返回记录时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub▲当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub▲指定单元显示光标位置内容(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub▲每编辑一个单元保存文件返回Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub▲指定允许编辑区域返回Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub▲解除允许编辑区域限制返回Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub▲删除指定行返回Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub▲删除A列为指定内容的行返回Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub▲删除A列非数字单元行返回Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.DeleteEnd Sub▲有条件删除当前行返回Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenSelection.Delete Shift:=xlUpEnd IfEnd Sub▲选择下一行返回Sub 选择下一行()ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.SelectEnd Sub▲选择第5行开始所有数据行返回Sub 选择第5行开始所有数据行A()Dim i%i = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).EntireRow.RowRows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & Cells.Find("*", , , , 1, 2).Row).SelectEnd Sub▲选择光标或选区所在行返回Sub 选择光标或选区所在行()Selection.EntireRow.SelectEnd Sub▲选择光标或选区所在列返回Sub 选择光标或选区所在列()Selection.EntireColumn.SelectEnd Sub▲光标定位到名称指定位置返回Sub 定位()Application.Goto Range(Evaluate("名称"))End Sub▲选择名称定义的数据区返回Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'Sheet1.Range("数据区").Select 或者End Sub▲选择到指定列的最后行返回Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub▲将Sheet1的A列的非空值写到Sheet2的A列返回Sub 将Sheet1的A列的非空值写到Sheet2的A列()Sheet1.Columns("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1]End Sub▲将名称1的数据写到名称2返回Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub▲单元反选返回Sub 单元反选()Application.DisplayAlerts = FalseApplication.ScreenUpdating = FalseDim raddress As String, taddress As Stringraddress = Selection.Addresstaddress = edRange.AddressWith Sheets.Add.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address.DeleteEnd WithActiveSheet.Range(raddress).SelectApplication.ScreenUpdating = TrueEnd Sub▲调整选中对象中的文字返回Sub 调整选中对象中的文字()'文字居中、自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub▲去除指定范围内的对象返回Sub 去除指定范围内的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p In My.ShapesIf Not Application.Intersect(p.TopLeftCell, Range("范围")) Is Nothing Then p.Delete NextEnd Sub▲更新透视表数据项返回Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'如果无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws In ActiveWorkbook.WorksheetsFor Each pt In ws.PivotTablespt.PivotCache.MissingItemsLimit = xlMissingItemsNoneNext ptNext wsEnd Sub▲将全部工作表名称写到A列返回Sub 将全部表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列。
EXCEL宏命令大全
EXCEL宏命令⼤全Excel表格公式⼤全1、查找重复内容公式:=IF(COUNTIF(A:A,A2)>1,"重复","")。
2、⽤出⽣年⽉来计算年龄公式:=TRUNC((DAYS360(H6,"2009/8/30",FALSE))/360,0)。
3、从输⼊的18位⾝份证号的出⽣年⽉计算公式:=CONCATENATE(MID(E2,7,4),"/",MID(E2,11,2),"/",MID(E2,13,2))。
4、从输⼊的⾝份证号码内让系统⾃动提取性别,可以输⼊以下公式:=IF(LEN(C2)=15,IF(MOD(MID(C2,15,1),2)=1,"男","⼥"),IF(MOD(MID(C2,17,1),2)=1,"男","⼥"))公式内的“C2”代表的是输⼊⾝份证号码的单元格。
1、求和:=SUM(K2:K56)——对K2到K56这⼀区域进⾏求和;2、平均数:=AVERAGE(K2:K56)——对K2K56这⼀区域求平均数;3、排名:=RANK(K2,K$2:K$56)——对55名学⽣的成绩进⾏排名;4、等级:=IF(K2>=85,"优",IF(K2>=74,"良",IF(K2>=60,"及格","不及格")))5、学期总评:=K20.3+M20.3+N20.4——假设K列、M列和N列分别存放着学⽣的“平时总评”、“期中”、“期末”三项成绩;6、最⾼分:=MAX(K2:K56)——求K2到K56区域(55名学⽣)的最⾼分;7、最低分:=MIN(K2:K56)——求K2到K56区域(55名学⽣)的最低分;8、分数段⼈数统计:(1)=COUNTIF(K2:K56,"100")——求K2到K56区域100分的⼈数;假设把结果存放于K57单元格;(2)=COUNTIF(K2:K56,">=95")-K57——求K2到K56区域95~99.5分的⼈数;假设把结果存放于K58单元格;(3)=COUNTIF(K2:K56,">=90")-SUM(K57:K58)——求K2到K56区域90~94.5分的⼈数;假设把结果存放于K59单元格;(4)=COUNTIF(K2:K56,">=85")-SUM(K57:K59)——求K2到K56区域85~89.5分的⼈数;假设把结果存放于K60单元格;(5)=COUNTIF(K2:K56,">=70")-SUM(K57:K60)——求K2到K56区域70~84.5分的⼈数;假设把结果存放于K61单元格;(6)=COUNTIF(K2:K56,">=60")-SUM(K57:K61)——求K2到K56区域60~69.5分的⼈数;假设把结果存放于K62单元格;(7)=COUNTIF(K2:K56,"<60")——求K2到K56区域60分以下的⼈数;假设把结果存放于K63单元格;说明:COUNTIF函数也可计算某⼀区域男、⼥⽣⼈数。
excel常用宏集合
65:删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is NothingEnd Sub72:在指定颜色区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "") NextEnd Sub73:在指定区域选择单元时添加/取消"√"(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing Then For Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub74:双击指定单元,循环录入文本(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub75:双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub76:单元区域引用(工作表代码)Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub77:在指定区域选择单元时数值加1(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub259个常用宏-excelhome(3)2009-08-15 14:12:5878:混合文本的编号Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "" & (--(Mid(Worksheets(1).Range("B2"), 3, 100))+ 1)End Sub79:指定区域单元双击数据累加(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub80:选择单元区域触发事件(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub81:当修改指定单元容时自动执行宏(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub82:被指定单元容限制执行宏Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub83:双击单元隐藏该行(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)Rows(Target.Row).Hidden = TrueEnd Sub84:高亮显示行(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40, Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub85:高亮显示行和列(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub86:为指定工作表设置滚动围(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Sheet1.ScrollArea = "A1:M30"End Sub87:在指定单元记录打印和预览次数(工作簿代码)Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub88:自动数字金额转大写(工作表代码)Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub89:将所有工作表的A1单元作为单击按钮(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub90:闹钟——到指定时间执行宏(工作簿代码)Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub91:改变Excel界面标题的宏(工作簿代码)Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub92:在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub93:B列录入数据时在A列返回记录时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub94:当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub95:指定单元显示光标位置容(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub96:每编辑一个单元保存文件Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub97:指定允许编辑区域Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub98:解除允许编辑区域限制Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub99:删除指定行Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub100:删除A列为指定容的行Sub 删除A列为指定容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub101:删除A列非数字单元行Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.Delete End Sub102:有条件删除当前行Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenSelection.Delete Shift:=xlUpEnd IfEnd Sub103:选择下一行Sub 选择下一行()ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.SelectEnd Sub104:选择第5行开始所有数据行Sub 选择第5行开始所有数据行A()Dim i%i = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).EntireRow.RowRows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & Cells.Find("*", , , , 1, 2).Row).SelectEnd Sub105:选择光标或选区所在行Sub 选择光标或选区所在行()Selection.EntireRow.SelectEnd Sub106:选择光标或选区所在列Sub 选择光标或选区所在列()Selection.EntireColumn.SelectEnd Sub107:光标定位到名称指定位置Sub 定位()Application.Goto Range(Evaluate("名称"))End Sub108:选择名称定义的数据区Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'Sheet1.Range("数据区").Select 或者End Sub109:选择到指定列的最后行Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub110:将Sheet1的A列的非空值写到Sheet2的A列Sub 将Sheet1的A列的非空值写到Sheet2的A列()Sheet1.Columns("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1] End Sub111:将名称1的数据写到名称2Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub112:单元反选Sub 单元反选()Application.DisplayAlerts = FalseApplication.ScreenUpdating = FalseDim raddress As String, taddress As Stringraddress = Selection.Addresstaddress = edRange.AddressWith Sheets.Add.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address.DeleteEnd WithActiveSheet.Range(raddress).SelectApplication.ScreenUpdating = TrueEnd Sub113:调整选中对象中的文字Sub 调整选中对象中的文字()'文字居中:自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub114:去除指定围的对象Sub 去除指定围的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p In My.ShapesIf Not Application.Intersect(p.TopLeftCell, Range("围")) Is Nothing Then p.DeleteNextEnd Sub115:更新透视表数据项Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'假如无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws In ActiveWorkbook.WorksheetsFor Each pt In ws.PivotTablespt.PivotCache.MissingItemsLimit = xlMissingItemsNoneNext ptNext wsEnd Sub116:将所有工作表名称写到A列Sub 将所有表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列k = k + 1NextEnd Sub117:为当前选定的多单元插入指定名称Sub 为当前选定的多单元插入指定名称() = "临时"s.Add Name:="临时", RefersTo:=Selection '或者换用这行代码也可以End Sub118:删除所有名称Sub 删除所有名称()On Error Resume NextDim l As Integerl = s.CountFor i = l To 1 Step -1s(i).DeleteNextEnd Sub119:以指定区域为表目录补充新表Sub 以指定区域为表目录补充新表()Dim dic As Object, sh As WorksheetDim arr, itemarr = Range("B1:BB1")Set dic = CreateObject("scripting.dictionary")For Each sh In ThisWorkbook.Worksheetsdic.Add , ""NextFor Each item In arrIf item <> "" And Not dic.exists(Trim(item)) Then With ThisWorkbook.Worksheets.Add.Name = itemEnd WithEnd IfNextSet dic = NothingEnd Sub120:按A列数据批量修改表名称Sub 按A列数据批量修改表名称()Dim i%For i = 1 To Sheets.Count - 1Sheets(i).Name = Cells(i + 1, 1).TextNextEnd Sub121:按A列数据批量创建新表(控件按钮代码)Private Sub CommandButton1_Click()On Error Resume NextDim i%, j%For i = 1 To [a65536].End(xlUp).RowFor j = 2 To Sheets.CountIf Cells(i, 1) = Sheets(j).Name ThenExit ForEnd IfNextSheets.Add(after:=Sheets(Sheets.Count)).Name = Cells(i, 1) NextEnd Sub122:清除剪贴板Sub 清除剪贴板()Application.CutCopyMode = FalsemandBars("Task Pane").Visible = FalseEnd Sub123:批量清除软回车Sub 批量清除软回车()'也可直接使用Alt+10或13替换Cells.Replace What:=Chr(10), Replacement:="", LookAt:=xlPart, SearchOrder:= _ xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False End Sub124:判断指定文件是否已经打开Sub 判断指定文件是否已经打开()Dim x As IntegerFor x = 1 To Workbooks.CountIf Workbooks(x).Name = "函数.xls" Then '文件名称MsgBox "文件已打开"Exit SubEnd IfNextMsgBox "文件未打开"End Sub125:当前文件另存到指定目录Sub 当前激活文件另存到指定目录()ActiveWorkbook.SaveAs Filename:="E:\信件\" & End Sub126:另存指定文件名Sub 另存指定文件名()ActiveWorkbook.SaveAs ThisWorkbook.Path & "\别名.xls"End Sub127:以本工作表名称另存文件到当前目录Sub 以本工作表名称另存文件到当前目录()ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & & ".xls"End Sub128:将本工作表单独另存文件到Excel当前默认目录Sub 将本工作表单独另存文件到Excel当前默认目录()ActiveSheet.CopyActiveWorkbook.SaveAs Filename:= & ".xls"End Sub129:以活动工作表名称另存文件到Excel当前默认目录Sub 以活动工作表名称另存文件到Excel当前默认目录()ActiveWorkbook.SaveAs Filename:= & ".xls", FileFormat:= _ xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _, CreateBackup:=FalseEnd Sub130:另存所有工作表为工作簿Sub 另存所有工作表为工作簿()Dim sht As WorksheetApplication.ScreenUpdating = Falseipath = ThisWorkbook.Path & "\"For Each sht In Sheetssht.CopyActiveWorkbook.SaveAs ipath & & ".xls" '(工作表名称为文件名)'ActiveWorkbook.SaveAs ipath & & Trim(sht.[d15]) & ".xls" '(文件名称 & D15单元容)'ActiveWorkbook.SaveAs ipath & Trim(sht.[d15]) & ".xls" '(文件名称为D15单元容)ActiveWorkbook.CloseNextApplication.ScreenUpdating = TrueEnd Sub131:以指定单元容为新文件名另存文件Sub 以指定单元容为新文件名另存文件()ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Sheet1.[A1]End Sub132:以当前日期为新文件名另存文件Sub 以当前日期为新文件名另存文件()ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now(), "yyyymmdd") & ".xls" End SubSub 以当前日期为名称另存文件()ActiveWorkbook.SaveAs Filename:=Date & ".xls"End Sub133:以当前日期和时间为新文件名另存文件Sub 以当前日期和时间为新文件名另存文件()ThisWorkbook.SaveAs ThisWorkbook.Path & "\" & Format(Now(), "yyyy" & "年" & "mm" & "月" & "dd" & "日" & "h" & "时" & "mm" & "分" & "ss" & "秒") & ".xls"End Sub134:另存本表为TXT文件Sub 另存本表为TXT文件()Dim s As StringDim FullName As String, rng As RangeApplication.ScreenUpdating = FalseFullName = ( & ".txt") '以当前表名为TXT文件名' FullName = Replace(ThisWorkbook.FullName, ".xls", ".txt") '以当前文件名为TXT 文件名' FullName = Replace(ThisWorkbook.FullName, ".xls", & ".txt") '以文件名&表名为TXT文件名Open FullName For Output As #1 '以读写方式打开文件,每次写容都会覆盖原先的容'参考帮助,fullname为文件全名For Each rng In Range("a1").CurrentRegions = s & IIf(s = "", "", "|") & rng.ValueIf rng.Column = Range("a1").CurrentRegion.Columns.Count ThenPrint #1, s & "|" '把数据写到文本文件里s = ""End IfNextClose #1 '关闭文件Application.ScreenUpdating = TrueMsgBox "数据已导入文本"End Sub135:引用指定位置单元容为部分文件名另存文件Sub 引用指定位置单元容为部分文件名另存文件()ActiveWorkbook.SaveAs Filename:="E:\信件\" & "解答" & Range("sheet1!a1") & "郎雀.xls"End Sub136:将A列数据排序到D列Sub 将A列数据排序到D列()[d:d] = [a:a].Value[d:d].Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlYesEnd Sub137:将指定围的数据排列到D列Sub 将指定围的数据排列到D列()Dim arr1, arr2, i%, xarr1 = Range("A1:C3")ReDim arr2(1 To UBound(arr1, 1) * UBound(arr1, 2), 1 To 1)For Each x In Application.Transpose(arr1)i = i + 1arr2(i, 1) = xNext xRange("D1").Resize(i, 1) = arr2End Sub光标移动Sub 光标移动()ActiveCell.Offset(1, 2).Select '向下移动1行,向右移动2列End Sub138:光标所在行上移一行Sub 光标所在行上移一行()Dim i%i = Split(ActiveCell.Address, "$")(2)If i > 1 ThenRows(i).CutRows(i - 1).Insert Shift:=xlDownEnd IfEnd Sub139:加数据有效限制Sub 加数据有效限制()With Selection.Validation.Delete.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:="bigsun010sina.".IgnoreBlank = False.InCellDropdown = False.InputTitle = "".ErrorTitle = "".InputMessage = "".ErrorMessage = "要奋斗就会有牺牲,死人的事是经常发生的。
Excel宏大全
Excel宏大全EXCEL 宏命令大全 ?打开全部隐藏工作表Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.Count Sheets(i).Visible = TrueNext iEnd Sub循环宏Sub 循环()AAA = Range(“C2″)Dim i As LongDim times As Longtimes = AAA‘times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range(“完成标志”) = “完成” Then Exit For ‘如果名为’完成标志’的命名单元的值等于’完成’,则退出循环,如果一开始就等于’完成’,则只执行一次循环就退出‘If Sheets(“传送参数”).Range(“A” & i).Text = “完成” Then Exit For ’如果某列出现”完成”内容则退出循环Next iEnd Sub录制宏时调用“停止录制”工具栏Sub 录制宏时调用停止录制工具栏()/doc/b0c8331752d380eb62946da2.ht mlmandBars(“Stop Recording”).Visible = TrueEnd Sub高级筛选5列不重复数据至指定表Sub 高级筛选5列不重复数据至Sheet2()Sheets(“Sheet2″).Range(“A1:E65536″) = “” ‘清除Sheet2的A:D列Range(“A1:E65536″).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _“A1″), Unique:=TrueSheet2.Columns(“A:E”).Sort Key1:=Sheet2.Range(“A2″), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub双击单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range(“$A$1″) = “关闭” Then Exit SubSelect Case Target.AddressCase “$A$4″Call 宏1Cancel = TrueCase “$B$4″Call 宏2Cancel = TrueCase “$C$4″Call 宏3Cancel = TrueCase “$E$4″Call 宏4Cancel = TrueEnd SelectEnd Sub双击指定区域单元执行宏(工作表代码)Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range(“$A$1″) = “关闭” Then Exit SubIf Not Application.Intersect(Target, Range(“A4:A9″, “C4:C9″)) Is Nothing Then Call 打开隐藏表End Sub进入单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)‘以单元格进入代替按钮对象调用宏If Range(“$A$1″) = “关闭” Then Exit SubSelect Case Target.AddressCase “$A$5″ ‘单元地址(Target.Address),或命名单元名字(/doc/b0c8331752d380eb62946da2.html)Call 宏1Case “$B$5″Call 宏2Case “$C$5″Call 宏3End SelectEnd Sub进入指定区域单元执行宏(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range(“$A$1″) = “关闭” Then Exit SubIf Not Application.Intersect(Target, Range(“A4:A9″,”C4:C9″)) Is Nothing Then Call 打开隐藏表End Sub在多个宏中依次循环执行一个(控件按钮代码)Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Private Sub CommandButton1_Click()With CommandButton1If .Caption = “保护工作表” ThenCall 保护工作表.Caption = “取消工作表保护”Exit SubEnd IfIf .Caption = “取消工作表保护” ThenCall 取消工作表保护.Caption = “保护工作表”Exit SubEnd IfEnd WithEnd Sub在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = “宏1″ Then Call 宏1.Caption = “宏2″Exit SubEnd IfIf .Caption = “宏2″ Then Call 宏2.Caption = “宏3″Exit SubEnd IfIf .Caption = “宏3″ Then Call 宏3.Caption = “宏1″Exit SubEnd IfEnd WithEnd Sub根据A1单元文本隐藏/显示按钮(控件按钮代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range(“A1″) > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub当前单元返回按钮名称(控件按钮代码)Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub当前单元内容返回到按钮名称(控件按钮代码)Private Sub CommandButton1_Click() CommandButton1.Caption = ActiveCellEnd Sub奇偶页分别打印Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro(“GET.DOCUMENT(50)”) ‘总页数MsgBox “现在打印奇数页,按确定开始.”For i = 1 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox “现在打印偶数页,按确定开始.”For i = 2 To Ps Step 2 ActiveSheet.PrintOut from:=i, To:=i Next iEnd Sub自动打印多工作表第一页Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox(“请输入起始工作表名字:”) sy = InputBox(“请输入结束工作表名字:”) y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub查找A列文本循环插入分页符Sub 循环插入分页符()‘ Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range(“a:a”), “分页”)‘times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:=”分页”, After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _.ActivateActiveWindow.SelectedSheets.HPageBreaks.AddBefore:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub将A列最后数据行以上的所有B列图片大小调整为所在单元大小Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range(“B1:B” & i)) Is Nothing ThenPic.Top = Pic.T opLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub返回光标所在行数Sub 返回光标所在行数()x = ActiveCell.RowRange(“A1″) = xEnd Sub在A1返回当前选中单元格数量Sub 在A1返回当前选中单元格数量() [A1] = Selection.CountEnd Sub返回当前工作簿中工作表数量Sub 返回当前工作簿中工作表数量() t = Application.Sheets.Count MsgBox tEnd Sub返回光标选择区域的行数和列数Sub 返回光标选择区域的行数和列数() x = Selection.Rows.County = Selection.Columns.Count Range(“A1″) = xRange(“A2″) = yEnd Sub工作表中包含数据的最大行数Sub 包含数据的最大行数()n = Cells.Find(“*”, , , , 1, 2).Row MsgBox nEnd Sub返回A列数据的最大行数Sub 返回A列数据的最大行数()n = Range(“a65536″).End(xlUp).Row Range(“B1″) = nEnd Sub将所选区域文本插入新建文本框Sub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizon tal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).SelectSelection.Characters.Text = “问题:” & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = “黑体”.FontStyle = “常规”.Size = 12End WithEnd Sub批量插入地址批注Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selection/doc/b0c8331752d380eb62946da2.ht mlment.Deleter.AddComment/doc/b0c8331752d380eb62946da2.ht mlment.Visible = False/doc/b0c8331752d380eb62946da2.ht mlment.Text Text:=”本单元格:” & r.Address & ” of ” & Selection.AddressNextEnd IfEnd Sub批量插入统一批注Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox(“请输入欲批量插入的批注”, “提示”, “随便输点什么吧”)If Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddComment/doc/b0c8331752d380eb62946da2.ht mlment.Visible = False/doc/b0c8331752d380eb62946da2.ht mlment.Text Text:=msgNextEnd IfEnd Sub以A1单元内容批量插入批注Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddComment/doc/b0c8331752d380eb62946da2.ht mlment.Visible = False/doc/b0c8331752d380eb62946da2.ht mlment.Text Text:=[a1].TextNextEnd IfEnd Sub不连续区域插入当前文件名和表名及地址Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = “[" + /doc/b0c8331752d380eb62946da2.html +"]” + /doc/b0c8331752d380eb62946da2.html + “!” + mycell.AddressNextEnd Sub不连续区域录入当前单元地址Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub连续区域录入当前单元地址Sub 连续区域录入当前单元地址()Selection = “=ADDRESS(ROW(),COLUMN(),4,1)”Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub返回当前单元地址Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub不连续区域录入当前日期Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), “yyyy-m-d”) End Sub不连续区域录入当前数字日期Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), “yyyymmdd”) End Sub不连续区域录入当前日期和时间Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), “yyyy-m-d h:mm:ss”)End Sub不连续区域录入对勾Sub 批量录入对勾()Selection.FormulaR1C1 = “√”End Sub不连续区域录入当前文件名Sub 批量录入当前文件名()Selection.FormulaR1C1 = /doc/b0c8331752d380eb62946da2.htmlEnd Sub不连续区域添加文本Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & “文本内容”NextEnd Sub不连续区域插入文本Sub 批量插入文本()Dim s As RangeFor Each s In Selections = “文本内容” & sNextEnd Sub从指定位置向下同时录入多单元指定内容Sub 从指定位置向下同时录入多单元指定内容() Dim arrarr = Array(“1″, “2″, “13″, “25″, “46″, “12″, “0″, “20″)[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr)End Sub按aa工作表A列的内容排列工作表标签顺序Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets(“aa”).SelectDo While Cells(I, 1).Value <> “”str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets(“aa”).SelectLoopEnd Sub以A1单元文本作表名插入工作表Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add/doc/b0c8331752d380eb62946da2.ht ml = nmEnd Sub删除全部未选定工作表Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = /doc/b0c8331752d380eb62946da2.htmln = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n – 1If ShtName(i) = /doc/b0c8331752d380eb62946da2.html TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub工作表标签排序Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox(“工作表按升序排列请选‘是[Y]‘. ” & vbCrLf & vbCrLf & “工作表按降序排列请选‘否[N]‘”, vbYesNoCancel, “工作表排序”)If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then ‘Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i)End IfNext jNext iElse ‘Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub定义指定工作表标签颜色Sub 定义指定工作表标签颜色()Sheets(“Sheet1″).Tab.ColorIndex = 46End Sub在目录表建立本工作簿中各表链接目录Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets(“目录”).ActivateIf Err = 0 ThenSheets(“目录”).UsedRange.DeleteElseSheets.Add/doc/b0c8331752d380eb62946da2.ht ml = “目录”End IfFor i = 1 To Sheets.CountIf S heets(i).Name <> “目录” Thens = s + 1Set Rng = Sheets(“目录”).Cells(((s – 1) Mod20) + 1, (s – 1) \ 20 + 1 + 1)Rng = Format(s, ” 0″) & “. ” & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, “#” & Sheets(i).Name & “!A1″, ScreenTip:=Sheets(i).NameEnd IfNextSheets(“目录”).Range(“b:iv”).EntireColumn.ColumnWidth = 20End Sub建立工作表文本目录Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = “目录”For i = 2 To Sheets.CountCells(i – 1, 1) = Sheets(i).Name‘Sheets(1).Hyperlinks.Add Cells(i –1, 1), “#” & Sheets(i).Name & “!A1″ ‘添加超链接NextEnd Sub查另一文件的全部表名Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & “\2.xls”Windows(“1.xls“).Activate ‘当前文件名称Sheets(“Sheet1“).Select ‘当前表名称i = 1 ’将表名称返回到第1行For Each sh In Workbooks(“2.xls”).WorksheetsCells(i, 1) = /doc/b0c8331752d380eb62946da2.html ‘将表名称返回到第1列i = i + 1 ‘返回每个表名称向下移动1行Next shWindows(“2.xls”).Close ‘关闭对象文件Application.ScreenUpdating = TrueEnd Sub当前单元录入计算机名Sub 当前单元录入计算机名()Selection = Environ(“COMPUTERNAME”)‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub当前单元录入计算机用户名Sub 当前单元录入计算机用户名()Selection = Environ(“Username”)‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub解除全部工作表保护Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub为指定工作表加指定密码保护表Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:=”123″End Sub在有密码的工作表执行代码Sub 在有密码的工作表执行代码()Sheets(“1″).Unprotect Password:=123 ‘假定表名为“1”,密码为“123” 打开工作表Range(“C:C”).SpecialCells(xlCellTypeBlanks).EntireRow.Hid den = True ‘隐藏C列空值行Sheets(“1″).Protect Password:=123 ‘重新用密码保护工作表End Sub执行前需要验证密码的宏(控件按钮代码)Private Sub CommandButton1_Click()If InputBox(“请输入密码:”) <> “123″ Then ‘密码是123MsgBox “密码错误,按确定退出!”, 64, “提示”Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox(“请输入您的使用权限:”, “系统提示”) = 123 Then重排窗口‘要执行的宏代码或宏名称ElseMsgBox “对不起,您没有使用该宏的权限,按确定键后退出!”End IfEnd Sub拷贝A1公式和格式到A2Sub 拷贝A1公式到A2()Workbooks(“临时表”).Sheets(“表1″).Range(“A1″).CopyWorkbooks(“临时表”).Sheets(“表2″).Range(“A2″).PasteSpecialEnd Sub复制单元数值Sub 复制数值()s = Workbooks(“book1″).Sheets(“Sheet1″).Range(“A1:A2″)Workbooks(“book2″).Sheets(“Sheet1″).Range(“A1:A2″) = sEnd Sub插入数值条件格式Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Form ula1:=”70″Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _Formula1:=”55″Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:=”60″Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub插入透明批注Sub 插入透明批注()Selection.AddComment/doc/b0c8331752d380eb62946da2.ht mlment.Visible = FalseDim XS As WorksheetFor i = 1 To /doc/b0c8331752d380eb62946da2.htmlm ents.Count/doc/b0c8331752d380eb62946da2.ht mlments(i).Text “透明批注”/doc/b0c8331752d380eb62946da2.htmlments(i).Shape.Fill.Visible = msoFalseNextEnd Sub添加文本Sub 添加文本()Selection = Selection + “×” ‘不可在数字后添加文本‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End Sub光标定位到指定工作表A列最后数据行下一单元Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets(“数据库”).[a65536].End(xlUp).RowSheets(“数据库”).SelectRange(“A” & a + 1).SelectEnd Sub定位选定单元格式相同的全部单元格Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith Application.FindFormat.Clear.NumberFormatLocal = Selection.NumberFormatLocal.HorizontalAlignment = Selection.HorizontalAlignment.VerticalAlignment = Selection.VerticalAlignment.WrapText = Selection.WrapText.Orientation = Selection.Orientation.AddIndent = Selection.AddIndent.IndentLevel = Selection.IndentLevel.ShrinkT oFit = Selection.ShrinkT oFit.MergeCells = Selection.MergeCells/doc/b0c8331752d380eb62946da2.html = /doc/b0c8331752d380eb62946da2.html.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.FormulaHiddenEnd WithSet FirstCell = /doc/b0c8331752d380eb62946da2.htmle dRange.Find(what:=”", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = /doc/b0c8331752d380eb62946da2.htmle dRange.Find(After:=FoundCell, what:=”", searchformat:=True)If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub按当前单元文本定位Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In /doc/b0c8331752d380eb62946da2.htmle dRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub按固定文本定位Sub 文本定位()Dim aa As RangeFor Each a In /doc/b0c8331752d380eb62946da2.htmle dRangeIf a Like “*合计*” ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub删除包含固定文本单元的行或列Sub 删除包含固定文本单元的行或列()DoCells.Find(what:=”哈哈”).ActivateSelection.EntireRow.Delete ‘删除行‘ Selection.EntireColumn.Delete ‘删除列Loop Until Cells.Find(what:=”哈哈”) Is NothingEnd Sub定位数据及区域以上的空值Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In /doc/b0c8331752d380eb62946da2.htmle dRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub右侧单元自动加5(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub当前单元加2Sub 当前单元加2()Selection = Selection + 2‘Selection = Workbooks(“临时表”).Sheets(“表2″).Range(“A1″) 调用指定地址内容End SubA列等于A列减B列Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) – Cells(i, 2)NextEnd Sub用于光标选定多区域跳转指定单元(工作表代码)Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub将A1单元录入的数据累加到B1单元(工作表代码)Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = “$A$1″ Thent = Sheet1.Range(“$B$1″).ValueSheet1.Range(“$B$1″).Value = t + T arget.ValueEnd IfEnd Sub在指定颜色区域选择单元时添加/取消”√”(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In T argetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> “√”, “√”, “”)NextEnd Sub在指定区域选择单元时添加/取消”√”(工作表代码)Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range(“D6:D20″)) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = “” Then。
Excel自编宏大全(Word版)
Excel自编宏大全(Word版)目录1,从数据源匹配取数的问题2,部分字符地址查找3,多表查询汇总和重复值问题(相同行删除、循环比较)4,工作表的名称和index号5,重复值加色6,统计7,最大或最小8,最后一记录(定义动态区域名称、不重复值公式宏、不重复值个数和行数公式宏、加边框宏)9,大港表格转换10,筛选尾数11,对比数据12,修改批注字体13,删除合并单元格14,物品领用报表15,条件格式设置16,多表查询,自动筛选法17,多条件查询累计汇总18,和值19,教师安排汇总(循环比较、不重复值)20,自动着色(不同个数、不同颜色)21,不重复值的个数及所在行的行数(各个值的个数、行数)22,分表自动字体格式化23,自动填充数字24,导入文本文件25,累计不变化(内部循环)26,同结构多表统计汇总(Consolidate方法)27,资产负债表汇总(多工作簿汇总)28,导出到文本文件29,角度求和的自定义公式30,表单输入模板31,两表间复制与核对1,从数据源匹配取数的问题Sub 宏131()'从数据源匹配取数的问题131.xls' 2007-1-31' Shizx98'Dim a As Range, Myrng1 As Range, Myrng2 As RangeDim Myrow As IntegerDim Myrow1 As IntegerDim Myrow2 As IntegerDim Myrow3 As IntegerDim x As IntegerWorksheets("Sheet1").ActivateRange("d2").SelectSelection.CurrentRegion.SelectMyrow2 = Selection.Rows.Count 'D 列数据的行数Range("a1").SelectMyrow3 = Selection.CurrentRegion.Rows.Count 'AB 列数据的行数Set Myrng1 = Range(Cells(2, 1), Cells(Myrow3, 1))Set Myrng2 = Range(Cells(2, 2),Cells(Myrow3, 2))For x = 2 To Myrow2 + 1Set a = Range("D" & x)For y = 1 To Myrow3If Len(a) > 7 ThenMyrow = Application.WorksheetFunction.Match(a, Myrng1, 0)ElseMyrow = Application.WorksheetFunction.Match(a, Myrng2, 0)End IfIf Myrow = 0 ThenGoTo 100ElseRange("F1").SelectSelection.CurrentRegion.SelectMyrow1 = Selection.Rows.CountRange(Cells(Myrow + 1, 1), Cells(Myrow + 1, 2)).SelectSelection.Cut Destination:=Range(Cells(Myrow1 + 1, 6), Cells(Myrow1 + 1, 7))Selection.Delete Shift:=xlUpMyrow = 0MsgBox "已找到!"GoTo 200End If100: Next y200: Next xEnd Sub2,部分字符地址查找‘2007/1/30‘部分字符地址查找.xlsSub bfzfcz()Dim Myrow1 As IntegerDim Myrow2 As IntegerDim x%, y1%, y2%, gg%Dim AA, BBOn Error Resume NextRange("a2").SelectSelection.CurrentRegion.SelectMyrow1 = Selection.Rows.CountRange("e1").SelectSelection.CurrentRegion.SelectMyrow2 = Selection.Rows.Countgg = 2For x = 2 To Myrow2AA = Range("e" & x)For y1 = 2 To Myrow1 + 1BB = Application.WorksheetFunction.SearchB(AA, Cells(y1, 1))If BB > 0 ThenRange("g" & gg) = "A" & y1gg = gg + 1ElseEnd IfBB = 0Next y1For y2 = 2 To Myrow1 + 1BB = Application.WorksheetFunction.SearchB(AA, Cells(y2, 2))If BB > 0 ThenRange("g" & gg) = "B" & y2gg = gg + 1ElseEnd IfBB = 0Next y2'gg = gg + 1Next xEnd Sub3,多表查询汇总和重复值问题(相同行删除、循环比较)Sub 宏0204()''见汇总0204.xls' 2007-2-4'蓝桥玄霜'大汇总问题'Dim x As Integer, y As IntegerDim rng1 As Range, tbl As RangeDim n As IntegerDim Myrow1 As Integer, Myrow2 As IntegerDim rng2Application.ScreenUpdating = FalseSheets("汇总").Select '清除总表原有的数据Range("a1").SelectSet tbl = ActiveCell.CurrentRegionIf tbl.Rows.Count > 1 Thentbl.Offset(1, 0).Resize(tbl.Rows.Count - 1, tbl.Columns.Count).ClearContentsElseEnd Ifn = 2Sheets("使用型号表").SelectRange("a1").SelectMyrow1=[a65536].End(xlUp).Row 'A列最下面一行的行数,中间有空格也行For x = 2 To Myrow1Sheets("使用型号表").SelectSet rng1 = Range("B" & x) '依次把“使用数量”的值赋给rng1变量rng2 = Range("A" & x).Text '把序号里的表格名赋给rng2变量If rng1.Value <> "" ThenSheets("汇总").Cells(1, 6).Value =rng1.ValueSheets(rng2).Select '用表格名选择表格Range("a1").SelectMyrow2 = Selection.CurrentRegion.Rows.Count '数据的行数Range(Cells(2, 2), Cells(Myrow2, 5)).Copy '复制这些数据Sheets("汇总").ActivateCells(n, 2).PasteSpecial '粘贴到汇总表Range(Cells(n, 6), Cells(Myrow2 + n - 2, 6)).Select '选择F列相同行数Selection.FormulaR1C1 = "=RC[-1]*r1c6" '将使用数量X 数量Range(Cells(n, 6), Cells(Myrow2 + n - 2, 6)).Copy '复制这些数据Cells(n, 5).SelectSelection.PasteSpecial Paste:=xlValues'以“选择性粘贴”的“数值”粘贴Range(Cells(n, 6), Cells(Myrow2 + n - 2, 6)).ClearContents '清除F列数量Cells(1, 6).ClearContentsn = n + Myrow2 - 1 '为下次粘贴数据的行位置ElseEnd IfNext xbcfhz0204 '不重复汇总的宏Application.ScreenUpdating = TrueEnd SubSub bcfhz0204()'不重复汇总'蓝桥玄霜'2007-2-4Dim b As Integer, x As Integer, y As Integer, aa As Integer, yyy As IntegerDim minc As RangeDim rng1 As Range, a As RangeDim n1 As Integer, nn As Integer, Myrow1 As IntegerDim pp, pp1On Error Resume NextSheets("汇总").SelectRange("a1").SelectMyrow1 = Selection.CurrentRegion.Rows.Count 'A列数据的行数Set minc = Range("b2:b" & Myrow1)Set rng1 = Range("m2:m" & Myrow1)Range("m2").Select '求重复值个数的辅助列公式Selection.Formula ="=if((countif(minc,$b2)>1)*(match($b2,minc,0)=row($a1)),count(m$1:m1)+1,"""")"Selection.AutoFill Destination:=rng1, Type:=xlFillDefault '公式往下复制b = Application.WorksheetFunction.Max(rng1)Range("n2").Select '求重复值的辅助列公式Selection.Formula ="=if(iserror(index(minc,match(row(b1),m$2:m$ 65536,0))),"""",index(minc,match(row(b1),m$2:m$65536,0)))"Selection.AutoFill Destination:=Range("n2:n" & b + 1), Type:=xlFillDefault '公式往下复制Range("n2:n" & b + 1).Select'以“选择性粘贴”的“数值”粘贴n,m列,因为删除一行后,公式会重新计算' Selection.CopyRange("n2").SelectSelection.PasteSpecial Paste:=xlValuesrng1.SelectSelection.CopyRange("m2").SelectSelection.PasteSpecial Paste:=xlValuesFor x = 2 To b + 1Set a = Range("n" & x)aa = Application.WorksheetFunction.CountIf(minc, a) '计算重复值的个数Range("o" & x).Value = aann = aaRange("p1") = aRange("p2").Select '重复值所在行数的数组公式Selection.FormulaArray = "=if($p$1<>"""",if(iserror(small(if(minc=$p$1,row(minc),""""),row(1:1))),"""",small(if(minc= $p$1,row(minc),""""),row(1:1))))"Selection.AutoFillDestination:=Range("p2:p" & aa + 1), Type:=xlFillDefaultRange("p2:p" & aa + 1).SelectSelection.CopyRange("p2").SelectSelection.PasteSpecial Paste:=xlValues'以“选择性粘贴”的“数值”粘贴去除公式影响For y = 2 To nn '在重复值里循环比较pp = Range("p" & y).Value '将行数赋给变量ppFor yy = y + 1 To nn + 1pp1 = Range("p" & yy).Value '将行数赋给变量pp1If pp1 = "" ThenGoTo 100ElseEnd IfIf Cells(pp, 2) = Cells(pp1, 2) And Cells(pp, 3) = Cells(pp1, 3) And Cells(pp, 4) = Cells(pp1, 4) ThenCells(pp, 5) = Cells(pp, 5) + Cells(pp1, 5) '汇总部分Range(Cells(pp1, 1), Cells(pp1, 5)).Delete shift:=xlUp'删除多余的行For yyy = yy + 1 To nn + 1Range("p" & yyy) = Range("p" & yyy) - 1Next yyyRange("p" & yy).Delete shift:=xlUpyy = yy - 1: nn = nn - 1ElseEnd IfNext yy100: Next ynn = aaRange("p1:P" & aa + 1).ClearContents '清除辅助列数据200: Next xRange("m1").SelectSelection.CurrentRegion.ClearContents'清除辅助列数据Range("A1").Select '以下在A列加上序号n1 = Selection.CurrentRegion.Rows.CountRange("A2").SelectActiveCell.FormulaR1C1 = "1"Range("A3").SelectActiveCell.FormulaR1C1 = "2"Range("A2:A3").SelectSelection.AutoFillDestination:=Range("A2:A" & n1), Type:=xlFillDefaultRange("A2").SelectEnd Sub4,工作表的名称和index号Sub Sheetsname()‘见上例的xls‘2007-2-2Dim Sht As WorksheetSheets("使用型号表").Activaten = 2For Each Sht In ActiveWorkbook.Worksheets If <> "汇总" And <> "使用型号表" ThenActiveSheet.Range("k" & n) = ActiveSheet.Range("l" & n) = Sht.Indexn = n + 1ElseEnd IfNext ShtEnd Sub5,重复值加色Sub 重复值加色()'重复值加色.xls' 蓝桥玄霜2007-2-2'表格中有重复值公式'Dim rng1 As Range, data As RangeDim b As IntegerSet rng1 = Range("n2:n117") ‘重复值区域b = Application.WorksheetFunction.Max(rng1)‘重复值个数Range("B2:B117").SelectSelection.FormatConditions.DeleteFor X = 2 To b + 1 ‘用查找Selection.FormatConditions.AddType:=xlCellValue, Operator:=xlEqual, _Formula1:="=$M$" & XSelection.FormatConditions(X - 1).Interior.ColorIndex = 3Next XEnd Sub6,统计Sub tongji()‘车次统计,见统计月报1.xls‘Excel论坛Dim Myrow1 As Integer, Myrow2 As IntegerDim Sht As Worksheet, Sht1 As WorksheetApplication.ScreenUpdating = FalseOn Error Resume NextFor Each Sht In ActiveWorkbook.Worksheets 'AB列空格填充If <> "月计" ThenSheets().SelectRange("a1").SelectMyrow1 = [a65536].End(xlUp).Row 'A列最下面一行的行数,中间有空格也行Set rng1 = Range(Cells(4, 1), Cells(Myrow1 - 1, 2))rng1.SelectIfIsError(Selection.SpecialCells(xlCellTypeBlanks )) ThenGoTo 100ElseSelection.SpecialCells(xlCellTypeBlanks).SelectRange("A5").ActivateSelection.FormulaR1C1 = "=R[-1]C"Range("A4").Selectrng1.SelectSelection.CopySelection.PasteSpecial Paste:=xlValuesApplication.CutCopyMode = FalseRange("A4").SelectEnd IfElseEnd If100: Next ShtSheets("月计").SelectSet Sht1 = Sheets("月计")Range("a1").SelectMyrow1 = [a65536].End(xlUp).RowMyrow1 = Myrow1 - 1Range(Cells(4, 4), Cells(Myrow1, 11)).ClearContentsFor x = 4 To Myrow1fa = Range("a" & x).Valuedao = Range("b" & x).ValueIf fa = "" And dao = "" ThenGoTo 200ElseEnd IfFor n = 1 To 10Sheets(n).ActivateRange("a1").SelectMyrow2 = [a65536].End(xlUp).RowMyrow2 = Myrow2 - 1For y = 4 To Myrow2fa1 = Range("a" & y).Valuedao1 = Range("b" & y).ValueIf fa = fa1 And dao = dao1 ThenSht1.Range("d" & x) = Sht1.Range("d" & x) + Range("d" & y) '汇总Sht1.Range("e" & x) = Sht1.Range("e" & x) + Range("e" & y)Sht1.Range("f" & x) = Sht1.Range("f" & x) + Range("f" & y)Sht1.Range("g" & x) = Sht1.Range("g" & x) + Range("g" & y)Sht1.Range("h" & x) = Sht1.Range("h" & x) + Range("h" & y)Sht1.Range("i" & x) = Sht1.Range("i" & x) + Range("i" & y)Sht1.Range("j" & x) = Sht1.Range("j" & x) + Range("j" & y)Sht1.Range("k" & x) = Sht1.Range("k" & x) + Range("k" & y)ElseEnd IfNext yNext nSheets("月计").Select200: Next xSheets("月计").SelectApplication.ScreenUpdating = TrueEnd Sub7,最大或最小‘Excel论坛‘最大或最小.xlsSub MaxMin()Dim rng1 As RangeDim x As Integer, b As IntegerDim a(12)Range("a14").Value = ""For x = 1 To 12Cells(2, x + 3).SelectSet rng1 = Cells(2, x + 3)a(x) = Selection.Valueb = Application.WorksheetFunction.Find("/", rng1)a(x) = Left(rng1, b)a(x) = Val(a(x))Next xMymax = Application.WorksheetFunction.Max(a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12))Mymin = Application.WorksheetFunction.Min(a(1), a(2), a(3), a(4), a(5), a(6), a(7), a(8), a(9), a(10), a(11), a(12))If a(1) = Mymax ThenRange("a14").FormulaR1C1 = "最大" ElseIf a(1) = Mymin ThenRange("a14").FormulaR1C1 = "最小"ElseEnd IfEnd IfEnd Sub8,最后一记录(定义名称)Sub zhytjl0206()'最后一次的那条记录.xls'Shizx98'2007-2-6‘/dispbbs.asp?BoardID=3&ID=219549&replyID=&skin=0Dim b As Integer, x As Integer, y As Integer, aa As IntegerDim minc As RangeDim rng1 As Range, a As RangeDim nn As Integer, Myrow1 As IntegerDim pp, pp1Dim Sht1 As Worksheet, Sht2 As WorksheetOn Error Resume NextApplication.ScreenUpdating = FalseSet Sht1 = Sheets(1): Set Sht2 = Sheets(3)Sht1.Activates.AddName:="data1", RefersToR1C1:= _"=OFFSET(Sheet1!R1C1,1,,COUNTA(Sheet1!R2C1:R65535C1),)"Range("n1").SelectSelection.CurrentRegion.ClearContents'清除辅助列数据Sheet2.ActivateRange("a1").SelectSelection.CurrentRegion.ClearContents'清除上次数据Sht1.Range("a1:g1").Copy Sheet2.[a1]Sht1.ActivateRange("a1").SelectMyrow1 = Selection.CurrentRegion.Rows.Count 'A 列数据的行数Set minc = Range("a2:a" & Myrow1)Set rng1 = Range("n2:n" & Myrow1)BcfzGS '转求不重复值宏For x = 2 To b + 1FuzulieGS '转辅助列公式宏Range("p2:p" & aa + 1).SelectSelection.Copy Sht2.Range("a2")Application.CutCopyMode = FalseSht1.Activatepp = Sht1.Range("p" & 2).Value '将行数赋给变量ppRange(Cells(pp, 7), Cells(pp + aa - 1, 7)).SelectSelection.Copy Sht2.Range("b2") '时间复制到表2Sht2.ActivateCells(2, 3).SelectSelection.Formula = "=datevalue(rc[-1])+timevalue(rc[-1])" '时间值公式If aa > 1 ThenSelection.AutoFill Destination:=Range("c2:c" & aa + 1), Type:=xlFillDefault '公式往下复制Range("a2:c" & aa + 1).SelectSelection.SortKey1:=Range("C2"), Order1:=xlDescending, Header:=xlNo, _OrderCustom:=2, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinElseEnd Ifpp1 = Range("a2").ValueSheets(2).SelectFor xx = 1 To 7Cells(x, xx) = Sht1.Cells(pp1, xx)Next xxnn = aaSht1.ActivateRange("p1:P" & aa + 1).ClearContents '清除辅助列数据Sht2.ActivateRange(Cells(1, 1), Cells(aa + 1,3)).ClearContents200: Next xSht1.ActivateRange("m1").SelectSelection.CurrentRegion.ClearContents'清除辅助列数据Sheets(2).ActivateRange("a1:g" & b + 1).Select ‘排序Selection.Sort Key1:=Range("a1"), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=2, MatchCase:=False,Orientation:=xlTopToBottom, SortMethod _:=xlPinYin加边框'转加边框宏Range("A1").SelectApplication.ScreenUpdating = TrueEnd SubSub BcfzGS()'不重复值公式Range("n2").Select '辅助列公式求有几个不重复值Selection.FormulaArray = "=index(a:a,min(if(countif(n$1:n1,data1),65536, row(data1))))&"""""Selection.AutoFill Destination:=rng1, Type:=xlFillDefault '公式往下复制b = Application.WorksheetFunction.CountIf(rng1, "4*")End SubSub FuzulieGS()'某个不重复值的个数和所在行数的数组公式Sht1.SelectSet a = Range("n" & x)aa = Application.WorksheetFunction.CountIf(minc,a) '计算某个不重复值的个数Range("o" & x).Value = aann = aaRange("p1") = Val(a)Range("p2").Select '某个不重复值所在行数的数组公式Selection.FormulaArray = "=if($p$1<>"""",if(iserror(small(if(minc=$p$1, row(minc),""""),row(1:1))),"""",small(if(minc= $p$1,row(minc),""""),row(1:1))))"If aa > 1 ThenSelection.AutoFill Destination:=Range("p2:p" & aa + 1), Type:=xlFillDefaultElseEnd IfEnd SubSub 加边框()BorderVars(1) = xlEdgeLeftBorderVars(2) = xlEdgeTopBorderVars(3) = xlEdgeBottomBorderVars(4) = xlEdgeRightBorderVars(5) = xlInsideVerticalBorderVars(6) = xlInsideHorizontalRange("a1:g" & b + 1).SelectSelection.Borders(xlDiagonalDown).LineStyle = xlNoneSelection.Borders(xlDiagonalUp).LineStyle = xlNoneFor x = 1 To 6WithSelection.Borders(BorderVars(x)).LineStyle = xlContinuous.Weight = xlThin.ColorIndex = xlAutomaticEnd WithNextEnd SubSub zhytjl0215()'最后一条记录0215.xls'蓝桥玄霜,'2007-2-15'根据czzqbµ的数组公式修改'在表1的H列加了时间转换公式Dim b As Integer, x As Integer, y As IntegerDim minc As RangeDim rng1 As Range, a As RangeDim Myrow1 As IntegerDim Sht1 As Worksheet, Sht2 As WorksheetDim BorderVars(6) As VariantOn Error Resume NextApplication.ScreenUpdating = FalseSet Sht1 = Sheets(1): Set Sht2 = Sheets(2)Sht1.Activate ‘定义动态区域名称s.AddName:="data1", RefersToR1C1:= _"=OFFSET(Sheet1!R1C1,1,,COUNTA(Sheet1!R 2C1:R65535C1),)"s.AddName:="shiji", RefersToR1C1:= _"=OFFSET(Sheet1!R1C8,1,,COUNTA(Sheet1!R 2C8:R65535C8),)"Range("n1").SelectSelection.CurrentRegion.ClearContentsSheet2.ActivateRange("a1").SelectSelection.CurrentRegion.ClearContentsSht1.Range("a1:g1").Copy Sheet2.[a1]Sht1.ActivateRange("a1").SelectMyrow1 = Selection.CurrentRegion.Rows.CountSet minc = Range("a2:a" & Myrow1)Set rng1 = Range("n2:n" & Myrow1)Range("n2").Select '¸'辅助列公式求不重复值Selection.FormulaArray = "=index(a:a,min(if(countif(n$1:n1,minc),65536,r ow(minc))))&"""""Selection.AutoFill Destination:=rng1, Type:=xlFillDefault‘不重复值的个数b = Application.WorksheetFunction.CountIf(rng1, "4*")Range("n2:n" & b + 1).SelectSelection.CopySelection.PasteSpecial Paste:=xlValuesRange(Cells(2, 14), Cells(1 + b, 14)).Copy Sht2.Cells(2, 1)Sht2.ActivateSht2.[b2].SelectSelection.FormulaR1C1 = "=index(mydata,match(value(rc[-1]),data1,0),2)"Sht2.[c2].SelectSelection.Formula = "=index(mydata,match(value(rc[-2]),data1,0),3) "Sht2.[d2].SelectSelection.Formula = "=index(mydata,match(value(rc[-3]),data1,0),4) "Sht2.[e2].SelectSelection.Formula = "=index(mydata,match(value(rc[-4]),data1,0),5) "Sht2.[f2].SelectSelection.Formula = "=index(mydata,match(value(rc[-5]),data1,0),6) "Sht2.[g2].SelectSelection.FormulaArray = "=max(if(data1=value(rc[-6]),shiji))" ‘求时间最后的数组公式,by:czzqbRange("b2:g2").SelectSelection.AutoFillDestination:=Range("b2:g" & b + 1), Type:=xlFillDefaultSheets(2).ActivateRange("a1:g" & b + 1).Select ‘排序Selection.Sort Key1:=Range("a1"), Order1:=xlAscending, Header:=xlGuess, _OrderCustom:=2, MatchCase:=False,Orientation:=xlTopToBottom, SortMethod _:=xlPinYinSelection.Borders(xlDiagonalDown).LineStyle = xlNoneSelection.Borders(xlDiagonalUp).LineStyle = xlNone‘加边框BorderVars(1) = xlEdgeLeftBorderVars(2) = xlEdgeTopBorderVars(3) = xlEdgeBottomBorderVars(4) = xlEdgeRightBorderVars(5) = xlInsideVerticalBorderVars(6) = xlInsideHorizontalFor x = 1 To 6WithSelection.Borders(BorderVars(x)).LineStyle = xlContinuous.Weight = xlThin.ColorIndex = xlAutomaticEnd WithNextrng1.ClearContentsRange("A1").SelectApplication.ScreenUpdating = TrueEnd Sub9,大港表格转换‘大港.xlsSub dagang()Dim Sht1 As Worksheet, Sht2 As WorksheetDim x As Integer, y As IntegerDim n As Integer, m As IntegerDim n1 As IntegerApplication.ScreenUpdating = FalseSet Sht1 = ActiveWorkbook.Sheets(1) Set Sht2 = Sheets(2)Sht2.Activate ‘清除原有数据Range("a1").SelectActiveCell.CurrentRegion.ClearContents With Sht2 ‘表格的表头赋值.Cells(1, 1) = "编号".Cells(1, 2) = "姓名".Cells(1, 3) = "性别".Cells(1, 4) = "年龄".Cells(1, 5) = "职务".Cells(1, 6) = "家庭地址".Cells(1, 7) = "户口性质及所在地".Cells(1, 8) = "身体状况".Cells(1, 9) = "备注"End Withn = 2: m = 2For x = 1 To 9For y = 3 To 168 Step 11 ‘两分表间隔11行If Sht1.Cells(y, m) <> "" Then‘如果姓名值为空,则程序结束With Sht2.Cells(n, 2) = Sht1.Cells(y, m).Cells(n, 3) = Sht1.Cells(y, m + 2).Cells(n, 4) = Sht1.Cells(y, m + 4).Cells(n, 5) = Sht1.Cells(y, m + 6).Cells(n, 6) = Sht1.Cells(y + 1, m).Cells(n, 7) = Sht1.Cells(y + 1, m + 4).Cells(n, 8) = Sht1.Cells(y + 3, m).Cells(n, 9) = Sht1.Cells(y + 4, m + 4)End Withn = n + 1ElseGoTo 100End IfNext y'Stopy = 3: m = m + 9Next x100: Range("a1").Select ‘加编号n1 = [b65536].End(xlUp).RowRange("A2").SelectActiveCell.FormulaR1C1 = "1"Range("A3").SelectActiveCell.FormulaR1C1 = "2"Range("A2:A3").SelectSelection.AutoFillDestination:=Range("A2:A" & n1), Type:=xlFillDefaultRange("A2").SelectApplication.ScreenUpdating = TrueEnd Sub10,筛选尾数/dispbbs.asp?boardid= 1&id=220360&star=1#220360Sub weisu()'筛选尾数0209.xls'by:Shizx98'2007-2-9Dim Myrow1 As IntegerDim Mymax As IntegerDim x%, y%, yy%, nn%Application.ScreenUpdating = FalseRange("n1:s30").ClearContents ‘清除原有数据Range("a1").SelectSelection.CurrentRegion.Copy Range("b1") Myrow1 = Selection.CurrentRegion.Rows.Count Range("c1").SelectSelection.Formula = "=right(rc[-1],4)" ‘取四位尾数Range("g1").SelectSelection.Formula = "=left(rc[-4],1)" ‘分成四列Range("f1").SelectSelection.Formula = "=mid(rc[-3],2,1)"Range("e1").SelectSelection.Formula = "=mid(rc[-2],3,1)"Range("d1").SelectSelection.Formula = "=right(rc[-1],1)"Range("c1:g1").SelectSelection.AutoFill Destination:=Range("c1:g" & Myrow1), Type:=xlFillDefault ‘公式复制For yy = 2 To 7Select Case yyCase 2Range("h1").SelectSelection.Formula = "=if(and(rc[-4]=rc[-3],rc[-3]=rc[-2],rc[-2]=rc[-1] ),1,"""")" ‘判断四位尾数相等Selection.AutoFillDestination:=Range("h1:h" & Myrow1), Type:=xlFillDefaultRange("n1") = "尾数四位相同" 'AAAACase 3Range("i1").SelectSelection.Formula = "=if(and(rc[-4]=rc[-3],rc[-3]=rc[-5],rc[-3]<>rc[-2 ]),1,"""")"Selection.AutoFillDestination:=Range("i1:i" & Myrow1), Type:=xlFillDefault ‘判断三位尾数相等Range("o1") = "尾数三位相同" 'BAAACase 4Range("j1").SelectSelection.Formula = "=if(and(rc[-6]=rc[-5],rc[-5]<>rc[-4],rc[-4]<>rc[-3]),1,"""")" ‘判断两位尾数相等Selection.AutoFillDestination:=Range("j1:j" & Myrow1), Type:=xlFillDefaultRange("p1") = "尾数两位相同" 'AACase 5Range("k1").SelectSelection.Formula = "=if(and(rc[-6]=rc[-7],rc[-4]=rc[-5],rc[-5]<>rc[-6 ]),1,"""")" 'AABBSelection.AutoFillDestination:=Range("k1:k" & Myrow1), Type:=xlFillDefaultRange("q1") = "尾数AABB"Case 6Range("l1").SelectSelection.Formula = "=if(and(rc[-6]=rc[-7],rc[-7]<>rc[-8],rc[-5]=rc[-6 ]),1,"""")" 'AAABSelection.AutoFillDestination:=Range("l1:l" & Myrow1), Type:=xlFillDefaultRange("r1") = "尾数AAAB"Case 7Range("m1").SelectSelection.Formula = "=if(and(rc[-9]=rc[-7],rc[-8]=rc[-6],rc[-7]<>rc[-8]),1,"""")" 'ABABSelection.AutoFillDestination:=Range("m1:m" & Myrow1), Type:=xlFillDefaultRange("s1") = "尾数ABAB"End SelectNext yyRange("c1:m" & Myrow1).SelectSelection.CopyRange("c1").PasteSpecial Paste:=xlValues '以“选择性粘贴”的“数值”粘贴去除公式影响500: nn = Myrow1For y = 1 To 6n = 2Mymax = Application.WorksheetFunction.Sum(Range(Cel ls(1, y + 7), Cells(Myrow1, y + 7)))If Mymax = 0 ThenGoTo 1000ElseEnd IfFor x = 1 To nnIf Cells(x, y + 7) = 1 ThenCells(n, y + 13) = Cells(x, 2)Range(Cells(x, 2), Cells(x, 13)).Delete shift:=xlUp ‘有相同的就复制后删除B列的一个,连一行公式数据也删除n = n + 1: nn = nn - 1ElseEnd IfNext x'Stop1000: Next yRange(Cells(1, 3), Cells(Myrow1, 13)).ClearContentsColumns("C:M").SelectSelection.EntireColumn.Hidden = True‘隐藏C-M列Application.ScreenUpdating = TrueEnd Sub11,对比数据‘/dispbbs.asp?boardID =3&ID=220430&page=1&px=0‘数据比对.xlsSub duibisujue()Dim Myrow1 As IntegerDim x%, y%, xx%, nn%Application.ScreenUpdating = FalseRange("a1").SelectMyrow1 = Selection.CurrentRegion.Rows.Count nn = Myrow1: n = 1For x = 1 To nnxx = 0If Cells(x, 1) = "" ThenGoTo 100。
Excel可以使用的两百多个常用宏
宏文件集▲打开全部隐藏工作表返回Sub 打开全部隐藏工作表()Dim i As IntegerFor i = 1 To Sheets.CountSheets(i).Visible = TrueNext iEnd Sub▲循环宏返回Sub 循环()AAA = Range("C2")Dim i As LongDim times As Longtimes = AAA'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)For i = 1 To timesCall 过滤一行If Range("完成标志") = "完成" Then Exit For '如果名为'完成标志'的命名单元的值等于'完成',则退出循环,如果一开始就等于'完成',则 'If Sheets("传送参数").Range("A" & i).Text = "完成" Then Exit For '如果某列出现"完成"内容则退出循环Next iEnd Sub▲录制宏时调用“停止录制”工具栏返回Sub 录制宏时调用停止录制工具栏()mandBars("Stop Recording").Visible = TrueEnd Sub▲高级筛选5列不重复数据至指定表返回Sub 高级筛选5列不重复数据至Sheet2()Sheets("Sheet2").Range("A1:E65536") = "" '清除Sheet2的A:D列Range("A1:E65536").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _"A1"), Unique:=TrueSheet2.Columns("A:E").Sort Key1:=Sheet2.Range("A2"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _:=xlPinYinEnd Sub▲双击单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$4"Call 宏1Cancel = TrueCase "$B$4"Call 宏2Cancel = TrueCase "$C$4"Call 宏3Cancel = TrueCase "$E$4"Call 宏4Cancel = TrueEnd SelectEnd Sub▲双击指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9", "C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲进入单元执行宏(工作表代码)返回'以单元格进入代替按钮对象调用宏If Range("$A$1") = "关闭" Then Exit SubSelect Case Target.AddressCase "$A$5" '单元地址(Target.Address),或命名单元名字()Call 宏1Case "$B$5"Call 宏2Case "$C$5"Call 宏3End SelectEnd Sub▲进入指定区域单元执行宏(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("$A$1") = "关闭" Then Exit SubIf Not Application.Intersect(Target, Range("A4:A9","C4:C9")) Is Nothing Then Call 打开隐藏表End Sub▲在多个宏中依次循环执行一个(控件按钮代码)返回Private Sub CommandButton1_Click()Static RunMacro As IntegerSelect Case RunMacroCase 0宏1RunMacro = 1Case 1宏2RunMacro = 2Case 2宏3RunMacro = 0End SelectEnd Sub▲在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()With CommandButton1If .Caption = "保护工作表" ThenCall 保护工作表.Caption = "取消工作表保护"Exit SubEnd IfIf .Caption = "取消工作表保护" ThenCall 取消工作表保护.Caption = "保护工作表"Exit SubEnd IfEnd WithEnd Sub▲在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)返回Option ExplicitPrivate Sub CommandButton1_Click()With CommandButton1If .Caption = "宏1" ThenCall 宏1.Caption = "宏2"Exit SubEnd IfIf .Caption = "宏2" ThenCall 宏2.Caption = "宏3"Exit SubEnd IfIf .Caption = "宏3" ThenCall 宏3.Caption = "宏1"End IfEnd WithEnd Sub▲根据A1单元文本隐藏/显示按钮(控件按钮代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Range("A1") > 2 ThenCommandButton1.Visible = 1ElseCommandButton1.Visible = 0End IfEnd SubPrivate Sub CommandButton1_Click()重排窗口End Sub▲当前单元返回按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()ActiveCell = CommandButton1.CaptionEnd Sub▲当前单元内容返回到按钮名称(控件按钮代码)返回Private Sub CommandButton1_Click()CommandButton1.Caption = ActiveCellEnd Sub▲奇偶页分别打印返回Sub 奇偶页分别打印()Dim i%, Ps%Ps = ExecuteExcel4Macro("GET.DOCUMENT(50)") '总页数MsgBox "现在打印奇数页,按确定开始."ActiveSheet.PrintOut from:=i, To:=iNext iMsgBox "现在打印偶数页,按确定开始."For i = 2 To Ps Step 2ActiveSheet.PrintOut from:=i, To:=iNext iEnd Sub▲自动打印多工作表第一页返回Sub 自动打印多工作表第一页()Dim sh As IntegerDim xDim yDim syDim syzx = InputBox("请输入起始工作表名字:")sy = InputBox("请输入结束工作表名字:")y = Sheets(x).Indexsyz = Sheets(sy).IndexFor sh = y To syzSheets(sh).SelectSheets(sh).PrintOut from:=1, To:=1Next shEnd Sub▲查找A列文本循环插入分页符返回Sub 循环插入分页符()' Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容Dim i As LongDim times As Longtimes = Application.WorksheetFunction.CountIf(Sheet1.Range("a:a"), "分页")'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647)Call 插入分页符Next iEnd SubSub 插入分页符()Cells.Find(What:="分页", After:=ActiveCell, LookIn:=xlValues, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .ActivateActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCellEnd SubSub 取消原分页()Cells.SelectActiveSheet.ResetAllPageBreaksEnd Sub▲将A列最后数据行以上的所有B列图片大小调整为所在单元大小返回Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小()Dim Pic As Picture, i&i = [A65536].End(xlUp).RowFor Each Pic In Sheet1.PicturesIf Not Application.Intersect(Pic.TopLeftCell, Range("B1:B" & i)) Is Nothing Then Pic.Top = Pic.TopLeftCell.TopPic.Left = Pic.TopLeftCell.LeftPic.Height = Pic.TopLeftCell.HeightPic.Width = Pic.TopLeftCell.WidthEnd IfNextEnd Sub▲返回光标所在行数返回Sub 返回光标所在行数()x = ActiveCell.RowEnd Sub▲在A1返回当前选中单元格数量返回Sub 在A1返回当前选中单元格数量()[A1] = Selection.CountEnd Sub▲返回当前工作簿中工作表数量返回Sub 返回当前工作簿中工作表数量()t = Application.Sheets.CountMsgBox tEnd Sub▲返回光标选择区域的行数和列数返回Sub 返回光标选择区域的行数和列数()x = Selection.Rows.County = Selection.Columns.CountRange("A1") = xRange("A2") = yEnd Sub▲工作表中包含数据的最大行数返回Sub 包含数据的最大行数()n = Cells.Find("*", , , , 1, 2).RowMsgBox nEnd Sub▲返回A列数据的最大行数返回Sub 返回A列数据的最大行数()n = Range("a65536").End(xlUp).RowRange("B1") = nEnd SubSub 将所选区域文本插入新建文本框()For Each rag In Selectionn = n & rag.Value & Chr(10)NextActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left + ActiveCell.Width, ActiveCell.Top + ActiveCell.Selection.Characters.Text = "问题:" & nWith Selection.Characters(Start:=1, Length:=3).Font.Name = "黑体".FontStyle = "常规".Size = 12End WithEnd Sub▲批量插入地址批注返回Sub 批量插入地址批注()On Error Resume NextDim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Deleter.AddCommentment.Visible = Falsement.Text Text:="本单元格:" & r.Address & " of " & Selection.AddressNextEnd IfEnd Sub▲批量插入统一批注返回Sub 批量插入统一批注()Dim r As Range, msg As Stringmsg = InputBox("请输入欲批量插入的批注", "提示", "随便输点什么吧")If Selection.Cells.Count > 0 ThenFor Each r In Selectionment.Visible = Falsement.Text Text:=msgNextEnd IfEnd Sub▲以A1单元内容批量插入批注返回Sub 以A1单元内容批量插入批注()Dim r As RangeIf Selection.Cells.Count > 0 ThenFor Each r In Selectionr.AddCommentment.Visible = Falsement.Text Text:=[a1].TextNextEnd IfEnd Sub▲不连续区域插入当前文件名和表名及地址返回Sub 批量插入当前文件名和表名及地址()For Each mycell In Selectionmycell.FormulaR1C1 = "[" + + "]" + + "!" + mycell.Address NextEnd Sub▲不连续区域录入当前单元地址返回Sub 区域录入当前单元地址()For Each mycell In Selectionmycell.FormulaR1C1 = mycell.AddressNextEnd Sub▲连续区域录入当前单元地址返回Selection = "=ADDRESS(ROW(),COLUMN(),4,1)"Selection.CopySelection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _:=False, Transpose:=FalseEnd Sub▲返回当前单元地址返回Sub 返回当前单元地址()d = ActiveCell.Address[A1] = dEnd Sub▲不连续区域录入当前日期返回Sub 区域录入当前日期()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d")End Sub▲不连续区域录入当前数字日期返回Sub 区域录入当前数字日期()Selection.FormulaR1C1 = Format(Now(), "yyyymmdd")End Sub▲不连续区域录入当前日期和时间返回Sub 区域录入当前日期和时间()Selection.FormulaR1C1 = Format(Now(), "yyyy-m-d h:mm:ss")End Sub▲不连续区域录入对勾返回Sub 批量录入对勾()Selection.FormulaR1C1 = "√"End Sub▲不连续区域录入当前文件名返回Sub 批量录入当前文件名()Selection.FormulaR1C1 = End Sub▲不连续区域添加文本返回Sub 批量添加文本()Dim s As RangeFor Each s In Selections = s & "文本内容"NextEnd Sub▲不连续区域插入文本返回Sub 批量插入文本()Dim s As RangeFor Each s In Selections = "文本内容" & sNextEnd Sub▲从指定位置向下同时录入多单元指定内容返回Sub 从指定位置向下同时录入多单元指定内容()Dim arrarr = Array("1", "2", "13", "25", "46", "12", "0", "20")[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr)End Sub▲按aa工作表A列的内容排列工作表标签顺序返回Sub 按aa工作表A列的内容排列工作表标签顺序()Dim I%, str1$I = 1Sheets("aa").SelectDo While Cells(I, 1).Value <> ""str1 = Trim(Cells(I, 1).Value)Sheets(str1).SelectSheets(str1).Move after:=Sheets(I)I = I + 1Sheets("aa").SelectLoopEnd Sub▲以A1单元文本作表名插入工作表返回Sub 以A1单元文本作表名插入工作表()Dim nm As Stringnm = [a1]Sheets.Add = nmEnd Sub▲删除全部未选定工作表返回Sub 删除全部未选定工作表()Dim sht As Worksheet, n As Integer, iFlag As BooleanDim ShtName() As Stringn = ActiveWindow.SelectedSheets.CountReDim ShtName(1 To n)n = 1For Each sht In ActiveWindow.SelectedSheetsShtName(n) = n = n + 1NextApplication.DisplayAlerts = FalseFor Each sht In SheetsiFlag = FalseFor i = 1 To n - 1If ShtName(i) = TheniFlag = TrueExit ForEnd IfNextIf Not iFlag Then sht.DeleteNextApplication.DisplayAlerts = TrueEnd Sub▲工作表标签排序返回Sub 工作表标签排序()Dim i As Long, j As Long, nums As Long, msg As Longmsg = MsgBox("工作表按升序排列请选 '是[Y]'. " & vbCrLf & vbCrLf & "工作表按降序排列请选 '否[N]'", vbYesNoCancel, "工作表排序") If msg = vbCancel Then Exit Subnums = Sheets.CountIf msg = vbYes Then 'Sort ascendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) < UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iElse 'Sort descendingFor i = 1 To numsFor j = i To numsIf UCase(Sheets(j).Name) > UCase(Sheets(i).Name) ThenSheets(j).Move Before:=Sheets(i)End IfNext jNext iEnd IfEnd Sub▲定义指定工作表标签颜色返回Sub 定义指定工作表标签颜色()Sheets("Sheet1").Tab.ColorIndex = 46End Sub▲在目录表建立本工作簿中各表链接目录返回Sub 在目录表建立本工作簿中各表链接目录()Dim s%, Rng As RangeOn Error Resume NextSheets("目录").ActivateIf Err = 0 ThenSheets("目录").UsedRange.DeleteElseSheets.Add = "目录"End IfFor i = 1 To Sheets.CountIf Sheets(i).Name <> "目录" Thens = s + 1Set Rng = Sheets("目录").Cells(((s - 1) Mod 20) + 1, (s - 1) \ 20 + 1 + 1)Rng = Format(s, " 0") & ". " & Sheets(i).NameActiveSheet.Hyperlinks.Add Rng, "#" & Sheets(i).Name & "!A1", ScreenTip:=Sheets(i).Name End IfNextSheets("目录").Range("b:iv").EntireColumn.ColumnWidth = 20End Sub▲建立工作表文本目录返回Sub 建立工作表文本目录()Sheets.Add before:=Sheets(1)Sheets(1).Name = "目录"For i = 2 To Sheets.CountCells(i - 1, 1) = Sheets(i).Name'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), "#" & Sheets(i).Name & "!A1" '添加超链接NextEnd Sub▲查另一文件的全部表名返回Sub 查另一文件的全部表名()On Error Resume NextDim i%Dim sh As WorksheetApplication.ScreenUpdating = FalseWorkbooks.Open Filename:=ThisWorkbook.Path & "\2.xls"Windows("1.xls").Activate '当前文件名称Sheets("Sheet1").Select '当前表名称i = 1 '将表名称返回到第1行For Each sh In Workbooks("2.xls").WorksheetsCells(i, 1) = '将表名称返回到第1列i = i + 1 '返回每个表名称向下移动1行Next shWindows("2.xls").Close '关闭对象文件Application.ScreenUpdating = TrueEnd Sub▲当前单元录入计算机名返回Sub 当前单元录入计算机名()Selection = Environ("COMPUTERNAME")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲当前单元录入计算机用户名返回 Sub 当前单元录入计算机用户名()Selection = Environ("Username")'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲解除全部工作表保护返回Sub 解除全部工作表保护()Dim n As IntegerFor n = 1 To Sheets.CountSheets(n).UnprotectNext nEnd Sub▲为指定工作表加指定密码保护表返回Sub 为指定工作表加指定密码保护表()Sheet10.Protect Password:="123"End Sub▲在有密码的工作表执行代码返回Sub 在有密码的工作表执行代码()Sheets("1").Unprotect Password:=123 '假定表名为“1”,密码为“123” 打开工作表Range("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True '隐藏C列空值行Sheets("1").Protect Password:=123 '重新用密码保护工作表End Sub▲执行前需要验证密码的宏(控件按钮代码)返回Private Sub CommandButton1_Click()If InputBox("请输入密码:") <> "123" Then '密码是123MsgBox "密码错误,按确定退出!", 64, "提示"Exit SubEnd IfCells(1, 1) = 10End SubSub 执行前需要验证密码的宏()If InputBox("请输入您的使用权限:", "系统提示") = 123 Then重排窗口 '要执行的宏代码或宏名称ElseMsgBox "对不起,您没有使用该宏的权限,按确定键后退出!"End IfEnd Sub▲拷贝A1公式和格式到A2返回Sub 拷贝A1公式到A2()Workbooks("临时表").Sheets("表1").Range("A1").CopyWorkbooks("临时表").Sheets("表2").Range("A2").PasteSpecialEnd Sub▲复制单元数值返回Sub 复制数值()s = Workbooks("book1").Sheets("Sheet1").Range("A1:A2")Workbooks("book2").Sheets("Sheet1").Range("A1:A2") = sEnd Sub▲插入数值条件格式返回Sub 插入数值条件格式()Selection.FormatConditions.DeleteSelection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="70"Selection.FormatConditions(1).Interior.ColorIndex = 45Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _Formula1:="55"Selection.FormatConditions(2).Interior.ColorIndex = 39Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _Formula1:="60"Selection.FormatConditions(3).Interior.ColorIndex = 34End Sub▲插入透明批注返回Sub 插入透明批注()Selection.AddCommentment.Visible = FalseDim XS As WorksheetFor i = 1 To ments.Countments(i).Text "透明批注"ments(i).Shape.Fill.Visible = msoFalseNextEnd Sub▲添加文本返回Sub 添加文本()Selection = Selection + "×" '不可在数字后添加文本'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲光标定位到指定工作表A列最后数据行下一单元返回Sub 光标定位到指定工作表A列最后数据行下一单元()a = Sheets("数据库").[a65536].End(xlUp).RowSheets("数据库").SelectRange("A" & a + 1).SelectEnd Sub▲定位选定单元格式相同的全部单元格返回Sub 定位选定单元格式相同的全部单元格()Dim FirstCell As Range, FoundCell As RangeDim AllCells As RangeWith 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.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.FormulaHiddenEnd WithSet FirstCell = edRange.Find(what:="", searchformat:=True)If FirstCell Is Nothing ThenExit SubEnd IfSet AllCells = FirstCellSet FoundCell = FirstCellDoSet FoundCell = edRange.Find(After:=FoundCell, what:="", searchformat:=True) If FoundCell Is Nothing Then Exit DoSet AllCells = Union(FoundCell, AllCells)If FoundCell.Address = FirstCell.Address Then Exit DoLoopAllCells.SelectEnd Sub▲按当前单元文本定位返回Sub 按当前单元文本定位()ABC = SelectionDim aa As RangeFor Each a In edRangeIf a Like ABC ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲按固定文本定位返回Sub 文本定位()Dim aa As RangeFor Each a In edRangeIf a Like "*合计*" ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲删除包含固定文本单元的行或列返回Sub 删除包含固定文本单元的行或列()DoCells.Find(what:="哈哈").ActivateSelection.EntireRow.Delete '删除行' Selection.EntireColumn.Delete '删除列Loop Until Cells.Find(what:="哈哈") Is NothingEnd Sub▲定位数据及区域以上的空值返回Sub 定位数据及区域以上的空值()Dim aa As RangeFor Each a In edRangeIf a Like 〈0 ThenIf aa Is Nothing ThenSet aa = a.CellsElseSet aa = Union(aa, a.Cells)End IfEnd IfNextaa.SelectEnd Sub▲右侧单元自动加5(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Application.EnableEvents = FalseTarget.Offset(0, 1) = Target + 5Application.EnableEvents = TrueEnd Sub▲当前单元加2返回Sub 当前单元加2()Selection = Selection + 2'Selection = Workbooks("临时表").Sheets("表2").Range("A1") 调用指定地址内容End Sub▲A列等于A列减B列返回Sub A列等于A列减B列()For i = 1 To 23Cells(i, 1) = Cells(i, 1) - Cells(i, 2)NextEnd Sub▲用于光标选定多区域跳转指定单元(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)a = Array([b6:b7], [e6], [h6])For i = 0 To 2If Not Application.Intersect(T, a(i)) Is Nothing Then[a1].Select: Exit ForEnd IfNextEnd Sub▲将A1单元录入的数据累加到B1单元(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)Dim t As LongIf Target.Address = "$A$1" Thent = Sheet1.Range("$B$1").ValueSheet1.Range("$B$1").Value = t + Target.ValueEnd IfEnd Sub▲在指定颜色区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim myrg As RangeFor Each myrg In TargetIf myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> "√", "√", "")NextEnd Sub▲在指定区域选择单元时添加/取消"√"(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Dim Rng As RangeIf Target.Count <= 15 ThenIf Not Application.Intersect(Target, Range("D6:D20")) Is Nothing ThenFor Each Rng In SelectionWith RngIf .Value = "" Then.Value = "√"Else.Value = ""End IfEnd WithNextEnd IfEnd IfEnd Sub▲双击指定单元,循环录入文本(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean)If T.Address <> "$A$1" Then Exit SubCancel = TrueT = IIf(T = "好", "中", IIf(T = "中", "差", "好"))End Sub双击指定单元,循环录入文本(工作表代码)Dim nums As BytePrivate Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Target.Address = "$A$1" Thennums = nums Mod 3 + 1Target = Mid("上中下", nums, 1)Target.Offset(1, 0).SelectEnd IfEnd Sub▲单元区域引用(工作表代码)返回Private Sub Worksheet_Activate()Sheet1.Range("A1:B3").Value = Sheet2.Range("A1:B3").ValueEnd Sub▲在指定区域选择单元时数值加1(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Not Application.Intersect([a1:e10], Target) Is Nothing ThenTarget = Val(Target) + 1End IfEnd Sub▲混合文本的编号返回Sub 混合文本的编号()Worksheets(1).Range("B2").Value = "北京" & (--(Mid(Worksheets(1).Range("B2"), 3, 100)) + 1) End Sub▲指定区域单元双击数据累加(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)If Not Application.Intersect([A1:Y100], Target) Is Nothing Thenoldvalue = Val(Target.Value)inputvalue = InputBox("请输入数量,按ENTER键确认!", "数值累加器")Target.Value = oldvalue + inputvalueEnd IfEnd Sub▲选择单元区域触发事件(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)If Target.Address = "$A$1:$B$2" ThenMsgBox "你选择了$A$1:$B$2单元"End IfEnd Sub▲当修改指定单元内容时自动执行宏(工作表代码)返回Private Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then重排窗口End IfEnd Sub▲被指定单元内容限制执行宏返回Sub 被指定单元限制执行宏()If Range("$A$1") = "关闭" Then Exit Sub窗口End Sub▲双击单元隐藏该行(工作表代码)返回Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)Rows(Target.Row).Hidden = TrueEnd Sub▲高亮显示行(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = 2Rows("1:2").Interior.ColorIndex = 40 '保持1至2行的颜色推荐39,22,40,Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15End Sub▲高亮显示行和列(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal Target As Range)Cells.Interior.ColorIndex = xlNoneRows(Target.Row).Interior.ColorIndex = 34Columns(Target.Column).Interior.ColorIndex = 34End Sub▲为指定工作表设置滚动范围(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)Sheet1.ScrollArea = "A1:M30"End Sub▲在指定单元记录打印和预览次数(工作簿代码)返回Private Sub Workbook_BeforePrint(Cancel As Boolean)Range("A1") = 1 + Range("A1")End Sub▲自动数字金额转大写(工作表代码)返回Private Sub Worksheet_Change(ByVal M As Range)On Error Resume Nexty = Int(Round(100 * Abs(M)) / 100)j = Round(100 * Abs(M) + 0.00001) - y * 100f = (j / 10 - Int(j / 10)) * 10A = IIf(y < 1, "", Application.Text(y, "[DBNum2]") & "元")b = IIf(j > 9.5, Application.Text(Int(j / 10), "[DBNum2]") & "角", IIf(y < 1, "", IIf(f > 1, "零", "")))c = IIf(f < 1, "整", Application.Text(Round(f, 0), "[DBNum2]") & "分")M = IIf(Abs(M) < 0.005, "", IIf(M < 0, "负" & A & b & c, A & b & c))End Sub▲将全部工作表的A1单元作为单击按钮(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)If Target.Address = "$A$1" ThenCall 宏名End IfEnd Sub▲闹钟——到指定时间执行宏(工作簿代码)返回Private Sub Workbook_Open()Application.OnTime ("11:45:00"), "提示1" '宏名字Application.OnTime ("12:00:00"), "提示2" '宏名字End Sub▲改变Excel界面标题的宏(工作簿代码)返回Private Sub Workbook_Open()Application.Caption = "春节快乐"End Sub▲在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)返回Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets("表2").Range("A1") = Target.Address(0, 0)End Sub▲B列录入数据时在A列返回记录时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Target.Column = 2 ThenTarget.Offset(, -1) = NowEnd IfEnd Sub▲当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)返回Public Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = DateTarget.Offset(, 2) = TimeEnd IfEnd IfEnd SubPublic Sub Worksheet_Change(ByVal Target As Range)If Not Application.Intersect(Target, [A1:A1000]) Is Nothing ThenIf Target.Column = 1 ThenTarget.Offset(, 1) = Format(Now(), "yyyy-mm-dd")Target.Offset(, 2) = Format(Now(), "h:mm:ss")End IfEnd IfEnd Sub▲指定单元显示光标位置内容(工作表代码)返回Private Sub Worksheet_SelectionChange(ByVal T As Range)Sheets(1).Range("A1") = SelectionEnd Sub▲每编辑一个单元保存文件返回Private Sub Worksheet_Change(ByVal Target As Range)ThisWorkbook.SaveEnd Sub▲指定允许编辑区域返回Sub 指定允许编辑区域()ActiveSheet.ScrollArea = "B8:G15"End Sub▲解除允许编辑区域限制返回Sub 解除允许编辑区域限制()ActiveSheet.ScrollArea = ""End Sub▲删除指定行返回Sub 删除指定行()Workbooks("临时表").Sheets("表2").Range("5:5").DeleteEnd Sub▲删除A列为指定内容的行返回Sub 删除A列为指定内容的行()Dim a, b As Integera = Sheet1.[a65536].End(xlUp).RowFor b = a To 2 Step -1If Cells(b, 1).Value = "删除" ThenRows(b).DeleteEnd IfNextEnd Sub▲删除A列非数字单元行返回Sub 删除A列非数字单元行()i = [a65536].End(xlUp).RowRange("A1:A" & i).SpecialCells(xlCellTypeConstants, 2).EntireRow.DeleteEnd Sub▲有条件删除当前行返回Sub 有条件删除当前行()If [A1] = 2 Or [B1] = "删除" ThenSelection.Delete Shift:=xlUpEnd IfEnd Sub▲选择下一行返回Sub 选择下一行()ActiveCell.Offset(1, 0).Rows("1:1").EntireRow.SelectEnd Sub▲选择第5行开始所有数据行返回Sub 选择第5行开始所有数据行A()Dim i%i = Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).EntireRow.RowRows("5:" & i).SelectEnd SubSub 选择第5行开始所有数据行B()Rows("5:" & Cells.Find("*", , , , 1, 2).Row).SelectEnd Sub▲选择光标或选区所在行返回Sub 选择光标或选区所在行()Selection.EntireRow.SelectEnd Sub▲选择光标或选区所在列返回Sub 选择光标或选区所在列()Selection.EntireColumn.SelectEnd Sub▲光标定位到名称指定位置返回Sub 定位()Application.Goto Range(Evaluate("名称"))End Sub▲选择名称定义的数据区返回Sub 选择名称定义的数据区()[数据区].Select '插入名称要使用INDIRECT函数'Range("数据区").Select 或者'Sheet1.Range("数据区").Select 或者End Sub▲选择到指定列的最后行返回Sub 选择到指定列的最后行()Range("C4:G" & [G65536].End(xlUp).Row).SelectEnd Sub▲将Sheet1的A列的非空值写到Sheet2的A列返回Sub 将Sheet1的A列的非空值写到Sheet2的A列()Sheet1.Columns("A:A").SpecialCells(2, 23).SpecialCells(12).Copy Sheet2.[A1]End Sub▲将名称1的数据写到名称2返回Sub Macro2()Range("位置2") = Range("位置1").ValueEnd Sub▲单元反选返回Sub 单元反选()Application.DisplayAlerts = FalseApplication.ScreenUpdating = FalseDim raddress As String, taddress As Stringraddress = Selection.Addresstaddress = edRange.AddressWith Sheets.Add.Range(taddress) = 0.Range(raddress) = "=0"raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address.DeleteEnd WithActiveSheet.Range(raddress).SelectApplication.ScreenUpdating = TrueEnd Sub▲调整选中对象中的文字返回Sub 调整选中对象中的文字()'文字居中、自动调整大小With Selection.HorizontalAlignment = xlCenter.VerticalAlignment = xlCenter.ReadingOrder = xlContext.Orientation = xlHorizontal.AutoSize = True.AddIndent = FalseEnd WithEnd Sub▲去除指定范围内的对象返回Sub 去除指定范围内的对象()Dim p As ShapeSet My = Worksheets("工作表名")For Each p In My.ShapesIf Not Application.Intersect(p.TopLeftCell, Range("范围")) Is Nothing Then p.Delete NextEnd Sub▲更新透视表数据项返回Sub DeleteMissingItems2002All()'防止数据透视表中显示无用的数据项'在 Excel 2002 或更高版本中'如果无用的数据项已经存在,'运行这个宏可以更新Dim pt As PivotTableDim ws As WorksheetFor Each ws In ActiveWorkbook.WorksheetsFor Each pt In ws.PivotTablespt.PivotCache.MissingItemsLimit = xlMissingItemsNoneNext ptNext wsEnd Sub▲将全部工作表名称写到A列返回Sub 将全部表名称写到A列()k = 1For Each Sht In SheetsCells(k + 1, 1) = '指定写入的行和列。