【问题标题】:Creating combobox with only one of each value from worksheet VBA Excel仅使用工作表 VBA Excel 中的每个值之一创建组合框
【发布时间】:2015-10-09 09:45:11
【问题描述】:

我正在尝试创建一个包含工作表中数据的组合框。我有执行此操作的代码,但我需要的是仅显示给定列中的每个值之一。例如,在 A 列中,我有多个狗、猫和鱼,我希望组合框显示的是 3 个列表,即狗、猫、鱼。例如,如何让它停止显示狗、狗、狗、猫、猫、鱼、鱼、鱼、鱼。下面是我目前正在使用的代码。

    With Worksheets("RuleID")
    OriginatingDomainComboBox.List = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).row).value
    End With

任何帮助都会很棒,如果您现在可能需要任何其他帮助,请告诉我。

谢谢

【问题讨论】:

    标签: vba list excel combobox


    【解决方案1】:

    除了@mielk 的回答,您还可以使用字典来完成您所追求的。

    以下使用“Microsoft Scripting Runtime”参考。请记住在工具 -> 参考中启用此功能。

    Option Explicit
    Sub populateUF()
        Dim dict As Scripting.Dictionary, myItem As Variant
        Dim lrow As Long, i As Long
        Dim myValues() As Variant
    
        Set dict = New Scripting.Dictionary
        lrow = Cells(Rows.Count, 1).End(xlUp).Row
        myValues = Range(Cells(2, 1), Cells(lrow, 1))
        For i = 1 To UBound(myValues, 1)
            If Not dict.Exists("Item" & myValues(i, 1)) Then
                dict.Item("Item" & myValues(i, 1)) = myValues(i, 1)
            End If
        Next i
    
        For Each myItem In dict
            UserForm1.ComboBox1.AddItem dict.Item(myItem)
        Next myItem
    
        UserForm1.Show
    End Sub
    

    我们正在使用.Existsmethod 来评估数组中的值是否先前已添加到字典中。新值被添加到字典中,从而只分配数组中的唯一值。然后我们使用for each 语句遍历字典,为组合框赋值。

    有关字典的进一步阅读,请参阅文档here,以及更详细的文档here

    【讨论】:

      【解决方案2】:

      以下是完成此任务的方法:

      Public Sub loadValues()
          Dim lastRow As Long
          Dim rng As Excel.Range
          Dim rawData As Variant
          Dim columnItems() As String
          Dim arraySize As Integer
          Dim i As Long
          Dim uniqueItems() As Variant
          '-------------------------------------------------------------------
      
      
          'Find data range
          With Worksheets(1)
              lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
              Set rng = .Range(.Cells(1, 1), .Cells(lastRow, 1))
          End With
      
      
          'Get data from worksheet.
          rawData = rng   '<-- for better performance we assign all the data to
                          '    Variant array and we iterate through this array
                          '    later instead of iterating through worksheet's cells
      
      
          'Convert rawData array to 1D array of strings.
          arraySize = UBound(rawData, 1) - LBound(rawData, 1) + 1
          ReDim columnItems(1 To arraySize)
          For i = LBound(columnItems) To UBound(columnItems)
              columnItems(i) = rawData(i, 1)
          Next i
      
      
          'Get unique values from [columnItems] array by using function [uniqueValues].
          uniqueItems = uniqueValues(columnItems)
      
      
          'Assign array of unique values as a list to ComboBox.
          cmbTest.List = uniqueItems
      
      
      End Sub
      

      为了使此方法正常工作,您需要包含function to get unique values from the given array

      【讨论】:

      • 你我的朋友是个天才。非常感谢你做的这些。它就像一种享受。你惊人的。谢谢:):) @mielk
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      相关资源
      最近更新 更多