【问题标题】:VBA Multicolumn Combox - get the value of second column when the first column is the bound column in the listsVBA多列组合框 - 当第一列是列表中的绑定列时获取第二列的值
【发布时间】:2018-01-14 17:29:26
【问题描述】:

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

第一种形式称为“ExistingOrNewWelder”(我称之为Form A)。

第二种形式叫做“InitialInfo_Form”(我称之为Form B)。

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

当用户选择“为现有焊工添加新 WQTR”旁边的单选按钮时,会出现一个组合框。

当ComboBox中的一个项目仅选择列表中的绑定列的值时显示在框中。

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

在图像中,单词 Foo 代表表格 A 中的 ID 号,您将在我的当前代码中看到该单词,我将其包含在下面。

第一段代码用于表格 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

【问题讨论】:

  • 如何将您的welderIdselected 设置为一个全局变量,这应该使您能够从同一工作簿的不同子中调用它。或者不将 sub 设置为 private,而是设置为 public。
  • welderIDSelected 已经是第二个脚本中 IDSelected 引用的公共变量。我无法弄清楚的是如何使welderIDSelected =“ComboBox列表中的焊工ID号 - 这是ComboBox中的第二列数据。” @Luuklag - 每个焊工都有一个唯一匹配的 ID 号,并与他的名字相匹配。我虽然想在工作表中进行列与列的比较,但在我看来,应该有一种方法可以访问组合框中的第二列。如果选择了 Joe Bob,他的 ID 号就在列表中。

标签: excel vba forms combobox


【解决方案1】:

好的,我找到了答案。这是我为访问多列组合框的未绑定列中的数据所做的操作。

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

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

我的问题围绕着用任何可以让我获得未绑定列中的数据的代码替换“Foo”。例如,如果我选择“Allan Bailey”,他的姓名将显示在 ComboBox 字段中,但不会显示与他的姓名关联的 ID 号,因为该 ID 号是另一列,并且只有名称列被绑定。这是解决这个问题的代码:

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

所以您可以看到我添加了welderNameSelected 变量,但诀窍是列已编入索引。绑定的列是索引 (0),第二列是索引 (1)。就这么简单!这两个变量都是公共的,并且当用户进行选择时,它们都被分配了一个值。它们都被表单 B 的后续脚本访问。

需要注意的是,我使用 Me.Hide 来隐藏 Form A 而不是卸载它。这样,它保持卸载状态,并且我不会丢失用户在组合框中选择的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2021-08-09
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多