消除listbox rowsource中的空白单元格

时间:2017-01-10 16:36:43

标签: excel vba excel-vba listbox

我正在尝试创建一个消除行上空白单元格的列表框。在A列中,我有一些包含数据的单元格和一些空白的单元格。我无法删除空行,因为在其他列中它们包含数据。如何将非空单元格设为rowsource中的listbox

2 个答案:

答案 0 :(得分:1)

检查每个单元格中是否有值的循环怎么样:

Dim CountLng as Long

'Set CountLng to maximum row in column A that you would like to search for.
'This example uses the number of rows in the entire used range of the worksheet

CountLng = ActiveSheet.UsedRange.Rows.Count

With listbox1

    ' Loop over each cell in the column A 
    For x = 1 To CountLng

        ' If the cell is not blank then add it as a list item
        If ActiveSheet.Range("A" & x).Value <> "" Then

            .AddItem ActiveSheet.Range("A" & x).Value

        End If

    Next x

End With

答案 1 :(得分:0)

为避免重复,请在下面使用以下附加代码:

    ' If the cell is not blank then add it as a list item
        If ActiveSheet.Range("A" & x).Value <> "" Then
          ' Avoid Duplicates   
             If ActiveSheet.Range("A" & x) <> ActiveSheet.Range("A" & x).Offset(-1, 0).Value 
            Then

     .AddItem ActiveSheet.Range("A" & x).Value

    End If
    End If