应用场景 把经常用到的查找工具,创建成工具栏,在打开工作薄时生成 知识要点 1:auto_open过程,打开工作薄时自动执行 2:CommandBars 代表容器应用程序中命令栏的 CommandBar 对象的集合 3:CommandBars('Formatting') Formatting 表示格式工具栏 4:Type:=msoControlButton 命令按钮 Type:=msoControlEdit 文本框 Sub auto_open() 'auto_open表示打开文件时就执行 On Error Resume Next CommandBars('Formatting').Controls('请输入查找内容').Delete '删除上次产生的工具栏 With CommandBars('Formatting').Controls.Add(Type:=msoControlButton, temporary:=True) .Caption = '请输入查找内容' '工具栏显示的标题 .BeginGroup = True '显示一条分割线 .TooltipText = '请输入查找内容' '鼠标指向时出现提示 .Style = msoButtonCaption '显示文字 End With '在创建一个文字框菜单 With CommandBars('Formatting').Controls.Add(Type:=msoControlEdit, temporary:=True) .Caption = '查找' '指定显示标题 .Text = '' '默认显示空白 .OnAction = 'intos' '关联的宏,表示输入文字后按回车键执行的过程名称 End With End Sub Sub intos() With ActiveSheet.UsedRange '在当前表已用区域中查找 Dim rng As Range, rngg As Range, firstaddress As String Set rng = .Find(CommandBars('Formatting').Controls('查找').Text, LookIn:=xlValues, lookat:=xlPart) If Not rng Is Nothing Then '如果找到 firstaddress = rng.Address '记录第一个单元格地址 Do If rngg Is Nothing Then Set rngg = rng Else Set rngg = Union(rng, rngg) Set rng = .FindNext(rng) Loop While rng.Address <> firstaddress rngg.Select '选择所有符合条件的单元格 MsgBox '已找到目标所在地址:' & rngg.Address(0, 0) '报告地址 End End If MsgBox '没找到' End With End Sub |
|
来自: L罗乐 > 《VBA基础入门教程》