Vba中Find方法使用总结(一)

查找表格中的数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Sub findNum()
Dim i&, j&, d As Date
 
For i = 1 To 10000
  For j = 1 To 50
   If Cells(i, j) = "老石" Then
     Cells(i, j).Interior.Color = vbRed
     Cells(i, j).Select
    GoTo FOUND
    End If
    Next j
   Next i
  
FOUND:
   MsgBox "公用时:" & DateDiff("s", d, Time()) & "秒"
End Sub

  改成数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Sub findNum()
Dim i&, j&, d As Date, arr()
d = Time()
 
arr = Range(Cells(1, 1), Cells(10000, 50))
 
For i = 1 To 10000
  For j = 1 To 50
   If arr(i, j) = "老石" Then
     Cells(i, j).Interior.Color = vbRed
     Cells(i, j).Select
    GoTo FOUND
    End If
    Next j
   Next i
  
FOUND:
   MsgBox "公用时:" & DateDiff("s", d, Time()) & "秒"
End Sub

  用Range:  没有找到任何结果,返回Nothing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sub findNun()
 
   Dim d As Date, r As Range
    
   d = Time()
    
   Set r = Range(Cells(1, 1), Cells(10000, 50)).Find("老石")
    
   r.Interior.Color = vbRed
   r.Select
    
    MsgBox "公用时:" & DateDiff("s", d, Time()) & "秒"
    
End Sub

  加上判断:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sub findNun()
 
   Dim d As Date, r As Range
    
   d = Time()
    
   Set r = Range(Cells(1, 1), Cells(10000, 50)).Find("老石")
    
   If Not r Is Nothing Then
      r.Interior.Color = vbRed
      r.Select
    
      MsgBox "公用时:" & DateDiff("s", d, Time()) & "秒"
    Else
      MsgBox "没有找到"
    End If
End Sub

  office常用通配符:

 

Find中的参数:

 

 

 

 lookat:

 

 

 

 查找范围:

 

 

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
Sub formatDemo()
    Dim r As Range
    Application.FindFormat.Interior.Color = vbBlack
    Application.FindFormat.Font.Color = vbWhite
     
    Set r = Cells.Find("老石", searchformat:=True)
     
    If Not r Is Nothing Then
        MsgBox r.Address
    End If
End Sub

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Sub formatDemo()
    Dim r As Range
      
    With Application.FindFormat
        .Interior.Color = vbBlack
        .Font.Color = vbWhite
    End With
     
    Set r = Cells.Find("老石", searchformat:=True)
     
    If Not r Is Nothing Then
        MsgBox r.Address
    End If
End Sub

  

 

 

 

 

 

 

1
2
3
4
5
6
7
8
9
Sub findNum()
    Dim r As Range
     
    Set r = Range("b2:e5").Find(2, after:=Range("C4"), lookat:=xlWhole)
     
    If Not r Is Nothing Then
        r.Interior.Color = vbRed
    End If
End Sub

  

1
2
3
4
5
6
7
8
9
Sub findNum()
    Dim r As Range
     
    Set r = Range("b2:e5").Find(2, searchorder:=xlByColumns)
     
    If Not r Is Nothing Then
        r.Interior.Color = vbRed
    End If
End Sub

  

 

1
2
3
4
5
6
7
8
9
Sub findNum()
    Dim r As Range
     
    Set r = Range("b2:e5").Find(2, searchorder:=xlByColumns, searchdirection:=xlPrevious)
     
    If Not r Is Nothing Then
        r.Interior.Color = vbRed
    End If
End Sub

  

 

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Sub findNum()
    Dim r As Range
     
    Set r = Cells.Find("熊猫")
     
    If Not r Is Nothing Then
        r.Interior.Color = vbRed
    End If
End Sub
 
 
Sub findNum1()
    Dim r As Range
     
    Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
     
    If Not r Is Nothing Then
       r.Interior.Color = vbRed
    End If
     
End Sub
 
Sub findNum2()
  Dim r As Range
  Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
   
  If Not r Is Nothing Then
     r.Interior.Color = vbRed
  End If
   
  Do While Not r Is Nothing
   
    Set r = Cells.Find(2, after:=r)
     
    If Not r Is Nothing Then
       r.Interior.Color = vrRed
    End If
     
  Loop
   
End Sub
 
Sub findNum3()
   Dim r As Range
   Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
    
   Do While Not r Is Nothing
      r.Interior.Color = vbRed
      '程序进入了死循环'
      Set r = Cells.Find(2, after:=r)
      '判断是不是第一次的单元格'
      If r.Address = "$C$2" Then Exit Do
       
    Loop
       
End Sub
 
Sub findNum4()
   Dim r As Range, s As String
    
   Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
   If Not r Is Nothing Then
       s = r.Address
   End If
    
   Do While Not r Is Nothing
      r.Interior.Color = vbRed
      Set r = Cells.Find(2, after:=r)
      If r.Address = s Then Exit Do
   Loop
End Sub
 
 
Sub findNum5()
  Dim r As Range, s As String
  Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
  If Not r Is Nothing Then
     s = r.Address
     'do while 循环'
     Do
       r.Interior.Color = vbRed
       Set r = Cells.Find(2, after:=r)
     Loop While r.Address <> s
   
  End If
End Sub
 
Sub findNum6()
 
  Dim r As Range, s As String
  Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
  If Not r Is Nothing Then
     s = r.Address
     'do while 循环'
     Do
       r.Interior.Color = vbRed
       Set r = Cells.Find(2, after:=r)
       '不断循环,知道r的地址是s时终止'
     Loop Until r.Address = s
   
  End If
End Sub
 
Sub findNum7()
 
  Dim r As Range, s As String
  Set r = Cells.Find(2, lookat:=xlWhole, searchorder:=xlRows)
  If Not r Is Nothing Then
     s = r.Address
     'do while 循环'
     Do
       r.Interior.Color = vbRed
       Set r = Cells.FindNext(r)
       '不断循环,知道r的地址是s时终止'
     Loop Until r.Address = s
   
  End If
End Sub

  

posted @   石shi  阅读(39835)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示