简体   繁体   English

使用excel vba在逗号分隔的列中查找字符串

[英]Finding a string in comma seperated column with excel vba

with Excel VBA, I have to search for a specific Partnumber in a selected column.使用 Excel VBA,我必须在选定的列中搜索特定的零件号。 The Column has Partnumbers separated by a comma.该列的部件号以逗号分隔。 The data in column is something like this列中的数据是这样的

Column A A列

ab,ca,ade,ar ab,ca,ade,ar

ad,aw,dr...广告,哦,博士...

When I do a Selection.Find on that column for "ad", it returns the first row having 'ade' cannot do a find for Whole cell as there are more than 1 part number in any cell on that column Here is code I use to do that task.当我在“广告”的该列上执行 Selection.Find 时,它返回具有“ade”的第一行无法对整个单元格进行查找,因为该列的任何单元格中都有多个部件号这是我使用的代码做那个任务。

Set Cell = Selection.Find(What:=:"ad", after:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)  ' This is BUG that needs correction for xlPart

If Cell Is Nothing Then
        ….
End If

What is best and efficient way of solving this issue什么是解决此问题的最佳和有效方法

Thanks for help in advance...:)提前感谢您的帮助...:)

You can use a RegExp based function like below您可以使用如下所示的基于RegExp的函数

Public Function blCheckString(InputRange As Range, strChkString) As Boolean
Dim objRgEx As Object
Set objRgEx = CreateObject("VBScript.RegExp")
With objRgEx
    .Pattern = "\b" & strChkString & "\b"
    If .Test(InputRange.Value) Then
        blCheckString = True
    Else
        blCheckString = False
    End If
End With
Set objRgEx = Nothing
End Function

This can then be used in the code as below.然后可以在下面的代码中使用它。

Sub Test()
If blCheckString(Selection, "ad") Then
    MsgBox "Success"
Else
    MsgBox "No Success"
End If
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM