需要打开带有组合框列表但已选择值的用户窗体
Need to open a userform with combobox lists but with values already selected
我有一个工作正常的用户表单。现在我需要编辑我使用用户窗体选择的一些值,所以我想用已经选择的值打开用户窗体。我已经有了在某些工作表中保存值的用户表单,我尝试打开相同的用户表单,但在打开用户表单时选择了我已经选择的值。
我该怎么做?这是我的代码:
Private Sub CommandButton1_Click()
Worksheets("Info").Activate
With Worksheets("Info").Range("A:A")
Set c = .Find(ComboBox1.Value, LookIn:=xlValues)
If Not c Is Nothing Then
Dim cliente As String
Dim prov As String
Dim dia As Integer
Dim mes As String
Dim año As Long
Dim prod As String
Dim fob As Long
Dim hon As Variant
Dim fee As Variant
Dim cont As Integer
Dim est As Integer
cliente = c.Offset(0, 1).Value
prov = c.Offset(0, 2).Value
dia = c.Offset(0, 3).Value
mes = c.Offset(0, 4).Value
año = c.Offset(0, 5).Value
prod = c.Offset(0, 6).Value
fob = c.Offset(0, 7).Value
hon = c.Offset(0, 8).Value
fee = c.Offset(0, 9).Value
cont = c.Offset(0, 10).Value
est = c.Offset(0, 21).Value
'
End If
End With
'HERE STARTS THE PROBLEM!!!
' Now I open the userform, I need comboboxs open with the old values
UserForm1.Show
UserForm1.ComboBox1.Value = cliente
UserForm1.ComboBox1.Value = prov
UserForm1.ComboBox1.Value = dia
UserForm1.ComboBox1.Value = mes
UserForm1.ComboBox1.Value = año
UserForm1.TextBox1.Value = prod
UserForm1.TextBox1.Value = fob
UserForm1.TextBox1.Value = hon
UserForm1.TextBox1.Value = fee
UserForm1.TextBox1.Value = cont
UserForm1.ComboBox1.Value = est
End Sub
要向 ComboBox
添加项目,请使用以下语法:
ComboBox1.AddItem "your value"
像这样:
UserForm1.ComboBox1.AddItem = cliente
要显示默认项目,请使用其索引:
UserForm1.ComboBox1.ListIndex = 2
如果只有文本,请使用循环:
For i = 1 To ComboBox1.ListCount
If (ComboBox1.List(i) = myValue) Then
ComboBox1.ListIndex = i
Exit For
End If
Next i
UserForm1.Show
默认为模态,因此您需要在显示表单之前设置其属性。目前的代码是显示表单,然后在表单退出后,创建一个 new UserForm1(旧的已经卸载),设置它的属性,然后让它出去的范围。试试这个:
Dim form_one As UserForm1
Set form_one = New UserForm1
temp.Controls("ComboBox1").Value = cliente
'etc...
form_one.Show
如果我对工作流程的理解正确,更简单的解决方案可能是 .Hide
在用户选择所有内容后而不是卸载表单。
我有一个工作正常的用户表单。现在我需要编辑我使用用户窗体选择的一些值,所以我想用已经选择的值打开用户窗体。我已经有了在某些工作表中保存值的用户表单,我尝试打开相同的用户表单,但在打开用户表单时选择了我已经选择的值。
我该怎么做?这是我的代码:
Private Sub CommandButton1_Click()
Worksheets("Info").Activate
With Worksheets("Info").Range("A:A")
Set c = .Find(ComboBox1.Value, LookIn:=xlValues)
If Not c Is Nothing Then
Dim cliente As String
Dim prov As String
Dim dia As Integer
Dim mes As String
Dim año As Long
Dim prod As String
Dim fob As Long
Dim hon As Variant
Dim fee As Variant
Dim cont As Integer
Dim est As Integer
cliente = c.Offset(0, 1).Value
prov = c.Offset(0, 2).Value
dia = c.Offset(0, 3).Value
mes = c.Offset(0, 4).Value
año = c.Offset(0, 5).Value
prod = c.Offset(0, 6).Value
fob = c.Offset(0, 7).Value
hon = c.Offset(0, 8).Value
fee = c.Offset(0, 9).Value
cont = c.Offset(0, 10).Value
est = c.Offset(0, 21).Value
'
End If
End With
'HERE STARTS THE PROBLEM!!!
' Now I open the userform, I need comboboxs open with the old values
UserForm1.Show
UserForm1.ComboBox1.Value = cliente
UserForm1.ComboBox1.Value = prov
UserForm1.ComboBox1.Value = dia
UserForm1.ComboBox1.Value = mes
UserForm1.ComboBox1.Value = año
UserForm1.TextBox1.Value = prod
UserForm1.TextBox1.Value = fob
UserForm1.TextBox1.Value = hon
UserForm1.TextBox1.Value = fee
UserForm1.TextBox1.Value = cont
UserForm1.ComboBox1.Value = est
End Sub
要向 ComboBox
添加项目,请使用以下语法:
ComboBox1.AddItem "your value"
像这样:
UserForm1.ComboBox1.AddItem = cliente
要显示默认项目,请使用其索引:
UserForm1.ComboBox1.ListIndex = 2
如果只有文本,请使用循环:
For i = 1 To ComboBox1.ListCount
If (ComboBox1.List(i) = myValue) Then
ComboBox1.ListIndex = i
Exit For
End If
Next i
UserForm1.Show
默认为模态,因此您需要在显示表单之前设置其属性。目前的代码是显示表单,然后在表单退出后,创建一个 new UserForm1(旧的已经卸载),设置它的属性,然后让它出去的范围。试试这个:
Dim form_one As UserForm1
Set form_one = New UserForm1
temp.Controls("ComboBox1").Value = cliente
'etc...
form_one.Show
如果我对工作流程的理解正确,更简单的解决方案可能是 .Hide
在用户选择所有内容后而不是卸载表单。