使用Find时SearchDirection不起作用

Orhun S.

Ms Excel我有一个VBA代码看起来在一个范围内找到一个月的日子。范围像1,2,3,...,30,1(在列中),因为有些月份有30天,而有些月份则有31天。当试图在范围代码中找到“ 1”时,请始终输入第二个“ 1” “ 结果是。当我从范围中删除第二个“ 1”时,它将给出正确的结果。在这种情况下,“ SearchDirection”似乎无能为力。这是什么原因呢?

我使用的代码:

day = 1
Set findday = findmonth.Find(day, LookIn:=xlValues, LookAt:=xlWhole, SearchDirection:=xlNext, SearchOrder:=xlByColumns)
Debug.Print findday.Address
VBasic2008

查找方法“失败”

  • 如果xlValues与该LookIn参数一起使用,则如果您希望在其中查找值的行或列被隐藏,Find则将“失败”查找它。
  • 默认情况下,该Find方法使用xlNextfor,SearchDirection因此您可以忽略它。
  • 您的问题无疑是忽略了After论点。问题是,如果您输入第一个单元格(A1)作为参数的After参数(默认为(A1)),则搜索将从A2ifxlByColumnsB1if开始xlByRows因此,您必须指定Find方法范围内的最后一个单元格,才能使用范围(A1)中的第一个单元格开始搜索

例如:

Day = 1
Set findday = findmonth.Find( _
    What:=Day, _
    After:=findmonth.Cells(findmonth.Rows.Count, findmonth.Columns.Count), _
    LookIn:=xlFormulas, _
    LookAt:=xlWhole, _
    SearchOrder:=xlByColumns, _
    SearchDirection:=xlNext)
Debug.Print findday.Address

或者,如果您更喜欢一行:

Dim findday As Range
Day = 1
Set findday = findmonth.Find(What:=Day, After:=findmonth.Cells(findmonth.Rows.Count, findmonth.Columns.Count), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext)
Debug.Print findday.Address

使用该With语句可以使它更具可读性:

Day = 1
With findmonth
    Set findday = .Find(What:=Day, After:=.Cells(.Rows.Count, .Columns.Count), LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns)
End With
Debug.Print findday.Address

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 GoogleFonts 时 FontWeight 不起作用

使用动词时workingdirectory不起作用

使用ViewModel时导航不起作用

使用?/ attr时actionMenuTextColor不起作用

使用路由时指令不起作用

使用glOrtho()时gluLookAt()不起作用?

使用遮罩时“ WithAnimation”不起作用?

使用Bootstrap时不起作用

使用 [[ngModel]] 时 patchValue 不起作用

使用 viewBinding 时 SetTheme 不起作用

在集合上使用聚合方法时,find查询中的聚合不起作用

使用driver.find_elements_by_class_name后运行打印时,打印不起作用

使用 cssgrid 时,pageYOffset 在滚动时不起作用

容器在使用Dockerfile时起作用,但在使用docker-compose.yml时不起作用

使用groovy类的Jenkinsfile在使用列表时不起作用

提交时不起作用

使用附加功能时ng-click不起作用

使用Spotipy库时current_playing不起作用

_delay_ms 在使用 pwm 时似乎不起作用

在Eclipse插件中使用反射库时不起作用

为什么 FindOneAndUpdate 在使用 $in 时在 mongoose 中不起作用

使用 HTML5 时所需的属性不起作用?

为什么我的命令在使用别名时不起作用?

Python:使用readline时,串行超时不起作用

使用 puppeteer 抓取谷歌地图时滚动不起作用

为什么在使用 addPolygon() 方法时 setPos() 不起作用?

使用getter时,最终字段的PowerMock setInternalState不起作用

在Spring中使用@RequestPart时,@ Valid不起作用

使用 mutate 时,函数在管道内不起作用