简体   繁体   English

如何使用表的前两列在Excel VBA中的用户窗体上创建两列组合框?

[英]How can I create a two column combobox on a userform in Excel VBA using the first two columns of a table?

I am looking to generate a combobox in a userform that pops up from clicking on a command button on my excel spreadsheet. 我想在用户窗体中生成一个组合框,该组合框通过单击我的Excel电子表格上的命令按钮弹出。 The purpose is to select a date and time (including time in case there are multiple entries for a specific date) from the combobox then click an "Complete Action" command button on the form to change the value from "No" to "Yes" in a cell at the end of the row of the selected entry. 目的是从组合框中选择日期和时间(包括在特定日期有多个条目的情况下的时间),然后单击表单上的“完成操作”命令按钮,将值从“否”更改为“是”。在所选条目行末尾的单元格中。


From the research that I have done so far, this was the only code I was able to comprehend enough to enter in, but I do not get the display results that I was hoping for. 从到目前为止的研究来看,这是我唯一能够理解的代码,但是我没有得到我想要的显示结果。

Private Sub UserForm_Initialize()
Dim v, e
With Sheets("Phone Log").Range("B9:C76")
    v = .Value
End With

With CreateObject("scripting.dictionary")
    .comparemode = 1
    For Each e In v
        If Not .exists(e) Then .Add e, Nothing
    Next
    If .Count Then Me.combobox1.List = Application.Transpose(.keys)
End With


End Sub

Can someone please help with the code to display the dropdown list of the non-blank rows so that the two columns of reference data shows aligned and then take the selected row and change the cell value several columns to the right once the user clicks the "Complete Action" command button? 有人可以帮助代码显示非空白行的下拉列表,以使两列参考数据显示对齐,然后在用户单击“ “完成操作”命令按钮?


I would also be open to the combobox/user form being a separate sub so that I could use it as just selecting the row and running separate functions from command buttons that collect the user selected date. 我也会对组合框/用户表单作为一个单独的子控件保持开放,因此我可以将其用作选择行并从收集用户所选日期的命令按钮运行单独的功能。

TIA ~Decoy TIA〜诱饵

I am not sure if this is what you need, but it should work (i tried it). 我不确定这是否是您需要的,但是它应该可以工作(我尝试过)。

Tried to make it easy to read: 试图使其易于阅读:

Option Explicit

Private Dic As New Scripting.dictionary 'needs a reference to "Microsoft scripting Runtime" in VBE>Tools>Reference

Private Sub CommandButton1_Click()
Dim h$
Dim i&
h = Me.ComboBox1.Value
If h = vbNullString Then Exit Sub
i = Dic(h) 'converts to a Long in this case
ThisWorkbook.Sheets("Phone Log").Cells(i, 3).Value2 = "YES" '3 is the "C" Column
End Sub

Private Sub UserForm_Initialize()
Dim v() 'needs to be a variant
Dim e$ 'is actually a string
Dim i& 'that's a Long

With ThisWorkbook.Sheets("Phone Log").Range("B1:B76") 'i only start at 1, because else i would need to be i-8 later on
    v = .Value 'do NOT use .value2 in case of dates, or currencys
End With

'Set Dic = CreateObject("scripting.dictionary")
With Dic
   ' .comparemode = 1 'didn't find help on this one, not needed i guess
    'For Each e In v
    For i = 9 To 76
       e = v(i, 1)
        If e <> vbNullString Then If Not .exists(e) Then Dic(e) = i '.Add e,i 'or ThisWorkbook.Sheets("Phone Log").cells(i,2).address
    Next i
    If .Count > 0 Then Me.ComboBox1.List = Application.Transpose(.keys) 'i learned something here
End With

Erase v
End Sub

Private Sub UserForm_Terminate()
Set Dic = Nothing
End Sub

EDIT : (sorry for the late answer, was beasy with my own stuff) 编辑:(对不起,我的回答很晚,很抱歉)

The previous code is for a one column combobox. 前面的代码用于一栏组合框。

Here is an example on how to build a 3 column combobox (adjust code to your personal needs) : 这是有关如何构建3列组合框(根据您的个人需要调整代码)的示例:

With Me.Combobox1
    .ColumnCount = 3
    .ColumnWidths = "44;70;30"
    .ListWidth = 150
    .Clear
    .AddItem "*.XLSB"
    .List(0, 1) = "Binary"
    .List(0, 2) = "50"

    .AddItem "*.XLSM"
    .List(1, 1) = "Macro Enabled"
    .List(1, 2) = "52"

    .AddItem "*.XLS"
    .List(2, 1) = "Excel97"
    .List(2, 2) = "56"

    .ListIndex = 0
End With

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

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