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控件 中的textbox和listbox


工作表選取改變事件,選取工作表的特定的位置(代碼中規定),彈出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
