如何在Excel VBA中使用表的前两列在用户窗体上创build两列combobox?

我正在寻找生成一个用户窗体中的combobox,单击我的Excel电子表格上的命令buttonpopup。 目的是从combobox中selectdate和时间(包括特定date的多个条目的时间),然后单击表单上的“完成操作”命令button将值从“否”更改为“是”在所选条目的行的结尾处的单元格中。


从迄今为止所做的研究来看,这是我能够理解的唯一代码,但是我没有得到我期待的显示结果。

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 

有人可以请帮助代码来显示非空白行的下拉列表,以便两列参考数据显示alignment,然后采取选定的行,并更改单元格的值向右几列,一旦用户单击“完成操作“命令button?


我也将开放的combobox/用户forms是一个单独的子,所以我可以使用它只是select行,并从收集用户selectdate的命令button运行单独的function。

TIA〜Decoy

我不知道这是你需要什么,但它应该工作(我试了一下)。

试图让它容易阅读:

 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 

编辑:(抱歉迟到的答案,是我自己的东西很容易)

前面的代码是针对一列combobox的。

以下是如何构build3列combobox(根据个人需要调整代码)的示例:

 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