VBA,群里的例子,关于worksheetfunction.find() ,range.find(), object.find()的用法,还有按颜色,等格式查找

 

1 QQ群的问题和答案

1.1 原始问题

  • 别人的提问  
  • 别人的回答
  • 我只是旁观

 

 

1.2 尝试了下问题,确实存在

  • 猜想原因,可能是worksheetfunction.find() 设计就是给string 用的,查数字里可能就会有问题

 

代码

Sub test1()

Debug.Print Application.WorksheetFunction.Find(".", 0.168, 1)

Debug.Print Application.WorksheetFunction.Find(".", "0.168", 1)

Debug.Print Application.WorksheetFunction.Find(".", Str(0.168), 1)


End Sub

 

1.3 别人的好习惯,打断点,看本地窗口的方法,应该多用

  • 常用断点监控
  • 常用fn  f1 看变量,比脚手架要好

 

 

WorksheetFunction.Find

2.1 WorksheetFunction.Find 方法

 

  • 以下是微软帮助的信息,有点奇怪?
  • WorksheetFunction.Find 方法
  • 在工作表中查找特定信息。
  • 语法
  • 表达式.Find(Arg1, Arg2, Arg3)
  • 表达式 一个代表 WorksheetFunction 对象的变量。
  • 参数
名称必选/可选数据类型说明
Arg1必选String工作表名称。
Arg2必选String区域名称。
Arg3可选Variant精确限制查询的参数名称。

 

2.2 worksheetfunction.find() 的用法

  • 需要有 find(find_text, within_text,start_num)
  • find_test 需要是string类型
  • 否则按我自己查的,应该查到的是空值

 

3 application.find  

  • 效果基本等同  worksheetfunction.find  (Application.worksheetfunction.find )

 

4 没有 find()   ( 也就是没有vba.find())

 

 

5 range.find()    返回的是一个 range对象/一般是单元格cell

5.1 基本语法

  • cells.find() = activesheet.find()
  • find返回的是一个单元格,可以直接返回其值,或取到row column等
  • Debug.Print Cells.Find(999)  '查的内容,如果没有会直接报错 “对象变量或 With 块变量没有设置(错误 91)”
  • 语法
  • lookin:=xlvalues  或 xlformulas
  • lookat:=xlwhole 等
.Find(What:=findIt, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

 

  • Dim i As cell  错误,没有这种类型定义
  • 只能
  • dim i as range
  • dim sh as sheet
  • dim wb as workbook

 

 

“对象变量或 With 块变量没有设置(错误 91)”

 

5.2 代码实例

Sub test10()

' cells.find() = activesheet.find()
' find返回的是一个单元格,可以直接返回其值,或取到row column等


'很神奇的就是 find() 如果查的是0,1,2会出错,不知道原因  如 find(1) 总是定位到cells(1,2)去
'查了原因是cells(2,1) 有个公式=LEFT(RIGHT(C2,20),18) 删了就没事了。。。
Debug.Print Cells.Find(2)
Debug.Print Cells.Find("2")
Debug.Print Cells.Find(2).Row
Debug.Print Cells.Find(2).Column



Debug.Print Cells.Find("a")
Debug.Print Cells.Find("a").Row
Debug.Print Cells.Find("a").Column



Debug.Print Cells.Find(22)
Debug.Print Cells.Find("22")

Debug.Print Cells.Find(77)
Debug.Print Cells.Find("77")

Debug.Print Cells.Find(111)
Debug.Print Cells.Find("111")
Debug.Print Cells.Find(111).Row
Debug.Print Cells.Find(111).Column

'Debug.Print Cells.Find(999)  '查的内容,如果没有会直接报错

End Sub

 

5.3 lookat:=xlwhole 等 实现精确查找,默认是模糊查找

Sub test10()

Debug.Print Cells.Find("a")
Debug.Print Cells.Find("a").Row
Debug.Print Cells.Find("a").Column


Debug.Print Cells.Find("a", lookat:=xlWhole)
Debug.Print Cells.Find("a", lookat:=xlWhole).Row
Debug.Print Cells.Find("a", lookat:=xlWhole).Column


End Sub

 

 

5.4 推测find的原理

  • 我用的sheet内,循环单元格的方法,模拟find()方法

 

  • 注意点
  • For Each i In Cells   '不限制cells范围,sheet里cells太多,经常会卡死
  • for each  i in  activesheet.usedrange
  • range().find  也可以查,或者用 usedrange   或 selection等具体点的 range去查
  • cells.find 直接就可以全表查,效果最高

 

Sub test12()
'Dim i As cell  错误,没有这种类型定义
Dim i As Range
For Each i In ActiveSheet.UsedRange
   If i.Value = "11" Then
      Debug.Print "cells(" & i.Row & "," & i.Column; ")"
   End If
Next

End Sub

 

错误例子,下面这个基本运行就容易卡死

Sub test13()
'Dim i As cell
Dim i As Range
For Each i In Cells   '不限制cells范围,sheet里cells太多,经常会卡死
   If i.Value = "11" Then
      Debug.Print "cells(" & i.Row & "," & i.Column; ")"
   End If
Next

End Sub

 

相关的 findnext 和findprevious

 

 

 

6 object.Find 方法 (VBA外接程序对象模型)  VBA方法

  • 请参阅 示例 应用于 特性
  • 在活动模块上搜寻一个特定字符串。
  • 语法
  • object.Find(target, startline, startcol, endline, endcol [,爓holeword] [,爉atchcase] [, patternsearch]) As Boolean

 

 

下面这段FSO是错误的,应该可以改正确

Sub test3()

Dim f1 As Object

'Dim sh1 As Worksheet
'Set sh1 = Worksheets("sheet1")



Set fso = CreateObject("scripting.filesystemobject")


Set f1 = fso.GetFile(ThisWorkbook.Path & "\" & ThisWorkbook.Name)
Debug.Print f1.Find(1)

End Sub

 


 

 

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值