2016-11-29 5 views
-1

私はA1のユーザーのシリーズを持っているエクセルVBAの検索セル

、それが可能なExcelは、文字列を検索し、例えば、別の列

上の文字列の別の配列に対してそれを比較するためです。私はこのようになり、結果何かを見たい

enter image description here

C.

列で検索し、「名前のリスト」に対して、セルA1内のすべてのユーザ を確認したいです。可能?

enter image description here

よろしく、 テリー

+2

はい、間違いなく可能性を使用することができます。 –

答えて

0

私は自分の答えを出してきたし、そのビット長いけれどもそれが働きました。

Sub search() 
 
    Dim users As Variant 
 
    Dim search As Boolean 
 
    Dim j As Integer 
 
    Dim found As Integer 
 
    Dim ArraySize As Integer 
 
    Dim listSize As Integer 
 
    Dim listOfUsers As Integer 
 
    
 
    listSize = 2 
 
    
 
    search = False 
 
    
 
    'Count number of teachers in List Of Users column 
 
    listOfUsers = Range("C2:C1000").Cells.SpecialCells(xlCellTypeConstants).Count 
 
    
 
    While Cells(listSize, 1).Value <> "" 
 
     found = 0 
 
     users = Split(Cells(listSize, 1).Value, ",") 
 
     ArraySize = UBound(users, 1) ' Find array size 
 
     
 
     'Loop until each cell string is done 
 
     For i = 0 To ArraySize 
 
      j = 2 
 
      While search = False 
 
       If Trim(users(i)) = Cells(j, 3).Value Then 
 
        Cells(listSize, 2).Value = Trim(users(i)) 
 
        found = found + 1 
 
        search = True 
 
       ElseIf j > listOfUsers Then 
 
        search = True 
 
       Else 
 
        j = j + 1 
 
       End If 
 
      Wend 
 
      search = False 
 
     Next i 
 
     If found <> ArraySize + 1 Then 
 
     Cells(listSize, 2).Value = "Users not found" 
 
     Else 
 
     Cells(listSize, 2).Value = "All users found" 
 
     End If 
 
     listSize = listSize + 1 
 
    Wend 
 
    
 
End Sub

よろしく、 テリー

0

あなたはDictionaryオブジェクト

Option Explicit 

Sub search() 
    Dim usersRng As Range, cell As Range 
    Dim elem As Variant 
    Dim SearchResults As String 
    Dim searchResultsArray As Variant 
    Dim iCell As Long 

    Set usersRng = Range("A2", Cells(Rows.COUNT, "A").End(xlUp)) '<-- set usersRng in column "A" from row 2 down to last not empty row 
    ReDim searchResultsArray(1 To usersRng.COUNT) '<--| size the search result array to the actual number of cells to be processed 

    With CreateObject("Scripting.Dictionary") 'create and reference a 'dictionary' 
     'store all values from "list of users" column in reference dictionary 
     For Each cell In Range("C2", Cells(Rows.COUNT, "C").End(xlUp)) 
      .Add cell.Value, Null 
     Next cell 

     For Each cell In usersRng '<--| loop through "users" column cells 
      SearchResults = "" '<--| initialize search results 
      For Each elem In Split(Replace(cell.Value, " ", ""), ",") '<--| loop through current cell users 
       If Not .Exists(elem) Then SearchResults = SearchResults & elem & "," '<--| if current user is not in the dictionary then update 'searchResults' string 
      Next elem 
      If SearchResults = "" Then '<--| if all users have been found... 
       SearchResults = "All users found" '<--| ... then set 'searchResults' accordingly 
      Else '<--| otherwise... 
       SearchResults = Left(SearchResults, Len(SearchResults) - 1) & " not found" '<--| ... add " not found" to the already built list of not found users 
      End If 
      iCell = iCell + 1 '<--| update 'searchResultsArray' index 
      searchResultsArray(iCell) = SearchResults '<--| update 'searchResultsArray' 
     Next cell 
     Range("B2").Resize(usersRng.COUNT).Value = Application.Transpose(searchResultsArray) '<--| write down 'searchResultsArray' from cell "B2" downwards 
    End With 
End Sub 
+0

助けを求めるtks。 :) – xingtan

+0

あなたは大歓迎です。辞書を使用すると、セルをループするよりもはるかに高速なコードを使用できます。配列を使用してセルに書き込む場合も同様です。最後に、私の答えがあなたの質問を解決したら、それを受け入れることができます。ありがとうございました! – user3598756

+0

@ xingtan、私はあなたに依頼し続け、良い回答を得ているが、受け入れられたとしてマークすることはない。このサイトのルール(「誰かが私の質問に答えるとどうすればいいですか?」(http://stackoverflow.com/help/someone-answers)を参照)を表示するには、答えの横にあるチェックマークをクリックして、塗りつぶされたin_にグレー表示されます。ありがとうございました! – user3598756