Excel vba:从具有空行的列获取范围

时间:2016-01-14 16:17:21

标签: excel vba excel-vba


我有一个列,我想根据另一列的值从(使用空单元格)中提取信息 这个question给了我很多帮助,但我现在有一个包含多个空单元格的列,并且公式不起作用......

enter image description here

代码

Private Sub techCombo_Change()

Dim ws As Worksheet
Dim rFound As Range

Dim cCmbBox As MSForms.ComboBox
Set cCmbBox = ActiveWorkbook.Sheets(1).custCombo
cCmbBox.Clear

With Me.cmbSheet
    If .ListIndex = -1 Then Exit Sub
    Set ws = ActiveWorkbook.Sheets(.Text)
End With

With Me.techCombo
    If .ListIndex = -1 Then Exit Sub
    Set rFound = ws.Columns("A").Find(.Text, ws.Cells(ws.Rows.Count, "A"), xlValues, xlWhole)
End With

If Not rFound Is Nothing Then
    If Trim(Len(rFound.Offset(2, 1).Text)) = 0 Then
        cCmbBox.AddItem rFound.Offset(1, 1).Value
    Else
        cCmbBox.List = ws.Range(rFound.Offset(1, 1), rFound.Offset(1, 1).End(xlDown)).Value
    End If
End If

End Sub

显示示例的图像:
因此,选择VoLTE应该使用K1,K3,I7,U6组成一个组合框 enter image description here

1 个答案:

答案 0 :(得分:1)

尝试替换它:

If Not rFound Is Nothing Then
    If Trim(Len(rFound.Offset(2, 1).Text)) = 0 Then
        cCmbBox.AddItem rFound.Offset(1, 1).Value
    Else
        cCmbBox.List = ws.Range(rFound.Offset(1, 1), rFound.Offset(1, 1).End(xlDown)).Value
    End If
End If

用这个:

If Not rfound Is Nothing Then
 LastRow = Ws.Cells(Ws.Rows.Count, rfound.Column + 1).End(xlUp).Row
 j = rfound.Row
 i = 1

 Do While j <= LastRow And rfound.Offset(i) = ""
  If rfound.Offset(i, 1) <> "" Then
   cCmbBox.AddItem rfound.Offset(i, 1)
  End If
  i = i + 1
  j = j + 1
 Loop
End If