【问题标题】:Unique values two columns combobox vba唯一值两列组合框 vba
【发布时间】:2020-05-22 04:38:33
【问题描述】:

我需要在具有唯一值的组合框中显示两列 A 和 B。因此,如果两行具有相同的 A 但不同的 B,则它不是重复的,两列都需要重复。我找到了一个代码,其中列出了具有唯一值的一列 (A),但我不知道如何添加列 B。

有一张我的数据的图片以及我想如何在我的 ComboBox 中显示它。

代码如下:

Private Sub UserForm_Initialize()

    Dim Cell        As Range
    Dim col         As Variant
    Dim Descending  As Boolean
    Dim Entries     As Collection
    Dim Items       As Variant
    Dim index       As Long
    Dim j           As Long
    Dim RngBeg      As Range
    Dim RngEnd      As Range
    Dim row         As Long
    Dim Sorted      As Boolean
    Dim temp        As Variant
    Dim test        As Variant
    Dim Wks         As Worksheet

        Set Wks = ThisWorkbook.Worksheets("Sheet1")

        Set RngBeg = Wks.Range("A3")
        col = RngBeg.Column

        Set RngEnd = Wks.Cells(Rows.Count, col).End(xlUp)

            Set Entries = New Collection
            ReDim Items(0)

            For row = RngBeg.row To RngEnd.row
                Set Cell = Wks.Cells(row, col)
                    On Error Resume Next
                        test = Entries(Cell.Text)
                        If Err = 5 Then
                            Entries.Add index, Cell.Text
                            Items(index) = Cell.Text
                            index = index + 1
                            ReDim Preserve Items(index)
                        End If
                    On Error GoTo 0
            Next row

        index = index - 1
        Descending = False
        ReDim Preserve Items(index)

            Do
                Sorted = True

                For j = 0 To index - 1
                    If Descending Xor StrComp(Items(j), Items(j + 1), vbTextCompare) = 1 Then
                        temp = Items(j + 1)
                        Items(j + 1) = Items(j)
                        Items(j) = temp

                        Sorted = False
                    End If
                Next j

                index = index - 1
            Loop Until Sorted Or index < 1

        ComboBox1.List = Items


End Sub

有什么线索吗?谢谢!

【问题讨论】:

  • 你能更好地解释一下“两列都需要重复”是什么意思吗?在同一行上,A 列中的 val 是否足以与 B 不同?
  • 我的意思是,除了同一行的条件不同之外,是否有必要每列只包含唯一值?如果之前要进行过滤(为了识别每一列的唯一值,可能会改变同一行上的值之间的对应关系,恐怕......

标签: excel vba combobox multiple-columns unique-values


【解决方案1】:

请试试这个代码。它假设 unique 定义意味着来自同一行的两列的值对是唯一的

Sub UnicTwoValInTwoColumns()
  Dim sh As Worksheet, arr As Variant, arrFin As Variant, countD As Long
  Dim lastRow As Long, i As Long, j As Long, k As Long, boolDupl As Boolean

  Set sh = ActiveSheet 'use here your sheet
  'supposing that last row in column A:A is the same in column B:B
  'If not, the last row for B:B will be calculated and then the higher will be chosen:
  lastRow = sh.Range("A" & Rows.Count).End(xlUp).Row
  ReDim arrFin(1 To 2, 1 To lastRow)    'redim the final array for maximum possible number of elements
  arr = sh.Range("A3:B" & lastRow).value 'pun in array the range to be analized
  k = 1 'initialize the first array element number

  For i = 1 To UBound(arr, 1) 'iterate between the array elements
    boolDupl = False  'initialize the variable proving that the pair of data already in arrFin
    For j = 1 To k    'iterate between the arrFin elements in order to check for duplicates
        If arr(i, 1) & arr(i, 2) = arrFin(1, j) & arrFin(2, j) Then
              boolDupl = True: Exit For 'if a duplicate is found the loop is exited
        End If
    Next j
    If Not boolDupl Then 'load the arrFin only if a duplicate has not been found
        arrFin(1, k) = arr(i, 1): arrFin(2, k) = arr(i, 2)
        k = k + 1        'increment the (real) array number of elements
    End If
  Next
  ReDim Preserve arrFin(1 To 2, 1 To k - 1) 'redim array at the real dimension (preserving values)
  With Me.ComboBox1
      .ColumnCount = 2 'be sure that combo has 2 columns to receive values
      .List = WorksheetFunction.Transpose(arrFin) 'fill the combo with the array elements
  End With
End Sub

您可以将代码粘贴到表单初始化事件中,或者让Sub照原样,将其复制到表单模块中,然后仅从讨论中的事件中调用它。我建议你以这种方式进行。如果您在活动中有(或将有)其他事情,我认为如果发生问题,识别问题会更简单,

【讨论】:

  • 感谢您的帮助,但您建议的代码在组合框中显示了两列,但显示了重复项。
  • @我清楚地问过你关于它的问题定义,但你没有说什么。即使是现在,您也只会说“显示重复项”。我还根据代码的工作原理解释了假设。那么,在这种情况下,“重复”意味着什么?你能根据什么规则来解释避免重复吗?如果我之前过滤每一列以获得唯一值,那么同一行上两列之间的对应关系将会丢失。这方面我问的很清楚,你却什么都没说……我感觉你根本没看过……
  • @Lawrence231:如果难以用语言解释您的需求,我建议您编辑您的问题,并在您的两列中放一张图片(相关的),另一张向我们展示如何你想看后处理。不一定来自组合框。在其他两个 Excel 列中就足够了...
  • @你说的对,我没见过你以前的cmets!如果我说我的意思是如果我在 A 列中有两行具有相同的值,并且如果这些行在 B 列具有相同的值,这意味着它们是重复的,是否更清楚?
  • @Lawrence231:是的,在看到你的照片后(也)你的问题变得清晰了。我将调整答案代码以使其解决您的需求。
猜你喜欢
  • 2017-08-30
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
相关资源
最近更新 更多