Excel VBA组合框多列删除空行和列出的特定值

Excel VBA组合框多列删除空行和列出的特定值,excel,vba,list,combobox,multiple-columns,Excel,Vba,List,Combobox,Multiple Columns,我有一个组合框,其中列出了两列(a和H)。列出项目的条件是: 1.添加不包含A列中的空白行的项目 2.为H列添加不等于零的项 我能够使用此代码执行第一个条件: Private Sub UserForm_Activate() Dim currentCell As Range With ComboBox1 .ColumnCount = 2 .ColumnWidths = "70;30" .ColumnHeads = False .BoundColumn = 1 With Worksheets

我有一个组合框,其中列出了两列(a和H)。列出项目的条件是: 1.添加不包含A列中的空白行的项目 2.为H列添加不等于零的项

我能够使用此代码执行第一个条件:

Private Sub UserForm_Activate()

Dim currentCell As Range

With ComboBox1

.ColumnCount = 2
.ColumnWidths = "70;30"
.ColumnHeads = False
.BoundColumn = 1

With Worksheets("Sheet")
    With .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each currentCell In .Cells
            If Len(currentCell) > 0 Then
                With Me.ComboBox1
                    .AddItem currentCell.Value
                    .List(.ListCount - 1, 1) = currentCell.Offset(, 7).Value
                End With
            End If
        Next currentCell
    End With
End With
End With


End Sub
我尝试将该部分更改为第二个条件,但不起作用:

With Worksheets("Sheet")
    With .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        For Each currentCell In .Cells
            If Len(currentCell) > 0 & currentCell.Offset(, 7).Value <> 0 Then
                With Me.ComboBox1
                    .AddItem currentCell.Value
                    .List(.ListCount - 1, 1) = currentCell.Offset(, 7).Value

与工作表(“工作表”)
带.Range(“A2:A”和.Cells(.Rows.Count,“A”).End(xlUp.Row)
对于.Cells中的每个currentCell
如果Len(currentCell)>0¤tCell.Offset(,7).值为0,则
和我一起
.AddItem currentCell.Value
.List(.ListCount-1,1)=currentCell.Offset(,7).Value
谢谢

私有子用户表单_Initialize()
Private Sub UserForm_Initialize()
Dim Sh As Worksheet, rng As Range, arr(), cL As Range
Set Sh = ThisWorkbook.Worksheets("Sheet1")

'Make union of cells in Column A based on the two conditions given
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
    If Sh.Range("A" & i).Value <> "" And Sh.Range("H" & i).Value <> 0 Then
        If rng Is Nothing Then
        Set rng = Sh.Range("A" & i)
        Else
        Set rng = Union(rng, Sh.Range("A" & i))
        End If
    End If
Next

'Make array of values of rng ang corresponding H Column cells
ReDim arr(rng.Cells.Count - 1, 1)
i = 0
For Each cL In rng
    arr(i, 0) = cL.Value
    arr(i, 1) = cL.Offset(0, 7).Value
    Debug.Print rng.Cells(i + 1).Address; arr(i, 0); arr(i, 1)
i = i + 1
Next

'Assign the array to the ComboBox
ComboBox1.ColumnCount = 2
ComboBox1.List = arr

End Sub
尺寸Sh作为工作表,rng作为范围,arr(),cL作为范围 设置Sh=ThisWorkbook.Worksheets(“Sheet1”) '根据给定的两个条件合并列A中的单元格 对于i=1到范围(“A”&Rows.Count).End(xlUp).Row 如果Sh.Range(“A”&i).Value”和Sh.Range(“H”&i).Value为0,则 如果rng不算什么,那么 设置rng=Sh.Range(“A”和i) 其他的 设置rng=Union(rng,Sh.Range(“A”&i)) 如果结束 如果结束 下一个 '生成rng和相应H列单元格的值数组 ReDim arr(rng.Cells.Count-1,1) i=0 对于rng中的每个cL arr(i,0)=cL.值 arr(i,1)=cL.偏移量(0,7).值 调试.打印注册表单元格(i+1).地址;arr(i,0);arr(i,1) i=i+1 下一个 '将数组分配给组合框 ComboBox1.ColumnCount=2 ComboBox1.List=arr 端接头

在第二种情况下,您只需将“&”替换为“And”,即可使其正常工作。我也会避免在这里嵌套太多的。也许是这样的:

Dim myRange As Range
Dim mySheet As Worksheet
Dim currentCell As Range

lastRow = Cells(Rows.Count, 1).End(xlUp).Row
With Sheets("Sheet3")
    Set myRange = Range(.Cells(2, 1), .Cells(lastRow, 1))
End With

With ComboBox1
    .ColumnCount = 2
    .ColumnWidths = "70;30"
    .ColumnHeads = False
    .BoundColumn = 1

    For Each currentCell In myRange
        If Len(currentCell) > 0 And currentCell.Offset(, 7).Value <> 0 Then
            With Me.ComboBox1
                .AddItem currentCell.Value
                .List(.ListCount - 1, 1) = currentCell.Offset(, 7).Value
            End With
        End If
    Next currentCell
End With
将myRange变暗为范围
将我的工作表设置为工作表
小电流单元作为量程
lastRow=单元格(Rows.Count,1).End(xlUp).Row
附页(“第3页”)
设置myRange=Range(.Cells(2,1),.Cells(lastRow,1))
以
使用ComboBox1
.ColumnCount=2
.ColumnWidths=“70;30”
.ColumnHeads=False
.BoundColumn=1
对于myRange中的每个currentCell
如果Len(currentCell)>0且currentCell.Offset(,7).Value为0,则
和我一起
.AddItem currentCell.Value
.List(.ListCount-1,1)=currentCell.Offset(,7).Value
以
如果结束
下一个电流单元
以