Home » excel » excel – how to use equal to and multiple Vlookup in single cell

excel – how to use equal to and multiple Vlookup in single cell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have created a multiselect dropdown for Cities in my sheet 1 and the postcodes associated with the dropdown is in sheet 2.
This is how my sheet 2 looks.

enter image description here

1.) User is allowed to select multiple cities from the dropdown. As soon as user selects the city, I want to show in one cell the selected city and the associated postcodes. For e.g. If user selects Sion and Dadar from the dropdown then just below the dropdown user should be able to see something like this.

enter image description here

With the help of Vlookup i am able to retrieve either one of the value and also not able to show in a single cell with equals to sign.

2.) Also I have used VBA code from the internet to have multiple select and remove. The code works fine but I want to make some changes in it. Like when user selects two cities the value gets populated in the dropdown cell separated by “comma”. I want everytime the second value to go on next line but to remain in the same cell and also dynamically adjust the row height with leaving some margin from top and bottom. I am new to VBA and don’t know how exactly to get it on next line.
This is how it currently looks.

enter image description here

But instead of above, I want it look like this

enter image description here

Here is the VBA code which i have used.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim xRng As Range
    Dim xValue1 As String
    Dim xValue2 As String
    If Target.Count > 1 Then Exit Sub
    On Error Resume Next
    Set xRng = Me.Range("J2, K2,L2,M2,N2")
    If xRng Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Not Application.Intersect(Target, xRng) Is Nothing Then
        xValue2 = Target.Value
        Application.Undo
        xValue1 = Target.Value
        Target.Value = xValue2
        If xValue1 <> "" Then
            If xValue2 <> "" Then
                If InStr(1, xValue1, xValue2 & ",") > 0 Then
                    xValue1 = Replace(xValue1, xValue2 & ", ", "") ' If it's in the middle with comma
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                If InStr(1, xValue1, ", " & xValue2) > 0 Then
                    xValue1 = Replace(xValue1, ", " & xValue2, "") ' If it's at the end with a comma in front of it
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                If xValue1 = xValue2 Then        ' If it is the only item in string
                    xValue1 = ""
                    Target.Value = xValue1
                    GoTo jumpOut
                End If
                Target.Value = xValue1 & ", " & xValue2
            End If
jumpOut:
        End If
    End If
    Application.EnableEvents = True
End Sub
How to&Answers:

Select Formulas » Defined Names » Name Manager

Replace the Refers to: formula with the following formula:
enter image description here
=OFFSET(Lookups!$A$2,0,0,COUNTA(Lookups!$A:$A)-1)

You can now go nuts with adding and removing values from the Priority list and the dropdowns will have updated values with no additional effort!

To break down the OFFSET formula usage (using List_Priority as the example):

  • Lookups!$A$2: start at cell $A$2 on sheet named “Lookups” which is
    the first value in the list
  • 0: stay in that same row (so still at
    $A$2)
  • 0: stay in that same column (so, again, still at $A$2)

  • COUNTA(Lookups$A:$A)-1: count the number of cells in column A that
    have values and then subtract 1 (the heading cell: “Priority”); grab
    an area that is that tall, starting with the cell currently
    “selected” ($A$2)

Add the Dependent Drop Down

  • On the DataEntry sheet, select cell E6.
  • On the Ribbon, click the Data tab, then click Data Validation..
  • From the Allow drop-down list, choose List.
  • In the Source box, type an equal sign and INDIRECT function,
    referring to the first data cell in the Produce Type column: …
  • Click OK.

Put code on Sheet Lookup

   Private Sub Worksheet_Change(ByVal Target As Range)
                    On Error Resume Next
                    If Not Intersect(Target, Range("E6")) Is Nothing And Target.Cells.Count = 1 Then
                        Application.EnableEvents = False
                        If Len(Target.Offset(1, 0)) = 0 Then ' (1,0) down direction (0,1) right
                            Target.Offset(1, 0) = Target ' (1,0) down direction (0,1) right
                        Else
                            Target.End(xlDown).Offset(1, 0) = Target ' (1,0) down direction (0,1) right
                        End If
                        Target.ClearContents
                        Application.EnableEvents = True
                    End If
    End Sub

Answer:

For
Sion = 400022
You can use Vlookup formula

=VLOOKUP(Table1[Segments];Table1[Segments];1;FALSE)&" = "&VLOOKUP(Table1[Segments];Sheet2!A2:B4;2;FALSE)

I am not getting how to do it for multiselect. This works only when user select single option from the dropdown

Answer:

Another solution. Change Sheet name and ranges and try:

Option Explicit

Sub test()

    Dim strCitys As String
    Dim rng As Range
    Dim arr As Variant, strResults As Variant, City As Variant

    With ThisWorkbook.Worksheets("Sheet1")

        strCitys = .Range("A1").Value
        Set rng = .Range("D1:E3")
        strResults = ""

        If strCitys <> "" Then
            If InStr(1, strCitys, ",") = 0 Then

                strResults = Application.VLookup(strCitys, rng, 2, False)
                If Not IsError(strResults) Then
                    .Range("B1").Value = strCitys & "=" & strResults
                Else
                    .Range("B1").Value = strCitys & "=" & "Missing Code"
                End If

            Else

                For Each City In Split(strCitys, ",")

                    strResults = Application.VLookup(Trim(City), rng, 2, False)

                    If Not IsError(strResults) Then
                        If .Range("B1").Value = "" Then
                            .Range("B1").Value = Trim(City) & "=" & strResults
                        Else
                            .Range("B1").Value = .Range("B1").Value & vbNewLine & Trim(City) & "=" & strResults
                        End If
                    Else
                        If .Range("B1").Value = "" Then
                            .Range("B1").Value = Trim(City) & "=" & "Missing Code"
                        Else
                            .Range("B1").Value = .Range("B1").Value & vbNewLine & Trim(City) & "=" & "Missing Code"
                        End If
                    End If

                Next City

            End If
        Else
            .Range("B1").Clear
            MsgBox "Please select city/ies."
        End If

    End With

End Sub

Results:

enter image description here