我有两个用户表单(实际上更多,但这两个是我目前正在使用的那些)。
第一种形式称为“ExistingOrNewWelder”(我称之为表格A )。
第二种形式称为“InitialInfo_Form”(我称之为表格B )。
当用户单击工作表上的按钮时,将打开表单A.最初,它看起来像这样:
当用户选择“为现有焊工添加新WQTR”旁边的单选按钮时,会出现一个Combobox。
当选择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
答案 0 :(得分: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)。就这么简单!两个变量都是Public,当用户进行选择时,它们都被赋值。它们都由表格B的后续脚本访问。
有一点需要注意的是,我使用Me.Hide来隐藏表单A而不是卸载它。这样,它仍然被卸载,并且我不会丢失用户在组合框中选择的值。