问题
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