【问题标题】:Adding items to a listbox from a selection in another listbox in excel vba从excel vba中另一个列表框中的选择中将项目添加到列表框
【发布时间】:2020-03-17 23:23:02
【问题描述】:

我想要完成的是从列表框 (SelectHousingList) 中筛选出一个通过 excel 中的表进行筛选,并返回同一行但不同列中的值(很像索引/vlookup)。然后在第一行显示具有相同值的选项列表,并在第二个列表框 (CatalystTypeList) 中显示值。

到目前为止,我的代码如下,但第二个列表框中没有显示任何内容。我没有收到任何错误消息。

第一个列表框的行中有 9 个值,所以我尝试使用 vb 中的 listindex 函数在 excel 的表中查找该值。

CatalystTypeList.Clear

lastrow1 = Sheet3.Cells(Rows.Count, 28).End(xlUp).Row

curVal = SelectHousingList.List(SelectHousingList.ListIndex, 6)

Dim dict2 As Object
Set dict2 = CreateObject("Scripting.Dictionary")
For x = 29 To lastrow1
If Worksheets("Catalyst 2020A1").Cells(x, "D") = curVal Then
    If Not dict.Exists(Worksheets("Catalyst 2020A1").Cells(x, "A").Value) Then
        Me.CatalystTypeList.AddItem Worksheets("Catalyst 2020A1").Cells(x, "A")
        dict(Worksheets("Catalyst 2020A1").Cells(x, "A").Value) = 1
    End If
End If
Next x

【问题讨论】:

  • 您是否尝试过单步执行代码?这可能表明问题。
  • 我尝试过,但我一直在努力完成这一步,因为在此之前我有很多代码。我开始了,但它最终没有进入它所在的私人潜艇。
  • 在这个 sub 中设置一个断点,而不是尝试进入它。如果您仍然没有到达这里,那么这段代码可能不是问题。
  • 谢谢你,我能够单步执行这段代码,它不断地从 if 工作表跳到 End If 而没有经过中间,所以我会在那里看看。跨度>
  • curVal 是否正确。也许将.value 添加到检查范围。

标签: excel vba


【解决方案1】:

在所有 cmets 的帮助下,我弄清楚了如何使用断点来帮助单步执行并更轻松地在我的代码中查找错误。

这是我最后的代码(我把我取出的行作为 cmets 留下了,所以每个人都可以看到我所做的更改)。

CatalystTypeList.Clear

lastrow1 = Worksheets("Catalyst 2020A1").Cells(Rows.Count, 28).End(xlUp).Row

curVal = Worksheets("Catalyst 2020A1").Range("D26").Value   
'SelectHousingList.List(SelectHousingList.ListIndex, 6)

Dim dict2 As Object
Set dict2 = CreateObject("Scripting.Dictionary")
For x = 29 To lastrow1
If Worksheets("Catalyst 2020A1").Cells(x, "D") = curVal Then
    'If Not dict.Exists(Worksheets("Catalyst 2020A1").Cells(x, "A").Value) Then
        Me.CatalystTypeList.AddItem Worksheets("Catalyst 2020A1").Cells(x, "A")
        'dict(Worksheets("Catalyst 2020A1").Cells(x, "A").Value) = 1
    End If
'End If
Next x

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    相关资源
    最近更新 更多