阵列馈送ListBox中的额外空白行

时间:2011-08-16 21:06:53

标签: excel vba

这就是驱动它的原因:

 Dim wb As Workbook
 Dim arrList()
 Dim iInstances As Long

 iInstances = 0

 For Each wb In Application.Workbooks
  If wb.Name <> ThisWorkbook.Name Then
   iInstances = iInstances + 1
   ReDim Preserve arrList(iInstances)
   arrList(iInstances) = wb.Name
  End If
 Next

 With Sheets(1)
  .Shapes("ListBox1").Height = 136.5
  .ListBox1.List = arrList
 End With

问题:代码在列表顶部插入一个空行(我不想要)。

2 个答案:

答案 0 :(得分:2)

尝试:

  If wb.Name <> ThisWorkbook.Name Then
      ReDim Preserve arrList(iInstances)       
      arrList(iInstances) = wb.Name
      iInstances = iInstances + 1      
  End If

并将以下内容放在模块的顶部。

Option Base 0

答案 1 :(得分:1)

您正在创建一个空0索引的数组,因此您只需在添加工作簿后递增计数器

For Each wb In Application.Workbooks
   If wb.Name <> ThisWorkbook.Name Then
    ReDim Preserve arrList(iInstances)
    arrList(iInstances) = wb.Name
    iInstances = iInstances + 1
   End If
Next