VBA: How to search for * using Find?

给你一囗甜甜゛ 提交于 2021-02-05 12:17:59

问题


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)

Alternatively:

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

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.

EDIT: Was easier than I thought. Apparently this works:

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

回答1:


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)

Official source, posted by Axel Richter in comments



来源:https://stackoverflow.com/questions/40133451/vba-how-to-search-for-using-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!