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

この構文を使用して、Cells(1, j) で見つかった要素 (「ally」など) を配列のすべての要素 (「mally」、「kate」、「becks」など) と比較し、そうでない場合にアクションを実行しました。完全一致が見つかりました。問題は、このコード行に基づいて、「ally」が「mally」に一致すると見なされるように見えることです (おそらく「ally」は「mally」の部分文字列であるため)。 "。

これを達成するための構文の助けはありますか? ありがとうございました!

4

4 に答える 4

3

フィルタは、部分的に一致するすべてのアイテムを返します。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から取得されます

于 2013-01-07T23:46:54.677 に答える
3

配列がこの比較にのみ使用され、他には必要ない場合は、データに表示されない独自の区切り文字 (角かっこなど) を追加して、完全な単語比較を強制することもできます。
したがって、配列を変更して "[mally]"、"[kate]"、"[becks]"
を含めると、条件は次のようになります。

If UBound(Filter(myArray, "[" & Sheets(i).Cells(1, j).Value & "]", True)) = -1
于 2013-01-08T23:01:31.117 に答える
2

フィルターを使用する必要がない場合は、以下のスニペットが機能します

Dim v
Dim bMatch As Boolean
bMatch = False

For Each v In myArray
    'compare strings
    If StrComp(CStr(v), Sheets(i).Cells(1, j).Value, vbTextCompare) = 0 Then
        bMatch = True
    End If
Next

If Not bMatch Then
'do something
End If
于 2013-01-07T23:54:08.990 に答える