繁体   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?

我想在用户窗体中生成一个组合框,该组合框通过单击我的Excel电子表格上的命令按钮弹出。 目的是从组合框中选择日期和时间(包括在特定日期有多个条目的情况下的时间),然后单击表单上的“完成操作”命令按钮,将值从“否”更改为“是”。在所选条目行末尾的单元格中。


从到目前为止的研究来看,这是我唯一能够理解的代码,但是我没有得到我想要的显示结果。

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

有人可以帮助代码显示非空白行的下拉列表,以使两列参考数据显示对齐,然后在用户单击“ “完成操作”命令按钮?


我也会对组合框/用户表单作为一个单独的子控件保持开放,因此我可以将其用作选择行并从收集用户所选日期的命令按钮运行单独的功能。

TIA〜诱饵

我不确定这是否是您需要的,但是它应该可以工作(我尝试过)。

试图使其易于阅读:

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

编辑:(对不起,我的回答很晚,很抱歉)

前面的代码用于一栏组合框。

这是有关如何构建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