VBA Multicolumn Combox – 当第一列是列表中的绑定列时,获取第二列的值

我有两个用户表单(实际上更多,但这两个是我目前正在使用的)。

第一种forms被称为“ExistingOrNewWelder”(我将称之为表格A )。

第二种forms被称为“InitialInfo_Form”(我将称之为表格B )。

当用户单击工作表上的button时,表单A被打开。 最初,它看起来像这样:

表格A的初始视图

当用户select“为现有焊工添加新WQTR”旁边的单选button时,出现Combobox。

组合框显示在两个列表的形式

当selectComboBox中的一个项目时,只有列表中的绑定列的值显示在框中。

显示只有绑定列数据可见的ComboBox

所以我在挣扎的地方是我想能够使用ComboBox中显示的名字和ID号码。 我希望这两条信息都出现在用户单击确定时显示的后续表单B中。

下一个需要显示数据的用户表单。

在图片中,Foo这个词代表了表格A中的ID号码,您可以在下面的代码中看到这个词。

这第一段代码是Form A

Option Explicit Dim newWelder As Boolean Dim wqtr As Boolean Public newWelderBoolValue As Boolean Public welderIDSelected As String Private Sub UserForm_Initialize() 'varialbe fun Dim lastRow As Long Dim nameCell As range Dim box As control wqtr = False newWelder = False 'Set Window size and position With Application .WindowState = xlMaximized Me.Top = .Top * 0.5 Me.Left = .Left * 1.0015 Zoom = Int((.Width * 0.85) / (Width * 0.85) * 60) Width = .Width * 0.28 Height = .Height * 0.5 End With 'Activate the worksheet Worksheets("All Welders Data").range("A1").Activate 'sort the data in the active sheet by the welder's name then by welder's ID number With ActiveSheet.Sort .SortFields.Add Key:=range("E3"), Order:=xlAscending .SortFields.Add Key:=range("B3"), Order:=xlAscending .SetRange ActiveCell.CurrentRegion.Offset(1) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .Apply End With 'populate the combox from the active sheet (welder name in the 'first column, welder ID number in the second column. With ActiveSheet lastRow = .Cells(.Rows.count, "A").End(xlUp).row For Each nameCell In .range("E3:E" & lastRow) If nameCell.Value <> "" Then With Me.chooseWelderNameComboBox .ColumnCount = 2 .AddItem nameCell.Value .list(.ListCount - 1, 1) = nameCell.Offset(, -1).Value 'ComboBox now shows the values in column "E" and the values 'in coulmn "D" - in that order, as in "Name" - "ID Number". '(the reverse order of the columns in the worksheet.) End With End If Next End With End Sub Private Sub existingWelderOptionButton_Click() 'display the welderName Combox when this radio button 'is selected and set the switches(bools) for the Submit button. wqtr = True newWelder = False Me.chooseWelderLabel.Visible = True Me.chooseWelderNameComboBox.Visible = True Me.chooseWelderNameComboBox.Enabled = True End Sub Private Sub AddNewWelderOptionButton_Click() 'When this radio button is selected set 'the switches(bools) for the Submit button. wqtr = False newWelder = True End Sub Private Sub chooseWelderNameComboBox_Change() welderIDSelected = "Foo" End Sub Private Sub submitButton_Click() 'Based on the radio button selected, set 'the Public newWelderBoolValue to either true or false 'this is used by InitialInfo_Form.UserForm_Initialize If wqtr = True Then newWelderBoolValue = True InitialInfo_Form.Show Else newWelderBoolValue = newWelder InitialInfo_Form.Show End If Me.Hide End Sub 

然后这个下一个部分只是与这个问题相关的表单B的代码部分。

 Private Sub UserForm_Initialize() Dim welderSelected As Boolean Dim idSelected As String welderSelected = ExistingOrNewWelder.newWelderBoolValue idSelected = ExistingOrNewWelder.welderIDSelected 'Set Window size and position With Application .WindowState = xlMaximized Me.Top = .Top * 0.5 Me.Left = .Left * 1.0015 Zoom = Int((.Width * 0.85) / (Width * 0.85) * 40) Width = .Width * 0.995 Height = .Height * 0.992 End With If welderSelected = True Then Me.welderNameText.Text = ExistingOrNewWelder.chooseWelderNameComboBox.Text Me.welderNameText.Enabled = False Me.welderIDComboBox.Value = idSelected Me.welderIDComboBox.Enabled = False End If welderIDComboBox.list = UserFormDropDownDataSheet.range("J2:J9000").Value weldingProcessComboBox.list = UserFormDropDownDataSheet.range("M2:M13").Value positionWeldedComboBox.list = UserFormDropDownDataSheet.range("O2:O14").Value testNumberComboBox.list = UserFormDropDownDataSheet.range("Q2:Q100").Value End Sub 

好吧,我find了我的答案。 以下是我在多列combobox的未绑定列中访问数据的过程。

在我作为问题的一部分提交的Form A代码是这个子代码:

 Private Sub chooseWelderNameComboBox_Change() welderIDSelected = "Foo" End Sub 

我的问题围绕着取代“Foo”,用任何代码将我的数据在未绑定列。 例如,如果我select了“Allan Bailey”,他的名字将显示在ComboBox字段中,但不是与他的名字相关联的ID号码,因为该Id号码是另一列,并且只有名称列被绑定。 这是解决这个问题的代码:

 Private Sub chooseWelderNameComboBox_Change() welderNameSelected = Me.chooseWelderNameComboBox.column(0) welderIDSelected = Me.chooseWelderNameComboBox.column(1) End Sub 

所以你可以看到我添加了welderNameSelectedvariables,但诀窍是列索引。 绑定列是索引(0),第二列是索引(1)。 就这么简单! 这两个variables都是公共的,当用户进行select时,这两个variables都被分配一个值。 他们都通过后面的表单B脚本访问。

有一点要注意的是,我用Me.Hide来隐藏表单A,而不是卸载它。 这样,它仍然是卸载,我不会丢失用户在combobox中select的值。