从下拉框中select值,但填充隐藏的列

我有一组唯一的ID和名称:

ID NAME aa Jeff bb Matt cc Trung dd Trung 

所有ID都是唯一的。 名字不是。

在工作表上,我有一系列的列:

 Date Time ID Name Value 1/1 1:30 aa Jeff 123124 1/2 2:20 cc Trung 12443234 

现在,用户将填充ID字段,vlookup将返回Name。

是否有一种方法来设置显示ID和名称串联的ID单元格上的下拉菜单,但选中时只存储ID?

这个想法是,出现在下拉列表(例如:aa | Jeff)的连接值更加用户友好,只是“aa”。

那么,希望我的回答会有所帮助。 如果没有,请告诉我,我会尽力改善它。

代码在Worksheet中

 Private Sub ComboBox1_Change() 'Just use the frist part of the string, ID Range("I1").Value = Split(Me.ComboBox1.Value, " | ") 'Optional, if you want to put the name using code. 'If not, just use the VLOOLUP Range("J2").Value = Split(Me.ComboBox1.Value, " | ")(1) End Sub Private Sub Worksheet_Activate() Dim r Dim c Dim i Dim L Dim myRngID As Range r = Range("C2").End(xlDown).Row 'the final row of the ID column c = Range("D2").Column ' the number of the column of the name Set myRngID = Range(Cells(2, 3), Cells(r, 3)) 'use only the ID range 'Just to clean the ComboBox everytime to avoid duplicates Me.ComboBox1.Value = "" L = Me.ComboBox1.ListCount On Error Resume Next For i = 0 To L With Me.ComboBox1 .RemoveItem i End With Next i On Error GoTo 0 'Pupulate the ComboBox with the string {ID[space]|[space]NAME} For Each i In myRngID With Me.ComboBox1 .AddItem i.Value & " | " & i.Offset(0, 1).Value End With Next i End Sub 

在这个工作表中

一个图像胜过千言万语

正如你所看到的,表中唯一的公式是J1 ,VlookUp。 在J2 ,名称是使用VBA插入的。 combobox有任何特殊的属性。 一切都在上面的代码中。

结果是名称总是从ComboBox中取出,然后不pipeselect哪一个,总会是正确的,就像在VlookUp中一样。

这应该工作

  1. select第二张单元格(将是vlookup的单元格)
  2. 转到数据选项卡>数据validation>数据validation
  3. 在设置select列表>点击input框右侧的来源button>从第一个工作簿屏幕截图select范围
  4. 现在将该单元格复制并粘贴到第二个工作簿的列中