vba - VBA阵列-检查严格(非近似)匹配

标签 vba excel-2007

If UBound(Filter(myArray, Sheets(i).Cells(1, j).Value, True)) = -1 Then
 'take action
End if


我使用此语法将在Cells(1,j)中找到的元素(例如“ ally”)与数组的所有元素(例如“ mally”,“ kate”,“ becks”)进行比较,并在没有找到完全匹配的内容。
麻烦的是,根据这一行代码,似乎“盟友”被视为与“盟友”匹配(可能是因为“盟友”是“盟友”的子字符串),而我希望“盟友”被识别为与“盟友”不同”。

对实现这一点的语法有帮助吗?谢谢!

最佳答案

过滤器将返回部分匹配的所有项目。 Microsoft建议的解决方法是,然后在过滤后的数组中搜索精确匹配。

Function FilterExactMatch(astrItems() As String, _
                          strSearch As String) As String()

   ' This function searches a string array for elements
   ' that exactly match the search string.

   Dim astrFilter()   As String
   Dim astrTemp()       As String
   Dim lngUpper         As Long
   Dim lngLower         As Long
   Dim lngIndex         As Long
   Dim lngCount         As Long

   ' Filter array for search string.
   astrFilter = Filter(astrItems, strSearch)

   ' Store upper and lower bounds of resulting array.
   lngUpper = UBound(astrFilter)
   lngLower = LBound(astrFilter)

   ' Resize temporary array to be same size.
   ReDim astrTemp(lngLower To lngUpper)

   ' Loop through each element in filtered array.
   For lngIndex = lngLower To lngUpper
      ' Check that element matches search string exactly.
      If astrFilter(lngIndex) = strSearch Then
         ' Store elements that match exactly in another array.
         astrTemp(lngCount) = strSearch
         lngCount = lngCount + 1
      End If
   Next lngIndex

   ' Resize array containing exact matches.
   ReDim Preserve astrTemp(lngLower To lngCount - 1)

   ' Return array containing exact matches.
   FilterExactMatch = astrTemp
End Function


此代码取自http://msdn.microsoft.com/en-us/library/office/aa164525%28v=office.10%29.aspx

关于vba - VBA阵列-检查严格(非近似)匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205999/

相关文章:

vba - 调整旧帖子的代码

vba - Excel ActiveX ComboBox onClick 事件

c# - 使用 C# 的 Excel/VSTO 中的列号到列字母

vba - 确定VBE是否打开

VBA vlookup公式错误

sql-server - 运行简单的 VBA 脚本来测试连接

excel - 在 Visio 中选择事件页面

vba - 有条件复制 Excel File-2 数据到 Excel file-1?

image - 是否有使用 LoadPicture ("bmp_or_icon_file_path")在 Excel 2007 VBA 中加载图像的替代方法

excel - countif criteria_range 和 criteria 中的名称引用