excel常用宏集合
更新时间:2024-05-13 17:27:01 阅读量: 综合文库 文档下载
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公式和格式到A2 57:复制单元数值 58:插入数值条件格式 59:插入透明批注 60:添加文本
61:光标定位到指定工作表A列最后数据行下一单元 62:定位选定单元格式相同的所有单元格 63:按当前单元文本定位 64:按固定文本定位
65:删除包含固定文本单元的行或列 66:定位数据及区域以上的空值 67:右侧单元自动加5(工作表代码) 68:当前单元加2
69: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 Integer
For i = 1 To Sheets.Count Sheets(i).Visible = True Next i End Sub
2:循环宏
Sub 循环()
AAA = Range(\
Dim i As Long Dim times As Long times = AAA
'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To times Call 过滤一行
If Range(\完成标志\完成\ Exit For
'假如名为'完成标志'的命名单元的值等于'完成',则退出循环,假如一开始就等于'完成',则只执行一次循环就退出
'If Sheets(\传送参数\完成\ '假如某列出现\完成\内容则退出循环
Next i End Sub
3:录制宏时调用“停止录制”工具栏
Sub 录制宏时调用停止录制工具栏()
Application.CommandBars(\End Sub
4:高级筛选5列不重复数据至指定表
Sub 高级筛选5列不重复数据至Sheet2()
Sheets(\清除Sheet2的A:D列 Range(\Action:=xlFilterCopy, CopyToRange:=Sheet2.Range( _ \
Sheet2.Columns(\Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _ :=xlPinYin End Sub
5:双击单元执行宏(工作表代码)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range(\关闭\ Exit Sub
Select Case Target.Address Case \ Call 宏1 Cancel = True Case \ Call 宏2 Cancel = True Case \ Call 宏3 Cancel = True Case \ Call 宏4 Cancel = True End Select
End Sub
6:双击指定区域单元执行宏(工作表代码)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Range(\关闭\
If Not Application.Intersect(Target, Range(\打开隐藏表 End Sub
7:进入单元执行宏(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) '以单元格进入代替按钮对象调用宏
If Range(\关闭\ Select Case Target.Address
Case \单元地址(Target.Address),或命名单元名字(Target.Name) Call 宏1 Case \ Call 宏2 Case \ Call 宏3 End Select End Sub
8:进入指定区域单元执行宏(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range(\关闭\
If Not Application.Intersect(Target, Range(\打开隐藏表 End Sub
9:在多个宏中依次循环执行一个(控件按钮代码)
Private Sub CommandButton1_Click() Static RunMacro As Integer Select Case RunMacro Case 0 宏1
RunMacro = 1 Case 1 宏2
RunMacro = 2 Case 2 宏3
RunMacro = 0 End Select End Sub
10:在两个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)
Private Sub CommandButton1_Click() With CommandButton1
If .Caption = \保护工作表\ Call 保护工作表
.Caption = \取消工作表保护\ Exit Sub End If
If .Caption = \取消工作表保护\ Call 取消工作表保护 .Caption = \保护工作表\ Exit Sub End If End With End Sub
11:在三个宏中依次循环执行一个并相应修改按钮名称(控件按钮代码)
Option Explicit
Private Sub CommandButton1_Click() With CommandButton1
If .Caption = \宏1\ Call 宏1
.Caption = \宏2\ Exit Sub End If
If .Caption = \宏2\ Call 宏2
.Caption = \宏3\ Exit Sub End If
If .Caption = \宏3\ Call 宏3
.Caption = \宏1\ Exit Sub End If End With End Sub
12:根据A1单元文本隐藏/显示按钮(控件按钮代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Range(\
CommandButton1.Visible = 1 Else
CommandButton1.Visible = 0 End If End Sub
Private Sub CommandButton1_Click() 重排窗口 End Sub
13:当前单元返回按钮名称(控件按钮代码)
Private Sub CommandButton1_Click() ActiveCell = CommandButton1.Caption End Sub
14:当前单元内容返回到按钮名称(控件按钮代码)
Private Sub CommandButton1_Click() CommandButton1.Caption = ActiveCell End Sub
15:奇偶页分别打印
Sub 奇偶页分别打印() Dim i%, Ps%
Ps = ExecuteExcel4Macro(\总页数 MsgBox \现在打印奇数页,按确定开始.\For i = 1 To Ps Step 2
ActiveSheet.PrintOut from:=i, To:=i Next i
MsgBox \现在打印偶数页,按确定开始.\For i = 2 To Ps Step 2
ActiveSheet.PrintOut from:=i, To:=i Next i End Sub
16:自动打印多工作表第一页
Sub 自动打印多工作表第一页() Dim sh As Integer Dim x Dim y Dim sy Dim syz
x = InputBox(\请输入起始工作表名字:\sy = InputBox(\请输入结束工作表名字:\y = Sheets(x).Index syz = Sheets(sy).Index For sh = y To syz Sheets(sh).Select
Sheets(sh).PrintOut from:=1, To:=1 Next sh End Sub
17:查找A列文本循环插入分页符
Sub 循环插入分页符()
' Selection = Workbooks(\临时表\表2\调用指定地址内容
Dim i As Long Dim times As Long
times = Application.WorksheetFunction.CountIf(Sheet1.Range(\分页\
'times代表循环次数,执行前把times赋值即可(不可小于1,不可大于2147483647) For i = 1 To times Call 插入分页符 Next i End Sub
Sub 插入分页符()
Cells.Find(What:=\分页\
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False) _ .Activate
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell End Sub
Sub 取消原分页() Cells.Select
ActiveSheet.ResetAllPageBreaks End Sub
18:将A列最后数据行以上的所有B列图片大小调整为所在单元大小
Sub 将A列最后数据行以上的所有B列图片大小调整为所在单元大小() Dim Pic As Picture, i&
i = [A65536].End(xlUp).Row For Each Pic In Sheet1.Pictures
If Not Application.Intersect(Pic.TopLeftCell, Range(\ Pic.Top = Pic.TopLeftCell.Top Pic.Left = Pic.TopLeftCell.Left
Pic.Height = Pic.TopLeftCell.Height Pic.Width = Pic.TopLeftCell.Width End If Next End Sub
19:返回光标所在行数
Sub 返回光标所在行数() x = ActiveCell.Row Range(\End Sub
20:在A1返回当前选中单元格数量
Sub 在A1返回当前选中单元格数量() [A1] = Selection.Count End Sub
21:返回当前工作簿中工作表数量
Sub 返回当前工作簿中工作表数量() t = Application.Sheets.Count MsgBox t End Sub
22:返回光标选择区域的行数和列数
Sub 返回光标选择区域的行数和列数() x = Selection.Rows.Count y = Selection.Columns.Count Range(\ Range(\End Sub
23:工作表中包含数据的最大行数
Sub 包含数据的最大行数()
n = Cells.Find(\ MsgBox n End Sub
24:返回A列数据的最大行数
Sub 返回A列数据的最大行数()
n = Range(\ Range(\End Sub
25:将所选区域文本插入新建文本框
Sub 将所选区域文本插入新建文本框() For Each rag In Selection
n = n & rag.Value & Chr(10) Next
ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, ActiveCell.Left ActiveCell.Width, ActiveCell.Top + ActiveCell.Height, 250#, 100).Select Selection.Characters.Text = \问题:\
With Selection.Characters(Start:=1, Length:=3).Font .Name = \黑体\ .FontStyle = \常规\
+
.Size = 12 End With End Sub
26:批量插入地址批注
Sub 批量插入地址批注() On Error Resume Next Dim r As Range
If Selection.Cells.Count > 0 Then For Each r In Selection r.Comment.Delete r.AddComment
r.Comment.Visible = False
r.Comment.Text Text:=\本单元格:\ Next End If End Sub
27:批量插入统一批注
Sub 批量插入统一批注()
Dim r As Range, msg As String
msg = InputBox(\请输入欲批量插入的批注\提示\随便输点什么吧\ If Selection.Cells.Count > 0 Then For Each r In Selection r.AddComment
r.Comment.Visible = False r.Comment.Text Text:=msg Next End If End Sub
28:以A1单元内容批量插入批注
Sub 以A1单元内容批量插入批注() Dim r As Range
If Selection.Cells.Count > 0 Then For Each r In Selection r.AddComment
r.Comment.Visible = False
r.Comment.Text Text:=[a1].Text Next End If End Sub
29:不连续区域插入当前文件名和表名及地址
Sub 批量插入当前文件名和表名及地址() For Each mycell In Selection
mycell.FormulaR1C1 = \mycell.Address Next End Sub
30:不连续区域录入当前单元地址
Sub 区域录入当前单元地址() For Each mycell In Selection
mycell.FormulaR1C1 = mycell.Address Next End Sub
31:连续区域录入当前单元地址
Sub 连续区域录入当前单元地址()
Selection = \ Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False End Sub
32:返回当前单元地址
Sub 返回当前单元地址() d = ActiveCell.Address [A1] = d End Sub
33:不连续区域录入当前日期
Sub 区域录入当前日期()
Selection.FormulaR1C1 = Format(Now(), \End Sub
34:不连续区域录入当前数字日期
Sub 区域录入当前数字日期()
Selection.FormulaR1C1 = Format(Now(), \End Sub
35:不连续区域录入当前日期和时间
Sub 区域录入当前日期和时间()
Selection.FormulaR1C1 = Format(Now(), \End Sub
36:不连续区域录入对勾
Sub 批量录入对勾()
Selection.FormulaR1C1 = \ End Sub
37:不连续区域录入当前文件名
Sub 批量录入当前文件名()
Selection.FormulaR1C1 = ThisWorkbook.Name End Sub
38:不连续区域添加文本
Sub 批量添加文本() Dim s As Range
For Each s In Selection s = s & \文本内容\ Next End Sub
39:不连续区域插入文本
Sub 批量插入文本() Dim s As Range
For Each s In Selection s = \文本内容\ Next End Sub
40:从指定位置向下同时录入多单元指定内容
Sub 从指定位置向下同时录入多单元指定内容() Dim arr
arr = Array(\
[B2].Resize(8, 1) = Application.WorksheetFunction.Transpose(arr) End Sub
41:按aa工作表A列的内容排列工作表标签顺序
Sub 按aa工作表A列的内容排列工作表标签顺序() Dim I%, str1$ I = 1
Sheets(\
Do While Cells(I, 1).Value <> \ str1 = Trim(Cells(I, 1).Value) Sheets(str1).Select
Sheets(str1).Move after:=Sheets(I) I = I + 1
Sheets(\ Loop End Sub
42:以A1单元文本作表名插入工作表
Sub 以A1单元文本作表名插入工作表() Dim nm As String nm = [a1] Sheets.Add
ActiveSheet.Name = nm End Sub
43:删除所有未选定工作表
Sub 删除所有未选定工作表()
Dim sht As Worksheet, n As Integer, iFlag As Boolean Dim ShtName() As String
n = ActiveWindow.SelectedSheets.Count ReDim ShtName(1 To n) n = 1
For Each sht In ActiveWindow.SelectedSheets ShtName(n) = sht.Name n = n + 1 Next
Application.DisplayAlerts = False For Each sht In Sheets iFlag = False For i = 1 To n - 1
If ShtName(i) = sht.Name Then iFlag = True Exit For End If Next
If Not iFlag Then sht.Delete Next
Application.DisplayAlerts = True End Sub
44:工作表标签排序
Sub 工作表标签排序()
Dim i As Long, j As Long, nums As Long, msg As Long
msg = MsgBox(\工作表按升序排列请选 '是[Y]'. \工作表按降序排列请选 '否[N]'\工作表排序\ If msg = vbCancel Then Exit Sub nums = Sheets.Count
If msg = vbYes Then 'Sort ascending For i = 1 To nums For j = i To nums
If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i) End If Next j Next i
Else 'Sort descending For i = 1 To nums For j = i To nums
If UCase(Sheets(j).Name) > UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i) End If Next j Next i End If End Sub
259个常用宏-excelhome(2) 2009-08-15 14:11:45
45:定义指定工作表标签颜色
Sub 定义指定工作表标签颜色()
Sheets(\End Sub
46:在目录表建立本工作簿中各表链接目录
Sub 在目录表建立本工作簿中各表链接目录() Dim s%, Rng As Range On Error Resume Next Sheets(\目录\ If Err = 0 Then
Sheets(\目录\ Else
Sheets.Add
ActiveSheet.Name = \目录\ End If
For i = 1 To Sheets.Count
If Sheets(i).Name <> \目录\ s = s + 1
Set Rng = Sheets(\目录\
Rng = Format(s, \
ActiveSheet.Hyperlinks.Add Rng, \& Sheets(i).Name & \ScreenTip:=Sheets(i).Name End If Next
Sheets(\目录\End Sub
47:建立工作表文本目录
Sub 建立工作表文本目录()
Sheets.Add before:=Sheets(1) Sheets(1).Name = \目录\ For i = 2 To Sheets.Count
Cells(i - 1, 1) = Sheets(i).Name
'Sheets(1).Hyperlinks.Add Cells(i - 1, 1), \ Next End Sub
48:查另一文件的所有表名
Sub 查另一文件的所有表名() On Error Resume Next Dim i%
Dim sh As Worksheet
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & \ Windows(\当前文件名称 Sheets(\ '当前表名称
i = 1 '将表名称返回到第1行 For Each sh In Workbooks(\
Cells(i, 1) = sh.Name '将表名称返回到第1列 i = i + 1 '返回每个表名称向下移动1行 Next sh
Windows(\ '关闭对象文件 Application.ScreenUpdating = True End Sub
49:当前单元录入计算机名
'添加超链接 Sub 当前单元录入计算机名()
Selection = Environ(\
'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub
50:当前单元录入计算机用户名
Sub 当前单元录入计算机用户名() Selection = Environ(\
'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub
51:解除所有工作表保护
Sub 解除所有工作表保护() Dim n As Integer
For n = 1 To Sheets.Count Sheets(n).Unprotect Next n End Sub
52:为指定工作表加指定密码保护表
Sub 为指定工作表加指定密码保护表() Sheet10.Protect Password:=\End Sub
53:在有密码的工作表执行代码
Sub 在有密码的工作表执行代码()
Sheets(\假定表名为“1”,密码为“123” 打开工作表
Range(\= True '隐藏C列空值行
Sheets(\ '重新用密码保护工作表 End Sub
54:执行前需要验证密码的宏(控件按钮代码)
Private Sub CommandButton1_Click()
If InputBox(\请输入密码:\密码是123
MsgBox \密码错误,按确定退出!\提示\ Exit Sub End If
Cells(1, 1) = 10 End Sub
55:执行前需要验证密码的宏()
Sub 执行前需要验证密码的宏()
If InputBox(\请输入您的使用权限:\系统提示\ 重排窗口 '要执行的宏代码或宏名称 Else
MsgBox \对不起,您没有使用该宏的权限,按确定键后退出!\ End If End Sub
56:拷贝A1公式和格式到A2
Sub 拷贝A1公式到A2()
Workbooks(\临时表\表1\
Workbooks(\临时表\表2\End Sub
57:复制单元数值
Sub 复制数值()
s = Workbooks(\ Workbooks(\End Sub
58:插入数值条件格式
Sub 插入数值条件格式()
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:=\
Selection.FormatConditions(1).Interior.ColorIndex = 45
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _ Formula1:=\
Selection.FormatConditions(2).Interior.ColorIndex = 39
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:=\
Selection.FormatConditions(3).Interior.ColorIndex = 34 End Sub
59:插入透明批注
Sub 插入透明批注()
Selection.AddComment
Selection.Comment.Visible = False Dim XS As Worksheet
For i = 1 To ActiveSheet.Comments.Count
ActiveSheet.Comments(i).Text \透明批注\
ActiveSheet.Comments(i).Shape.Fill.Visible = msoFalse Next End Sub
60:添加文本
Sub 添加文本()
Selection = Selection + \\不可在数字后添加文本
'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub
61:光标定位到指定工作表A列最后数据行下一单元
Sub 光标定位到指定工作表A列最后数据行下一单元() a = Sheets(\数据库\ Sheets(\数据库\ Range(\End Sub
62:定位选定单元格式相同的所有单元格
Sub 定位选定单元格式相同的所有单元格() Dim FirstCell As Range, FoundCell As Range Dim AllCells As Range
With Application.FindFormat .Clear
.NumberFormatLocal = Selection.NumberFormatLocal
.HorizontalAlignment = Selection.HorizontalAlignment .VerticalAlignment = Selection.VerticalAlignment .WrapText = Selection.WrapText .Orientation = Selection.Orientation .AddIndent = Selection.AddIndent .IndentLevel = Selection.IndentLevel .ShrinkToFit = Selection.ShrinkToFit .MergeCells = Selection.MergeCells .Font.Name = Selection.Font.Name
.Font.FontStyle = Selection.Font.FontStyle .Font.Size = Selection.Font.Size
.Font.Strikethrough = Selection.Font.Strikethrough .Font.Subscript = Selection.Font.Subscript .Font.Underline = Selection.Font.Underline .Font.ColorIndex = Selection.Font.ColorIndex
.Interior.ColorIndex = Selection.Interior.ColorIndex .Interior.Pattern = Selection.Interior.Pattern .Locked = Selection.Locked
.FormulaHidden = Selection.FormulaHidden End With
Set FirstCell = ActiveSheet.UsedRange.Find(what:=\ If FirstCell Is Nothing Then Exit Sub End If
Set AllCells = FirstCell
Set FoundCell = FirstCell Do
Set FoundCell = ActiveSheet.UsedRange.Find(After:=FoundCell, searchformat:=True)
If FoundCell Is Nothing Then Exit Do Set AllCells = Union(FoundCell, AllCells)
If FoundCell.Address = FirstCell.Address Then Exit Do Loop AllCells.Select End Sub
63:按当前单元文本定位
Sub 按当前单元文本定位() ABC = Selection Dim aa As Range
For Each a In ActiveSheet.UsedRange If a Like ABC Then
what:=\ If aa Is Nothing Then Set aa = a.Cells Else
Set aa = Union(aa, a.Cells) End If End If Next aa.Select End Sub
64:按固定文本定位
Sub 文本定位()
Dim aa As Range
For Each a In ActiveSheet.UsedRange If a Like \合计*\ If aa Is Nothing Then Set aa = a.Cells Else
Set aa = Union(aa, a.Cells) End If End If Next aa.Select End Sub
65:删除包含固定文本单元的行或列
Sub 删除包含固定文本单元的行或列() Do
Cells.Find(what:=\哈哈\
Selection.EntireRow.Delete '删除行 ' Selection.EntireColumn.Delete '删除列
Loop Until Cells.Find(what:=\哈哈\End Sub
66:定位数据及区域以上的空值
Sub 定位数据及区域以上的空值() Dim aa As Range
For Each a In ActiveSheet.UsedRange
If a Like 〈0 Then If aa Is Nothing Then Set aa = a.Cells Else
Set aa = Union(aa, a.Cells) End If End If Next aa.Select End Sub
67:右侧单元自动加5(工作表代码)
Private Sub Worksheet_Change(ByVal Target As Range) Application.EnableEvents = False Target.Offset(0, 1) = Target + 5 Application.EnableEvents = True End Sub
68:当前单元加2
Sub 当前单元加2()
Selection = Selection + 2
'Selection = Workbooks(\临时表\表2\调用指定地址内容 End Sub
69:A列等于A列减B列
Sub A列等于A列减B列() For i = 1 To 23
Cells(i, 1) = Cells(i, 1) - Cells(i, 2) Next End Sub
70:用于光标选定多区域跳转指定单元(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal T As Range) a = Array([b6:b7], [e6], [h6]) For i = 0 To 2
If Not Application.Intersect(T, a(i)) Is Nothing Then
[a1].Select: Exit For End If Next End Sub
71:将A1单元录入的数据累加到B1单元(工作表代码)
Private Sub Worksheet_Change(ByVal Target As Range) Dim t As Long
If Target.Address = \t = Sheet1.Range(\
Sheet1.Range(\End If End Sub
72:在指定颜色区域选择单元时添加/取消\(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim myrg As Range For Each myrg In Target
If myrg.Interior.ColorIndex = 37 Then myrg = IIf(myrg <> \ Next End Sub
73:在指定区域选择单元时添加/取消\(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim Rng As Range
If Target.Count <= 15 Then
If Not Application.Intersect(Target, Range(\ For Each Rng In Selection With Rng
If .Value = \ .Value = \ Else
.Value = \ End If End With Next End If End If
End Sub
74:双击指定单元,循环录入文本(工作表代码)
Private Sub Worksheet_BeforeDoubleClick(ByVal T As Range, Cancel As Boolean) If T.Address <> \Cancel = True
T = IIf(T = \好\中\中\差\好\End Sub
75:双击指定单元,循环录入文本(工作表代码)
Dim nums As Byte
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Target.Address = \nums = nums Mod 3 + 1
Target = Mid(\上中下\Target.Offset(1, 0).Select End If End Sub
76:单元区域引用(工作表代码)
Private Sub Worksheet_Activate()
Sheet1.Range(\End Sub
77:在指定区域选择单元时数值加1(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Application.Intersect([a1:e10], Target) Is Nothing Then Target = Val(Target) + 1 End If End Sub
259个常用宏-excelhome(3) 2009-08-15 14:12:58
78:混合文本的编号
Sub 混合文本的编号()
Worksheets(1).Range(\北京\End Sub
79:指定区域单元双击数据累加(工作表代码)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Not Application.Intersect([A1:Y100], Target) Is Nothing Then oldvalue = Val(Target.Value)
inputvalue = InputBox(\请输入数量,按ENTER键确认!\数值累加器\Target.Value = oldvalue + inputvalue End If End Sub
80:选择单元区域触发事件(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Address = \ MsgBox \你选择了$A$1:$B$2单元\End If End Sub
81:当修改指定单元内容时自动执行宏(工作表代码)
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, [B3:B4]) Is Nothing Then 重排窗口 End If End Sub
82:被指定单元内容限制执行宏
Sub 被指定单元限制执行宏()
If Range(\关闭\窗口 End Sub
83:双击单元隐藏该行(工作表代码)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Rows(Target.Row).Hidden = True End Sub
84:高亮显示行(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Cells.Interior.ColorIndex = 2
Rows(\ '保持1至2行的颜色推荐39,22,40,
Rows(Target.Row).Interior.ColorIndex = 35 '高亮推荐颜色35,20,24,34,37,40,15 End Sub
85:高亮显示行和列(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Cells.Interior.ColorIndex = xlNone
Rows(Target.Row).Interior.ColorIndex = 34
Columns(Target.Column).Interior.ColorIndex = 34 End Sub
86:为指定工作表设置滚动范围(工作簿代码)
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Sheet1.ScrollArea = \End Sub
87:在指定单元记录打印和预览次数(工作簿代码)
Private Sub Workbook_BeforePrint(Cancel As Boolean) Range(\End Sub
88:自动数字金额转大写(工作表代码)
Private Sub Worksheet_Change(ByVal M As Range) On Error Resume Next
y = Int(Round(100 * Abs(M)) / 100)
j = Round(100 * Abs(M) + 0.00001) - y * 100 f = (j / 10 - Int(j / 10)) * 10
A = IIf(y < 1, \元\
b = IIf(j > 9.5, Application.Text(Int(j / 10), \角\零\\
c = IIf(f < 1, \整\分\ M = IIf(Abs(M) < 0.005, \负\End Sub
89:将所有工作表的A1单元作为单击按钮(工作簿代码)
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If Target.Address = \ Call 宏名 End If End Sub
90:闹钟——到指定时间执行宏(工作簿代码)
Private Sub Workbook_Open()
Application.OnTime (\提示1\ '宏名字 Application.OnTime (\提示2\ '宏名字 End Sub
91:改变Excel界面标题的宏(工作簿代码)
Private Sub Workbook_Open() Application.Caption = \春节快乐\End Sub
92:在指定工作表的指定单元返回光标当前多选区地址(工作簿代码)
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Worksheets(\表2\End Sub
93:B列录入数据时在A列返回记录时间(工作表代码)
Public Sub Worksheet_Change(ByVal Target As Range) If Target.Column = 2 Then Target.Offset(, -1) = Now End If End Sub
94:当指定区域修改时在其右侧的2个单元返回当前日期和时间(工作表代码)
Public Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, [A1:A1000]) Is Nothing Then If Target.Column = 1 Then Target.Offset(, 1) = Date Target.Offset(, 2) = Time End If End If End Sub
Public Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, [A1:A1000]) Is Nothing Then If Target.Column = 1 Then
Target.Offset(, 1) = Format(Now(), \Target.Offset(, 2) = Format(Now(), \End If End If End Sub
95:指定单元显示光标位置内容(工作表代码)
Private Sub Worksheet_SelectionChange(ByVal T As Range) Sheets(1).Range(\End Sub
96:每编辑一个单元保存文件
Private Sub Worksheet_Change(ByVal Target As Range) ThisWorkbook.Save End Sub
97:指定允许编辑区域
Sub 指定允许编辑区域()
ActiveSheet.ScrollArea = \End Sub
98:解除允许编辑区域限制
Sub 解除允许编辑区域限制() ActiveSheet.ScrollArea = \End Sub
99:删除指定行
Sub 删除指定行()
Workbooks(\临时表\表2\End Sub
100:删除A列为指定内容的行
Sub 删除A列为指定内容的行() Dim a, b As Integer
a = Sheet1.[a65536].End(xlUp).Row For b = a To 2 Step -1
If Cells(b, 1).Value = \删除\ Rows(b).Delete End If Next End Sub
101:删除A列非数字单元行
Sub 删除A列非数字单元行() i = [a65536].End(xlUp).Row
Range(\End Sub
102:有条件删除当前行
Sub 有条件删除当前行()
If [A1] = 2 Or [B1] = \删除\
Selection.Delete Shift:=xlUp End If End Sub
103:选择下一行
Sub 选择下一行()
ActiveCell.Offset(1, 0).Rows(\End Sub
104:选择第5行开始所有数据行
Sub 选择第5行开始所有数据行A() Dim i% i = Cells.Find(\SearchOrder:=xlByRows, SearchDirection:=xlPrevious).EntireRow.Row Rows(\End Sub
Sub 选择第5行开始所有数据行B()
Rows(\End Sub
105:选择光标或选区所在行
Sub 选择光标或选区所在行() Selection.EntireRow.Select End Sub
106:选择光标或选区所在列
Sub 选择光标或选区所在列() Selection.EntireColumn.Select End Sub
107:光标定位到名称指定位置
Sub 定位()
Application.Goto Range(Evaluate(\名称\
LookIn:=xlValues,
End Sub
108:选择名称定义的数据区
Sub 选择名称定义的数据区()
[数据区].Select '插入名称要使用INDIRECT函数 'Range(\数据区\ 或者 'Sheet1.Range(\数据区\或者 End Sub
109:选择到指定列的最后行
Sub 选择到指定列的最后行()
Range(\End Sub
110:将Sheet1的A列的非空值写到Sheet2的A列
Sub 将Sheet1的A列的非空值写到Sheet2的A列()
Sheet1.Columns(\End Sub
111:将名称1的数据写到名称2
Sub Macro2()
Range(\位置2\位置1\End Sub
112:单元反选
Sub 单元反选()
Application.DisplayAlerts = False Application.ScreenUpdating = False
Dim raddress As String, taddress As String raddress = Selection.Address
taddress = ActiveSheet.UsedRange.Address With Sheets.Add .Range(taddress) = 0 .Range(raddress) = \
raddress = .Range(taddress).SpecialCells(xlCellTypeConstants, 1).Address .Delete End With
ActiveSheet.Range(raddress).Select Application.ScreenUpdating = True End Sub
113:调整选中对象中的文字
Sub 调整选中对象中的文字() '文字居中:自动调整大小 With Selection
.HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .ReadingOrder = xlContext .Orientation = xlHorizontal .AutoSize = True .AddIndent = False End With End Sub
114:去除指定范围内的对象
Sub 去除指定范围内的对象() Dim p As Shape
Set My = Worksheets(\工作表名\ For Each p In My.Shapes
If Not Application.Intersect(p.TopLeftCell, Range(\范围\ Next End Sub
115:更新透视表数据项
Sub DeleteMissingItems2002All()
'防止数据透视表中显示无用的数据项 '在 Excel 2002 或更高版本中 '假如无用的数据项已经存在, '运行这个宏可以更新 Dim pt As PivotTable Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone Next pt Next ws End Sub
116:将所有工作表名称写到A列
Sub 将所有表名称写到A列() k = 1
For Each Sht In Sheets
Cells(k + 1, 1) = Sht.Name '指定写入的行和列 k = k + 1 Next End Sub
117:为当前选定的多单元插入指定名称
Sub 为当前选定的多单元插入指定名称() Selection.Name = \临时\
ActiveWorkbook.Names.Add Name:=\临时\End Sub
118:删除所有名称
Sub 删除所有名称() On Error Resume Next Dim l As Integer
l = ActiveWorkbook.Names.Count For i = l To 1 Step -1
ActiveWorkbook.Names(i).Delete Next End Sub
119:以指定区域为表目录补充新表
Sub 以指定区域为表目录补充新表() Dim dic As Object, sh As Worksheet Dim arr, item
arr = Range(\
'或者换用这行代码也可以 Set dic = CreateObject(\ For Each sh In ThisWorkbook.Worksheets dic.Add sh.Name, \ Next
For Each item In arr
If item <> \ With ThisWorkbook.Worksheets.Add .Name = item End With End If Next
Set dic = Nothing End Sub
120:按A列数据批量修改表名称
Sub 按A列数据批量修改表名称() Dim i%
For i = 1 To Sheets.Count - 1
Sheets(i).Name = Cells(i + 1, 1).Text Next End Sub
121:按A列数据批量创建新表(控件按钮代码)
Private Sub CommandButton1_Click() On Error Resume Next Dim i%, j%
For i = 1 To [a65536].End(xlUp).Row For j = 2 To Sheets.Count
If Cells(i, 1) = Sheets(j).Name Then Exit For End If Next
Sheets.Add(after:=Sheets(Sheets.Count)).Name = Cells(i, 1) Next End Sub
122:清除剪贴板
Sub 清除剪贴板()
Application.CutCopyMode = False
Application.CommandBars(\End Sub
123:批量清除软回车
Sub 批量清除软回车()
'也可直接使用Alt+10或13替换
Cells.Replace What:=Chr(10), Replacement:=\ xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False End Sub
124:判断指定文件是否已经打开
Sub 判断指定文件是否已经打开() Dim x As Integer
For x = 1 To Workbooks.Count
If Workbooks(x).Name = \函数.xls\ '文件名称 MsgBox \文件已打开\ Exit Sub End If Next
MsgBox \文件未打开\End Sub
125:当前文件另存到指定目录
Sub 当前激活文件另存到指定目录()
ActiveWorkbook.SaveAs Filename:=\信件\\\End Sub
126:另存指定文件名
Sub 另存指定文件名()
ActiveWorkbook.SaveAs ThisWorkbook.Path & \别名.xls\End Sub
127:以本工作表名称另存文件到当前目录
Sub 以本工作表名称另存文件到当前目录()
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & \End Sub
128:将本工作表单独另存文件到Excel当前默认目录
Sub 将本工作表单独另存文件到Excel当前默认目录() ActiveSheet.Copy
ActiveWorkbook.SaveAs Filename:=ActiveSheet.Name & \End Sub
129:以活动工作表名称另存文件到Excel当前默认目录
Sub 以活动工作表名称另存文件到Excel当前默认目录()
ActiveWorkbook.SaveAs Filename:=ActiveSheet.Name & \
xlNormal, Password:=\ , CreateBackup:=False End Sub
130:另存所有工作表为工作簿
Sub 另存所有工作表为工作簿() Dim sht As Worksheet
Application.ScreenUpdating = False ipath = ThisWorkbook.Path & \For Each sht In Sheets sht.Copy
ActiveWorkbook.SaveAs ipath & sht.Name & \工作表名称为文件名)
'ActiveWorkbook.SaveAs ipath & sht.Name & Trim(sht.[d15]) & \(文件名称 & D15单元内容)
'ActiveWorkbook.SaveAs ipath & Trim(sht.[d15]) & \ '(文件名称为D15单元内容) ActiveWorkbook.Close Next
Application.ScreenUpdating = True End Sub
131:以指定单元内容为新文件名另存文件
Sub 以指定单元内容为新文件名另存文件()
ThisWorkbook.SaveAs Filename:=ThisWorkbook.Path & \End Sub
132:以当前日期为新文件名另存文件
Sub 以当前日期为新文件名另存文件()
ThisWorkbook.SaveAs ThisWorkbook.Path & \End Sub
Sub 以当前日期为名称另存文件()
ActiveWorkbook.SaveAs Filename:=Date & \End Sub
133:以当前日期和时间为新文件名另存文件
Sub 以当前日期和时间为新文件名另存文件()
ThisWorkbook.SaveAs ThisWorkbook.Path & \年\月\日\时\分\秒\End Sub
134:另存本表为TXT文件
Sub 另存本表为TXT文件() Dim s As String
Dim FullName As String, rng As Range Application.ScreenUpdating = False
FullName = (ActiveSheet.Name & \ '以当前表名为TXT文件名
' FullName = Replace(ThisWorkbook.FullName, \以当前文件名为TXT文件名 ' FullName = Replace(ThisWorkbook.FullName, \以文件名&表名为TXT文件名
Open FullName For Output As #1 '以读写方式打开文件,每次写内容都会覆盖原先的内容
'参考帮助,fullname为文件全名
For Each rng In Range(\ s = s & IIf(s = \
If rng.Column = Range(\ Print #1, s & \ '把数据写到文本文件里 s = \ End If Next
Close #1 '关闭文件
Application.ScreenUpdating = True MsgBox \数据已导入文本\
End Sub
135:引用指定位置单元内容为部分文件名另存文件
Sub 引用指定位置单元内容为部分文件名另存文件()
ActiveWorkbook.SaveAs Filename:=\信件\\\解答\郎雀.xls\End Sub
136:将A列数据排序到D列
Sub 将A列数据排序到D列() [d:d] = [a:a].Value
[d:d].Sort Key1:=Range(\End Sub
137:将指定范围的数据排列到D列
Sub 将指定范围的数据排列到D列() Dim arr1, arr2, i%, x arr1 = Range(\
ReDim arr2(1 To UBound(arr1, 1) * UBound(arr1, 2), 1 To 1) For Each x In Application.Transpose(arr1) i = i + 1 arr2(i, 1) = x Next x
Range(\End Sub 光标移动
Sub 光标移动()
ActiveCell.Offset(1, 2).Select '向下移动1行,向右移动2列 End Sub
138:光标所在行上移一行
Sub 光标所在行上移一行() Dim i%
i = Split(ActiveCell.Address, \ If i > 1 Then Rows(i).Cut
Rows(i - 1).Insert Shift:=xlDown
End If End Sub
139:加数据有效限制
Sub 加数据有效限制()
With Selection.Validation .Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _ xlBetween, Formula1:=\ .IgnoreBlank = False .InCellDropdown = False .InputTitle = \ .ErrorTitle = \ .InputMessage = \
.ErrorMessage = \要奋斗就会有牺牲,死人的事是经常发生的。\ .IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End Sub
140:取消数据有效限制
Sub 取消数据有效限制() With Selection.Validation .Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _ :=xlBetween
.IgnoreBlank = False .InCellDropdown = False .InputTitle = \ .ErrorTitle = \ .InputMessage = \ .ErrorMessage = \
.IMEMode = xlIMEModeNoControl .ShowInput = True .ShowError = True End With End Sub
正在阅读:
excel常用宏集合05-13
江苏省2020届高三数学一轮复习典型题专题训练:圆锥曲线(含解析)12-24
关于文化产业配套设施建设的调查情况104-23
2014年最新平安夜祝福语短信05-20
公务员、选调生考试申论万用句型,想考公务员的一定要看,最好打印并背诵12-26
《难忘的外国童话故事形象》教案05-14
今日基督徒普遍的可怜的光景01-26
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 集合
- 常用
- excel