Excel VBA多列列表框添加项目在上面

所有,

有什么简单的方法来添加列表框的开始项目?

我发现了一些关于插入函数的信息,但它似乎在Excel VBA中不可用。

我发现的唯一方法是创buildFOR,将每行移动到+1位置,然后在0索引处插入项目。 我真的希望有一个更简单的方法。

我的长版本看起来像这样:

SSub InsertRecordAtTheTop(sText1, sText2) Dim i With lbCases If .ListCount > -1 Then .AddItem "0" ''' add new row For i = .ListCount - 1 To 1 Step -1 ''' move everything +1 postion .List(i, 0) = .List(i - 1, 0) .List(i, 1) = .List(i - 1, 1) .List(i, 2) = .List(i - 1, 2) .List(i, 3) = .List(i - 1, 3) .List(i, 4) = .List(i - 1, 4) Next i .List(0, 0) = sText1 ''' paste new values at top .List(0, 1) = sText2 .List(0, 2) = "" .List(0, 3) = "" .List(0, 4) = "" Else .AddItem sText1 ''' if listbox is empty, just paste .List(0, 1) = sText2 End If End With End Sub

谢谢,TJ

AddItem使用可选参数来指定索引位置。 默认情况下,它添加到最后。 如果你指定0,它将在开始处添加。 尝试如下所示:

 .AddItem "Some Text", 0 

更多信息可以在这篇MSDN文章中find。