用标题填充列表框

时间:2020-08-18 09:08:31

标签: excel vba

我正在尝试从项目列表中填充一个列表框,我可以填充它,但是它将我的标题行作为列表中的一行,并且标题为空白。我不确定我要去哪里。任何帮助都会很棒。

Sub populateList()
  Dim rngName As Range
  Dim ws As Worksheet
  Dim i As Integer
  Set ws = Worksheets("ProjectData")
  lbTasks.Clear
  lbTasks.ColumnHeads = True
  lbTasks.ColumnCount = 10
  Dim LastRow As Long
  LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
  For i = 1 To LastRow
    If ws.Cells(i, 1).Value <> vbNullString Then lbTasks.AddItem ws.Cells(i, 1).Value
    If ws.Cells(i, 2).Value <> vbNullString Then lbTasks.List(i - 1, 1) = ws.Cells(i, 2).Value
    If ws.Cells(i, 3).Value <> vbNullString Then lbTasks.List(i - 1, 2) = ws.Cells(i, 3).Value
    If ws.Cells(i, 4).Value <> vbNullString Then lbTasks.List(i - 1, 3) = ws.Cells(i, 4).Value
    If ws.Cells(i, 5).Value <> vbNullString Then lbTasks.List(i - 1, 4) = ws.Cells(i, 5).Value
    If ws.Cells(i, 6).Value <> vbNullString Then lbTasks.List(i - 1, 5) = ws.Cells(i, 6).Value
    If ws.Cells(i, 7).Value <> vbNullString Then lbTasks.List(i - 1, 6) = ws.Cells(i, 7).Value
    If ws.Cells(i, 8).Value <> vbNullString Then lbTasks.List(i - 1, 7) = ws.Cells(i, 8).Value
    If ws.Cells(i, 9).Value <> vbNullString Then lbTasks.List(i - 1, 8) = ws.Cells(i, 9).Value
    If ws.Cells(i, 10).Value <> vbNullString Then lbTasks.List(i - 1, 9) = ws.Cells(i, 10).Value
  Next i
End Sub

0 个答案:

没有答案
相关问题