【发布时间】: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 号就在列表中。