[英]vlookup multiple values in one cell
I am trying to vlookup multiple values in one cell based on the a selection from another cell. 我正在尝试根据另一个单元格的选择在一个单元格中查找多个值。
I have the below table where I select a single "Gym" the "GymIDs" is automatically populated. 我在下表中选择了一个“ Gym”,“ GymIDs”会自动填充。 I have then used the below VBA to allow me to select multiple "Gyms", I also want it to show me multiple "GymIDs". 然后,我使用下面的VBA允许我选择多个“ GymID”,我也希望它向我展示多个“ GymID”。
Current vlookup =VLOOKUP(M7,Ignore!F1:G300,2,FALSE) 当前vlookup = VLOOKUP(M7,忽略!F1:G300,2,FALSE)
for some reason I could only upload one image so put them all together 由于某种原因,我只能上传一张图片,所以将它们全部放在一起
VBA code for multiple selections 多种选择的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
I'd suggest writing a function in the lookup column. 我建议在查找列中编写一个函数。 As you've not provided the vlookup i'll leave that part to you. 由于您没有提供vlookup,因此我将把这一部分留给您。 The main concept at play here is to detect whether the Gyms column is a single value or a comma separated list and treat it based upon that: 这里的主要概念是检测“ Gyms”列是单个值还是逗号分隔的列表,并根据以下内容对其进行处理:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.