如何添加一个标签和文本框基于在excel中的用户窗体上的组合框选择
问题描述:
我目前正在制定一个用户窗体来创建一个公司用户发送到我的部门的订单。如何添加一个标签和文本框基于在excel中的用户窗体上的组合框选择
目前我陷入了僵局,因为我正在努力解决以下问题。
我有一个组合框,它有我们业务提供的产品列表。基于选择,我希望能够添加要求用户输入数据的标签和文本框。
如果选择在组合框中,然后 输入名称,要求的日期,用户等位置
而且这需要具体到组合框的选择。
任何帮助,将不胜感激:)
UPDATE
道歉,因为我没有该功能我没加任何这里是我的代码有任何代码。
Private Sub CommandButton1_Click()
Windows("RFS User Form Mock.xlsm").Visible = True
End Sub
Private Sub LegendDefinition_Change()
LegendDefinition.Locked = True
End Sub
Private Sub RequestList_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Definition").Cells(i, "A").Value = (Me.RequestList) Then
Me.DefinitionBox = Sheets("Definition").Cells(i, "B").Value
End If
Next
End Sub
Private Sub RequestList_DropButtonClick()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
If Me.RequestList.ListCount = 0 Then
For i = 2 To LastRow
Me.RequestList.AddItem Sheets("Definition").Cells(i, "A").Value
Next i
End If
End Sub
Sub UserForm_Initialize()
SiteList.List = Array("Birmingham", "Bristol", "Cardiff", "Chelmsford", "Edinburgh", "Fenchurch Street", "Glasgow", "Guernsey", "Halifax", "Homeworker", "Horsham", "Ipswich", "Jersey", "Leeds", "Leicester", "Lennox Wood", "Liverpool", "Manchester", "Peterborough", "Redhill", "Sunderland", "Madrid")
End Sub
Private Sub VLookUp_Change()
VLookUp.Locked = True
End Sub
答
发布问题时,您需要提供一些代码,显示您正在尝试解决问题的位置。不过这是一个简短的演示,它会给你一个起点。
创建一个新的用户窗体并在其上放置组合框,标签和文本框;确保它们分别命名为ComboBox1,Label1和TextBox1。
然后,粘贴此代码的形式的模块:
Option Explicit
Private Sub ComboBox1_Change()
Dim bVisible As Boolean
'Only show the label and the textbox when the combo list index is 1, which corresponds to "Item 2".
'Note: bVisible = (ComboBox1.Text = "Item 2") would also work.
bVisible = (ComboBox1.ListIndex = 1)
Label1.Visible = bVisible
TextBox1.Visible = bVisible
End Sub
Private Sub UserForm_Layout()
'Populate the combo.
ComboBox1.AddItem "Item 1", 0
ComboBox1.AddItem "Item 2", 1
'Note: the code below could be removed by setting the corresponding
'design-time properties from the form designer.
ComboBox1.Style = fmStyleDropDownList
Label1.Visible = False
TextBox1.Visible = False
End Sub
然后按F5显示形式。您会注意到标签和文本框仅在组合显示“项目2”时才可见。可见性调整在ComboBox1_Change
事件处理程序中执行。
如果您打算根据组合的值显示/隐藏多个控件,您可以将它们分组为一个或多个Frame
控件,并显示/隐藏这些框架。
代码是否添加了帮助?我会把用户界面的图像,但我没有足够的代表。此外,我正在使用VLookup与相同的组合框。该组合框称为RequestList。 –