我想保存ComboBox1& ComboBox2值作为变量,我可以在用户正确完成Userform后返回的模块中使用,但我不确定如何执行此操作。
Option Explicit
Private isCancelled As Boolean
Public Property Get Cancelled() As Boolean
Cancelled = isCancelled
End Property
Private Sub CancelButton1_Click()
isCancelled = True
Me.Hide
End Sub
Public Property Get Benefit() As String
Benefit = IIf(Me.ComboBox1.ListIndex = -1, vbNullString, Me.ComboBox1.Text)
End Property
Public Property Get Costdelivery() As String
Costdelivery = IIf(Me.ComboBox2.ListIndex = -1, vbNullString, Me.ComboBox2.Text)
End Property
Private Sub ComboBox1_Change()
ValidateForm
End Sub
Private Sub ComboBox2_Change()
ValidateForm
End Sub
Private Sub ValidateForm()
Me.Okbutton1.Enabled = (Benefit <> vbNullString And Costdelivery <> vbNullString)
End Sub
Private Sub UserForm_Activate()
ValidateForm
End Sub
Private Sub UserForm_Initialize()
'populate "Combo-Box with Boards
With Me.ComboBox1
.Clear ' clear previous items (not to have "doubles")
.AddItem "Very High"
.AddItem "High"
.AddItem "Medium"
.AddItem "Low"
End With
With Me.ComboBox2
.Clear ' clear previous items (not to have "doubles")
.AddItem "Very High"
.AddItem "High"
.AddItem "Medium"
.AddItem "Low"
End With
End Sub
Private Sub Okbutton1_Click()
Dim Ben As Long
Ben = Me.ComboBox1.Value ***ERROR
Dim Cost As Long
Cost = Me.ComboBox2.Value **** ERROR
Me.Hide
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
isCancelled = True
Me.Hide
End If
End Sub
理想情况下,我希望将低,中,高或非常高的值存储为值9,10,11,12,因为我将在关闭用户窗体后返回的模块中将这些用作单元格引用。
我知道我需要陈述一个公共财产,我的尝试在下面不起作用;
Public Function ConfidenceChart()
Dim ben As Long, costd As Long
ben = UserForm4.ComboBox1.Text
If ben = "Low" Then ben = 9
If ben = "Medium" Then ben = 8
If ben = "High" Then ben = 7
If ben = "Very High" Then ben = 6
costd = UserForm4.ComboBox2.Text
If costd = "Low" Then costd = 12
If costd = "Medium" Then costd = 13
If costd = "High" Then costd = 14
If costd = "Very High" Then costd = 15
End Function
答案 0 :(得分:3)
Excel控件允许您将控件链接到单元格。通过这种方式,您可以存储控件的值以供以后使用。
Userform控件使用ControlSource
属性来建立此链接。在下面的Demo中,我在设置页面上创建了两个命名范围(以控件和名称命名以便于参考),并将控件ControlSource
属性设置为命名范围。
@Mat&#39; sMug指出OP需要根据ComboBox中的选择来存储查找列表中的值。这也可以使用命名范围来实现。
Private Sub UserForm_Initialize()
'populate "Combo-Box with Boards
With Me.ComboBox1
.RowSource = "List1"
.ColumnCount = 2
.ColumnWidths = "0 pt;49.95 pt"
End With
With Me.ComboBox2
.RowSource = "List2"
.ColumnCount = 2
.ColumnWidths = "0 pt;49.95 pt"
End With
End Sub
您需要更改ComboBox的一些属性。
Private Sub UserForm_Initialize()
'populate "Combo-Box with Boards
With Me.ComboBox1
.RowSource = "List1"
.ColumnCount = 2
.ColumnWidths = "0 pt;49.95 pt"
.ControlSource = "ComboBox1"
End With
With Me.ComboBox2
.RowSource = "List2"
.ColumnCount = 2
.ColumnWidths = "0 pt;49.95 pt"
.ControlSource = "ComboBox2"
End With
End Sub
答案 1 :(得分:2)
Ben
和Cost
是 locals ,它们只能在Click
处理程序中看到。
不是表格&#39;找出这些值最终需要的工作 - 表单仅用于收集用户输入。
您已经拥有调用者可以访问的Benefit
和CostDelivery
个属性。
使用它们!
With New UserForm1 'whatever the name of that form is
.Show
If Not .Cancelled Then
Sheet1.Range("A1").Value = .Benefits
Sheet1.Range("B1").Value = .CostDelivery
End If
End With
如果您需要它们是数字值,那么您不能正确填充组合框。
您无法将"Very High"
映射到12
,组合框只知道字符串。
要做到这一点,你需要改变这个:
With Me.ComboBox1
.Clear ' clear previous items (not to have "doubles")
.AddItem "Very High"
.AddItem "High"
.AddItem "Medium"
.AddItem "Low"
End With
将Me.ComboBox1.List
指定给2D数组。或Range
,如果你有一个看起来像这样:
A B
15 Very High
14 High
13 Medium
12 Low
您可以使用此辅助方法:
Private Sub PopulateFromRange(ByVal control As MSForms.ComboBox, ByVal source As Range, Optional ByVal valueColumn As Long = 1, Optional ByVal hasHeader As Boolean = True)
With control
.ColumnCount = source.Columns.Count
.ColumnWidths = GetColumnWidths(source)
.ListWidth = IIf(control.Width > source.Width, control.Width, source.Width)
.List = source.Range(source.Rows(IIf(hasHeader, 2, 1)).EntireRow, source.Rows(source.Rows.Count).EntireRow).Value
.BoundColumn = valueColumn
End With
End Sub
Private Function GetColumnWidths(ByVal source As Range) As String
Dim cols As Long
cols = source.Columns.Count
Dim widths()
ReDim widths(1 To cols)
Dim col As Long
For col = 1 To cols
widths(col) = source(, col).Width
Next
GetColumnWidths = Join(widths, ",")
End Function
(摘自this post specifically about populating combobox and listbox controls from ranges)
并填充你的组合框:
PopulateFromRange Me.ComboBox1, DataSheet.Range("A1:B5")
PopulateFromRange Me.ComboBox2, DataSheet.Range("D1:E5")
假设您有DataSheet
工作表,其范围[A1:B5]
和[D1:E5]
分别包含每个项目的文本和相应的数值。