Home » excel » Excel copying value with VBA corresponding to a key after changing a cell

Excel copying value with VBA corresponding to a key after changing a cell

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying the folowing since a few days but due to my lack of VBA skills don’t get it working.

Scenario:

  1. User: Selects a value from dropdown list (cells allow only a list
    defined in another sheet).
  2. Code: Copy the value left to the appropriate list value. (This is a list of names.)
  3. Code: Paste the value into a specific field in sheet one.

Example:
The user is picking the value “Team One” from a dropdownlist in A1 in sheet one. This list is defined on sheet two. Next to each item of the list on sheet two is a cell with a comma separated list of names.
After the user has picked a team from the dropdown list, the corresponding list of names is copied into the field B1 in sheet one.

This procedure should only be fired when A1 is changed.

Hope I could make myself clear. If I finally find the solution myself, I will post it here.

Thank you for reading this.

How to&Answers:

You can do this without VBA. In the field you want the list of names pasted into enter this formula:

=IF(ISBLANK(<address of dropdown on Sheet1>),"",INDEX(<address of list to left of values on Sheet2>,MATCH(<address of dropdown on Sheet1>,<address of dropdown values on Sheet2>,0)))

This will be blank when nothing is selected from the dropdown and will display the appropriate list of names when a value is selected.

For example, if the dropdown is in B1 on Sheet1, the dropdown values are in B1:B9 on Sheet2, and the corresponding list of names are in A1:A9 on Sheet2, you would use this formula:

=IF(ISBLANK(Sheet1!B1),"",INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0)))

EDIT (per comment):
To use this in VBA, you’ll need to do something similar to what @chris neilsen suggested. In the Worksheet module, you’ll need to create a sub for a change event:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then
    Range("A1").Formula = "=INDEX(Sheet2!A1:A9,MATCH(Sheet1!B1,Sheet2!B1:B9,0))"
    If IsError(Range("A1").Value) Then
        Range("A1") = ""
    Else
        Range("A1").Value = Range("A1")
    End If
End If
End Sub

To remove any confusion, A1 is the cell that will display the output.

Answer:

To do this as VBA, you would do something like the following. Per your original question, there is a list on Sheet2 that corresponds to the selection from the dropdown box and populates on Sheet1.

In the VBA editor:

  1. In Sheet1, add the following methods

    Private Sub ComboBox1_Change()
    
    If ComboBox1.Text <> "Select" Then
    
        Dim selVal As String
    
        selVal = ComboBox1.Text
    
        Range("B1").Value = GetList(selVal)
    
    End If
    
    End Sub
    
    Public Function GetList(ByVal Value As String) As Variant
    
    Dim result As Variant
    
    result = Application.VLookup(Value, Worksheets("Sheet2").Range("A1:B100"), 2, False)
    
    GetList = result
    
    End Function
    
  2. In the workbook object code, enter the following method:

     Private Sub Workbook_Open()
    
     With ThisWorkbook.Worksheets("Sheet1").ComboBox1
          .AddItem "Team One"
          .AddItem "Team Two"
          .AddItem "Team Three"
          .AddItem "Team Four"
          .AddItem "Team Five"
          .Text = IIf(.Text = "", "Select", .Text)
        End With
        Worksheets("Sheet1").Activate
    End Sub
    

I should note that you could do this without any vba by simply using a a list control found in the Data Validation option in Excel. When you make a selection change in that, you would then use a standard VLookup in cell B1 to grab the corresponding value(s).

Answer:

To implement this in VBA, use a Change event to monitor the data entry cell.

Assuming you have named your validation data range as ListData, put his in the module for Sheet1

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        [B1] = Application.WorksheetFunction.VLookup(Target.Value, [ListData].Resize(, 2), 2, 0)
    End If
End Sub

Answer:

I would advocate an approach similar to the one that Excellll described, but with VLOOKUP rather than MATCH. To do this, you’d need to have your lists of names to the right of each team’s name. For example:

  |    A    |      B
1 |Team 1   |Albert, Beth
2 |Team 2   |Carlo, Delia
3 |Team 3   |Egbert, Frederika

Now, if the team’s name is at cell B7, you could use this formula to get the associated list of names:

=VLOOKUP(B7, Sheet2!$A$1:$B$3, 2, FALSE)

EDIT
See Doug Glancy’s comment below explaining why Excellll’s solution is better than using VLOOKUP.