Excel VBA 动态搜索&(双击选中|键盘选中listbox内容)&单个单元格局部文字高亮


1.单个单元格局部文字高亮

新建模块,输入代码,开发工具插入按键,赋予DemoHighLightChar()函数。

 1 Sub DemoHighLightChar()
 2     Dim KeyWords As String
 3     Dim i As Integer
 4     Dim C As Range
 5     Dim ComRng As Range
 6     Dim FirstAddress As String
 7      
 8     KeyWords = InputBox("输入查找关键字", "提示")
 9 If Not KeyWords = "" Then
10     With ActiveSheet.UsedRange
11         Set C = .Find(what:=KeyWords, lookat:=xlPart)
12         If Not C Is Nothing Then
13             FirstAddress = C.Address
14             Do
15                 With C
16                     .Font.ColorIndex = xlAutomatic
17                     .Characters(Start:=InStr(1, C, KeyWords), Length:=Len(KeyWords)).Font.Color = vbGreen
18                 End With
19                 Set C = .FindNext(C)
20             Loop While Not C Is Nothing And C.Address <> FirstAddress
21         Else
22             MsgBox "没找到相关内容", vbExclamation, "提示"
23         End If
24     End With
25 End If
26 End Sub

2.动态搜索

所在Sheet中使用 开发工具 ->插入 ActiveX控件 中的textboxlistbox

工作表选取改变事件,选取工作表的特定的位置(代码中规定),弹出textbox和listbox。

 1 '工作表选取改变事件
 2     Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 3     Dim i, x, rownu As Variant
 4     Dim d As Object
 5     Dim arr, arr_key, arr1, yun, arr_po
 6     Dim website_name As String
 7     
 8     Set d = CreateObject("scripting.dictionary")
 9     Me.ListBox1.Clear
10     'target为选取的单元格对象
11     tacolumn = Target.Column
12     tarow = Target.Row
13   
14     '添加website部分
15     '选择触发的区域,使用Target.Cells.CountLarge是为了保证选择的是一个单元格而不是一片区域,同时区域过大不会报错
16         If Target.Column = 22 And Target.Cells.CountLarge = 1 Then
17             With Me.TextBox1 'textbox的大小,位置,和显示
18                 .Visible = True
19                 .Top = Target.Top
20                 .Left = Target.Left
21                 .Width = Target.Width
22                 .Height = Target.Height
23                 .Activate
24             End With
25             With Me.ListBox1 'listbox的大小,位置,和显示
26                 .Visible = True
27                 .Top = Target.Top
28                 .Left = Target.Left + Target.Width
29                 .Width = 300
30                 .Height = 300
31                 '将需要写入的数据装入数组
32                 arr = Sheets("Sheet2").Range("e2:e" & Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row)
33                 For x = 1 To UBound(arr)
34                 d(arr(x, 1)) = ""
35                 Next
36                 '将值写入到listbox中
37                 .List = d.keys()
38                 
39             End With
40         Else
41             Me.ListBox1.Clear
42             Me.TextBox1 = ""
43             Me.ListBox1.Visible = False
44             Me.TextBox1.Visible = False
45         End If
46 End Sub

textbox键盘抬起事件:输入文字后进行动态查询代码中所规定的库(Sheet2中的内容),其中要注意KeyCode的值,13是回车(使用MsgBox试出来的)。

 1 'textbox键盘抬起事件:即输入了文字
 2 Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 3     Dim i, x As Integer
 4     Dim Language As Boolean, arr1 As Variant
 5     Dim myStr As String, str_B As String
 6     Dim d As Object
 7     Dim arr, arr_key
 8     
 9     Set d = CreateObject("scripting.dictionary")
10     Me.ListBox1.Clear
11     myStr = Me.TextBox1.Value
12     With Me.ListBox1
13                 .Width = 400
14                 .Height = 300
15     End With
16     If tacolumn = 22 Then
17     With Sheets("Sheet2")
18            
19                 arr1 = .Range("e2:e" & .Range("a65535").End(xlUp).Row)
20                 For i = 1 To .Range("a65535").End(xlUp).Row - 1
21                 '利用instr遍历找到包含输入文字的部分,并 赋值到字典里避免重复
22                    If InStr(1, arr1(i, 1), myStr, 1) Then
23                        d(arr1(i, 1)) = ""
24                    End If
25                 Next i
26                 Me.ListBox1.List = d.keys() 'listbox赋值
27             
28     End With
29     If KeyCode = 13 Then
30     Me.ListBox1.Activate
31     End If
32     End If
33 End Sub

3.双击选中|键盘选中listbox内容

双击listbox选中内容。

 1 'listbox双击事件
 2 Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
 3 Dim arr
 4     If tacolumn = 22 Then
 5     '将listbox值赋予当前单元格
 6         ActiveCell.Value = Me.ListBox1.Value
 7         Me.ListBox1.Clear
 8         Me.TextBox1 = "" '清空listbox与textbox
 9         Me.ListBox1.Visible = False 'y隐藏textbox和listbox
10         Me.TextBox1.Visible = False
11 
12     End If
13 End Sub

键盘选中listbox内容,需要上下键高亮文本后再空格选中。

 1 'listbox回车事件
 2 Private Sub ListBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
 3     If tacolumn = 22 And KeyCode = 32 Then
 4         '将listbox值赋予当前单元格
 5         Cells(tarow, tacolumn).Value = Me.ListBox1.Value
 6         Me.ListBox1.Clear
 7         Me.TextBox1 = "" '清空listbox与textbox
 8         Me.ListBox1.Visible = False 'y隐藏textbox和listbox
 9         Me.TextBox1.Visible = False
10 
11     End If
12 End Sub

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM