【问题标题】:VBA comboBox multicolumn remove blank row and specific value listedVBA组合框多列删除空白行并列出特定值
【发布时间】:2020-08-17 12:42:34
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: excel vba list combobox multiple-columns


    【解决方案1】:

    在您的第二种情况下,您需要做的就是将“&”替换为“And”以使其工作。我也会在这里避免太多嵌套的With。也许是这样的:

    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
    

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-08
        相关资源
        最近更新 更多