VBA:如何使用查找来搜索*? [英] VBA: How to search for * using Find?

查看:63
本文介绍了VBA:如何使用查找来搜索*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在工作表的一列中搜索字符串**,但是由于*在使用find时也可以用作通配符,因此我无法进行此项工作.为了使它更复杂,同一列还包括*,因此我需要专门查找**.到目前为止,我已经尝试了以下代码,在这两种情况下,它似乎都找到了第一个非空单元格,恰好是*.

I am trying to search for the string ** in a column in a worksheet, but I have trouble making this work as * also works as a wildcard when using find. To complicate it further the same column also includes *, so I need to find ** specifically. I have tried the below code so far, and in both cases it appears to find the first non-empty cell, which happens to be *.

Set searchRange = Workbooks(fileName).Worksheets(1).Range("B:B").Find(Chr(42) & Chr(42), LookAt:=xlWhole)

或者:

Set searchRange = Workbooks(fileName).Worksheets(1).Range("B:B").Find("**", LookAt:=xlWhole)

我已经成功地多次使用Chr()进行了转义,但似乎在这里却行不通.我已经在Google上寻求解决方案,但是该解决方案是我迄今为止发现的唯一答案,这是行不通的.

I have used Chr() succesfully to escape " many times before, but it does not seem to work here. I have googled for solutions, but this solution is the only answer I have found so far, which does not work.

另一种解决方案可能是遍历范围并进行字符串比较.不过,这似乎是一个非常丑陋的解决方案.

An alternative solution might be to loop through the range and do a string comparison. This feels like a rather ugly solution though.

比我想象的要容易.显然这可行:

Was easier than I thought. Apparently this works:

Set searchRange = Workbooks(fileName).Worksheets(1).Range("B:B").Find("~*~*", LookAt:=xlWhole)

推荐答案

通配符(*)可以使用代字号(〜)进行转义,因此可以这样解决:

Wildcard (*) can be escaped using tilde (~), this can thus be solved like this:

Set searchRange = Workbooks(fileName).Worksheets(1).Range("B:B").Find("~*~*", LookAt:=xlWhole)

由Axel Richter在评论中发布的官方消息

这篇关于VBA:如何使用查找来搜索*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆