首页 > 其他分享 >Vba中Find方法使用总结(一)

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

时间:2022-10-19 20:33:01浏览次数:55  
标签:总结 Vba Set End Color Cells Range Find

查找表格中的数据:

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

  改成数组:

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

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

  加上判断:

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常用通配符:

Vba中Find方法使用总结(一)_死循环

 

Find中的参数:

Vba中Find方法使用总结(一)_数据_02

 

 

Vba中Find方法使用总结(一)_数组_03

 

 lookat:

Vba中Find方法使用总结(一)_数据_04

Vba中Find方法使用总结(一)_数据_05

 

 

Vba中Find方法使用总结(一)_数组_06

 

 查找范围:

Vba中Find方法使用总结(一)_数组_07

 

 

Vba中Find方法使用总结(一)_数组_08

 

 

Vba中Find方法使用总结(一)_死循环_09

 

 

Vba中Find方法使用总结(一)_数据_10

 

 

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

  

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

  

Vba中Find方法使用总结(一)_数组_11

 

 

Vba中Find方法使用总结(一)_数据_12

 

 

Vba中Find方法使用总结(一)_数据_13

 

 

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

  

Vba中Find方法使用总结(一)_数据_14

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

  

Vba中Find方法使用总结(一)_死循环_15

 

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

  

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

  



标签:总结,Vba,Set,End,Color,Cells,Range,Find
From: https://blog.51cto.com/u_10999550/5776572

相关文章

  • vba中Range对象的Replace方法
    replace是Range对象的一个方法,用于单元格替换.      SubreplaceTest()Application.ReplaceFormat.Interior.Color=vbGreen'指定lookat参数为Whole,从而避免......
  • springBoot 总结
        这是标准创建boot工程的方式 注意这里使用的是阿里云的url  https://start.aliyun.com/修改服务器端口  自动提示功能消失解决方案    ......
  • 10月19日内容总结——包的使用、软件开发目录规范和常用内置模块
    目录一、包的具体使用二、编程思想的转变三、软件开发目录规范1、bin目录2、conf目录3、core目录4、lib目录5、db目录6、interface目录7、log目录8、readme文件9、requirem......
  • OAuth2知识点总结
    OAuth2是什么?OAuth2是一个授权协议。OAuth2.0框架能让第三方应用以有限的权限访问HTTP服务,可以通过构建资源拥有者与HTTP服务间的许可交互机制,让第三方应用代表资源拥有者......
  • 进程间通信7种方式总结
    //linuccommand===>man====>pipe,mkfifo,kill&signal,semget,shmget,msgget,inet_addr//三次握手四次挥手//client==>connectreq,//server==>ack,//client===>ack///......
  • 总结 vue 实现分页器的基本思路
    简介本文介绍基于vue2框架实现基本的分页器,包括页码前进/后退、页码点击跳转、显示...、显示总页数、显示总数据条数等功能。效果预览快速跳转视图部分、样式部......
  • vite..config.ts中Cannot find module 'path' or its corresponding type declaration
    ts中引入path模块出错Cannotfindmodule'path'oritscorrespondingtypedeclarations.解决方法第一步npminstall-D@types/node第二步在tsconfig.json中添加......
  • 设计模式总结
    简述设计模式的分类创建型模式:在创建对象的同时隐藏创建逻辑,不使用new直接实例化对象。有工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。结构型模......
  • 【Vue】图片拉近、全屏背景实战经验总结
    1图片拉近缘起是看到了下面的图片,我发现当鼠标悬浮的时候,发现他是可以拉近的,也就是图片的宽高不变,但是图片被放大了起初我以为是有一个这样的方法,可以实现这个操作,但是查找......
  • vue组件通信6种方式总结(常问知识点)
    前言在Vue组件库开发过程中,Vue组件之间的通信一直是一个重要的话题,虽然官方推出的Vuex状态管理方案可以很好的解决组件之间的通信问题,但是在组件库内部使用Vuex往往会......