简体   繁体   English

如何根据excel中用户窗体上的组合框选择添加标签和文本框

[英]How to add a label and textbox based on combo box selection on a userform in excel

I am currently working on a userform to create an order for users at a company to send to my dept.我目前正在处理一个用户表单,为公司的用户创建一个订单,发送给我的部门。

At the moment i have come to a standstill as i am struggling to work out the following.目前我已经陷入停滞,因为我正在努力解决以下问题。

I have a combobox which has a list of products our business offers.我有一个组合框,其中包含我们的业务提供的产品列表。 Based on the selection i want to be able to add labels and textbox which require the user to enter data for example.根据选择,我希望能够添加需要用户输入数据的标签和文本框。

If this selection in the combo box then Enter name, date required, location of user etc.如果在组合框中选择此选项,则输入姓名、所需日期、用户位置等。

Also this needs to be specific to the combobox selection.此外,这需要特定于组合框选择。

Any help would be much appreciated :)任何帮助将不胜感激:)

UPDATE更新

Apologies, as i did not have any code for that function I did not add any Here is the code i have.抱歉,因为我没有该功能的任何代码,所以我没有添加任何代码 这是我拥有的代码。

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

When posting a question, you are expected to provide some code showing where you're standing trying to address the problem.在发布问题时,您需要提供一些代码,显示您在尝试解决问题时所处的位置。 Here's nevertheless a short demo that will give you a starting point.不过,这里有一个简短的演示,可以为您提供一个起点。

Create a new UserForm and put a combobox, a label and a textbox on it;创建一个新的用户窗体并在其上放置一个组合框、一个标签和一个文本框; ensure they're named ComboBox1, Label1 and TextBox1, respectively.确保它们分别被命名为 ComboBox1、Label1 和 TextBox1。

Then, paste this code in the form's module:然后,将此代码粘贴到表单的模块中:

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

Then press F5 to show the form.然后按 F5 以显示表单。 You'll notice that the label and textbox are only visible when the combo shows "Item 2".您会注意到标签和文本框仅在组合显示“项目 2”时可见。 The visibility adjustment is performed within the ComboBox1_Change event handler.可见性调整在ComboBox1_Change事件处理程序中执行。

If you plan on having numerous controls shown / hidden depending on your combo's value, you could group them into one or more Frame controls, and show / hide those frames instead.如果您计划根据组合的值显示/隐藏多个控件,您可以将它们分组为一个或多个Frame控件,然后显示/隐藏这些框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM