首页 文章

多列列表框没有显示我想要的数据?

提问于
浏览
0

我一直在编写一个Excel宏,它匹配两个表之间的名称,并在列表框中显示它们 . 它只是匹配名称没有问题,但只要我尝试从其中一个表中包含其他数据,它似乎不再循环 .

这是我的代码:

Set matchedartistcell = Worksheets("Lots").Cells(curcell.Row, columnofregion)
matching_artist = matchedartistcell.Value
For i = 1 To maxmaster
    If Trim(Worksheets("Master").Cells(i, 1)) = Trim(matching_artist) Then
       ListBox1.AddItem (Worksheets("Master").Cells(i, 2))
       ListBox1.List(0, 1) = Worksheets("Master").Cells(i, 4)
       ListBox1.List(0, 2) = Worksheets("Master").Cells(i, 5)
       ListBox1.List(0, 3) = Worksheets("Master").Cells(i, 6)
       End If
Next i

所以基本上,第一列显示了一个名称列表......但是第二列,第三列和第四列显示与第一个匹配在同一行上的最后一个匹配的数据(如果这有意义吗?)

我觉得它不会像我希望的那样循环,但我不能为我的生活弄清楚为什么......任何帮助都会非常感激!!

谢谢!

为清晰起见编辑:

我使用测试工作簿包含一些图片来更好地描述问题

这是工作表上的艺术家的一个例子'Master'
enter image description here

这是工作表的一个例子我'm running the macro on ('很多')
enter image description here

当我为汉娜史密斯运行艺术品的宏时,我希望列表框看起来像

马克史密斯英国1940年1999年
Adam Smith澳大利亚1901年1980年
Hannah Smith加拿大人1982

但是,它返回
enter image description here

马克史密斯获得了汉娜史密斯的国籍和日期,亚当史密斯或汉娜史密斯都没有任何额外的信息 .

1 回答

  • 0

    问题是你总是更新(在你的代码中)ListBox的第一行 . 当 i 从1到该表中的最后一行时,您始终更新 ListBox1.List(0, someColumn) . 因此,您总是更新第0行(这是ListBox中的第一行) .

    尝试一下,让我知道它是否有效:

    Set matchedartistcell = Worksheets("Lots").Cells(curcell.Row, columnofregion)
    matching_artist = matchedartistcell.Value2
    For i = 1 To maxmaster
        If Trim(Worksheets("Master").Cells(i, 1)) = Trim(matching_artist) Then
           ListBox1.AddItem (Worksheets("Master").Cells(i, 2))
           ListBox1.List(ListBox1.ListCount - 1, 1) = Worksheets("Master").Cells(i, 4)
           ListBox1.List(ListBox1.ListCount - 1, 2) = Worksheets("Master").Cells(i, 5)
           ListBox1.List(ListBox1.ListCount - 1, 3) = Worksheets("Master").Cells(i, 6)
        End If
    Next i
    

相关问题