简体   繁体   English

如何在单击按钮时将项目添加到组合框?

[英]how to add item to a combobox on button click?

I want to add item to a combobox found in excel worksheet from text box which is located in the user form When button is clicked.i see the value added to combobox but it will become empty when I close and reopens the workbook.can any one help me handling this? 我想将项目添加到从用户窗体中位于文本框中的excel工作表中找到的组合框中。单击按钮时。我看到添加到组合框中的值,但是当我关闭并重新打开工作簿时它将变为空。可以是任何一个帮我处理这个吗?

Thank u for you're fast response first 谢谢您的快速回复

thank you both for your feedback and correction.let me make more clear my concern 感谢您的反馈和指正。让我更清楚地表达我的关注

  1. Create a workbook and save as xlsm. 创建一个工作簿并另存为xlsm。
  2. On the first worksheet define user name as follows: Name: dn_cmb_items Range: ="" 在第一个工作表上,如下定义用户名:名称: dn_cmb_items范围: =""
  3. Using the developer ribbon add an Excel (not ActiveX) combobox onto the Worksheet1 and set its list-by-range to dn_cmb_items 使用开发人员功能区将Excel(非ActiveX)组合框添加到Worksheet1上,并将其按范围列出的列表设置为dn_cmb_items
  4. Open VBA editor and add a user form to the workbook, name it as frm_add_cmb_item and set ShowModal to False . 打开VBA编辑器,并将用户窗体添加到工作簿,将其命名为frm_add_cmb_item并将ShowModal设置为False
  5. Drop a text box to the form and name it tb_item_text . 将一个文本框拖放到该表单并将其命名为tb_item_text
  6. Drop a button to the form, name it cmb_add and from its context menu choose View code . 在表单上放置一个按钮,将其命名为cmb_add ,然后从其上下文菜单中选择View code This creates the click event handler. 这将创建click事件处理程序。
  7. Implement the handler as follows: 实现处理程序,如下所示:
Private Sub cmb_add_Click()
      Dim v_r As Range, v_n As Name
      Set v_n = Names("dn_cmb_items")
      If v_n.Value = "=""""" Then
        v_n.Value = "=" & Worksheets(1).Name & "!$A$1:$A$1"
        v_n.RefersToRange.Value = tb_item_text.Text
      Else
        Set v_r = v_n.RefersToRange
        Set v_r = v_r.Cells(v_r.Rows.Count + 1, 1)
        v_r.Value = tb_item_text.Text
        v_n.Value = "=" & Worksheets(1).Name & "!$A$1:" & v_r.Address(True, True)
      End If
    End Sub
  1. Drop onto the workshet a button, then create/set a macros in the workbook. 在工作表上放一个按钮,然后在工作簿中创建/设置宏。 Implement the created macros with the code frm_add_cmb_item.Show . 使用代码frm_add_cmb_item.Show实现创建的宏。
  2. In the VBA Editor from the Debug menu choose Compile . 在VBA编辑器的“ Debug菜单中,选择“ Compile Then save the VBAProject as well as the workbook. 然后保存VBAProject和工作簿。 That's all for the coding. 这就是编码的全部内容。
  3. Switch to the worksheet, show the form. 切换到工作表,显示表单。
  4. Now when you enter some to the textbox, then click the cmb_add button, a new item will be added to the A column at the end thus changing the value of dn_cmb_items assigned to the combobox on the worksheet. 现在,当您在文本框中输入一些内容,然后单击cmb_add按钮时,新项目将添加到末尾的A列中,从而更改分配给工作表上组合框的dn_cmb_items的值。 See the screenshorts attached: 请参阅随附的屏幕短裤:

Initial state: 初始状态: 初始状态

1 added: 添加了1 加1

2 added: 2添加: 加2

PS I have the ready workbook with all the code. PS我已经准备好所有代码的工作簿。 Where should I upload it? 我应该在哪里上传?

Be specific with your question and always post the relevant code, so that it will become easy to solve it for others. 具体说明您的问题,并始终发布相关代码,以便为他人轻松解决。

If you want to see the data while executing the userform, just write the required data in userform_activate or Initialize. 如果要在执行用户窗体时查看数据,只需将所需的数据写入userform_activate或Initialize。 before executing it will take the values and shows up in the combobox. 在执行之前,它将获取值并显示在组合框中。

the input which you are taking from the worksheet just write those values in another worksheet so that whenever you open the workbook the values will not get erased. 您从工作表中获取的输入只需将这些值写入另一个工作表中,这样无论何时打开工作簿,这些值都不会被擦除。

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

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