2017-10-02 13 views
0

別のセルからの選択に基づいて、あるセル内の複数の値をvlookupしようとしています。1つのセル内で複数の値を調べる

私は「GymIDs」が自動的に入力される単一の「Gym」を選択する以下の表を持っています。私はその後、複数の "Gyms"を選択できるように、以下のVBAを使用しました。また、複数の "GymIDs"を表示したいと思います。

現在VLOOKUP = VLOOKUP(M7、無視F1:!G300,2、FALSE)複数選択のための

for some reason I could only upload one image so put them all together

excel table

VBAコード

Private Sub Worksheet_Change(ByVal Target As Range) 

'Code by Sumit Bansal from https://trumpexcel.com 
' To Select Multiple Items from a Drop Down List in Excel 

Dim Oldvalue As String 
Dim Newvalue As String 

On Error GoTo Exitsub 
If Not Intersect(Target, Range("M7:M30")) Is Nothing Then 
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then 
    GoTo Exitsub 
    ElseIf Target.Value = "All" Then GoTo Exitsub 
    ElseIf Target.Value = "Select" Then GoTo Exitsub 
    ElseIf Target.Value = "" Then GoTo Exitsub 
    Else 
     Application.EnableEvents = False 
     Newvalue = Target.Value 
     Application.Undo 
     Oldvalue = Target.Value 
     If Oldvalue = "" Then 
      Target.Value = Newvalue 
     ElseIf Oldvalue = "All" Then 
      Target.Value = Newvalue 
     ElseIf Oldvalue = "Select" Then 
      Target.Value = Newvalue 
     Else 
      Target.Value = Oldvalue & ", " & Newvalue 
     End If 
    End If 
End If 
Application.EnableEvents = True 
Exitsub: 
Application.EnableEvents = True 
End Sub 
+0

参照編1:[StackOverflowの](https://stackoverflow.com/questions/14803944/Excelで複数の用語を検索する)Reference2:[Stackoverflow](https://stackoverflow.com/questions/19504858/find-all-matches-in-workbook-using-excel -vba) – mgae2m

答えて

0

私は関数を書くことを提案したいルックアップ列に表示されます。あなたはvlookupを提供していないので、私はあなたにその部分を残します。ここで遊んでメインコンセプトは、ジムの列が単一の値またはカンマで区切られたリストであるかどうかを検出し、そのに基づいて、それを扱うことです:

Public Function GymLookup(ByVal stringToProcess As String) As Variant 
Application.Volatile 

Dim var As Variant 
Dim arrData As Variant 
Dim strConcat As String 

If InStr(1, stringToProcess, ",") > 0 Then 
    arrData = Split(stringToProcess, ",") 
    For Each var In arrData 
     'multiple handler 
     If strConcat = "" Then 
      strConcat = "enter your vlookup to resolve a result here" 
     Else 
      strConcat = strConcat & ", " & "enter your vlookup to resolve a result here" 
     End If 
    Next var 
    GymLookup = strConcat 
Else 
    'Single cell lookup 
    GymLookup = "enter your vlookup to resolve a result here" 
End If 

End Function 
+0

申し訳ありません私は今日自分自身に教えてきたVBAについて学んだすべてのものを秀でることで偉大ではありません。 stringToProcessは編集が必要ですか?私はvlookupを今追加しました、それは= VLOOKUP(M7、Ignore!F1:G300,2、FALSE)です。私はあなたが述べたところでvlookupで上記の関数を使ってみましたが、まだ動作していないようです。 – NathJP

+0

したがって、VBAでは、 "enter ..."を 'application.worksheetfunction.vlookup(M7、Ignore!F1:G300,2、FALSE)'に置き換えたいと思います。 – Zerk

関連する問題