【问题标题】:VBA Macro : get the maximum match cell value to a group of criteriaVBA宏:获取一组条件的最大匹配单元格值
【发布时间】:2021-08-18 04:31:43
【问题描述】:

我有 4 组标准要匹配。 如果要从 sheet1.range("A1:J1") 检查的范围数据包含:10 种水果。 我想知道哪个组与范围数据的匹配度最高。

例如:enter image description here

我尝试了以下代码,但不起作用:

Sub Get_the_relevant_group()
    Dim wb As Workbook
    Dim sh As Worksheet
    Dim searchList As Variant
    Dim searchVal As Variant, val2check As Variant
    Dim rng2check As Range
    Dim cell2check As Range
    Dim iMatch As Integer
    
    
    Set wb = ActiveWorkbook
    Set sh = wb.ActiveSheet
    
    iCol = sh.Cells(1, Columns.Count).End(xlToLeft).Column
    Set rng2check = sh.Range(sh.Cells(1, 1), sh.Cells(1, iCol))
    

    GroupA = Array("Apple", "Banana", "Coconut", "Grape", "Orange", "Guava", "Durian", _
         "Blackcurrent", "Mango", "Strawberry")
    GroupB = Array("Apple", "Coconut", "Guava", "Mango", "Strawberry", "Lime", "Grape", "Pear", "Blueberry", "Lemon")
    GroupC = Array("Apple", "Grape", "Durian", "Pineapple", "Watermelon", "Blueberry", "Banana", "Lemon")
    GroupD = Array("Apple", "Orange", "Lime", "Plums", "Pear", "Lemon", "Coconut", "Grape")
        
    For iGroup = 1 To 4
        If iGroup = 1 Then searchList = GroupA
        If iGroup = 2 Then searchList = GroupB
        If iGroup = 3 Then searchList = GroupC
        If iGroup = 4 Then searchList = GroupD
        
        For Each cell2check In rng2check
            If cell2check <> "" Then
                For Each searchVal In searchList
                    val2check = cell2check.Value
                    If InStr(1, val2check, searchVal) > 0 Then
                        iMatch = iMatch + 1
                        Exit For
                    End If
                Next searchVal
             End If
            If iMatch = iCol Then Exit For
        Next cell2check
    Next iGroup
End Sub

我希望有人能给我解决方案。非常感谢

【问题讨论】:

  • 使用Option Explicit 强制声明所有变量(例如Dim iCol As Long,...)。在For iGroup = 1 To 4 之后添加行iMatch = 0,在Next cell2check 之后添加行Debug.Print "Group" &amp; iGroup &amp; " Total = " &amp; iMatch。现在您可以在立即窗口 (Ctrl+G) 中看到一些结果。请分享您期望代码执行的操作,例如:是否应该是 Sub 并执行某项操作,是否应为 Function 并返回某些内容...

标签: arrays excel vba


【解决方案1】:

这是我的做法(我已经对这些行进行了评论,以便您了解它在做什么):

Sub Get_the_relevant_group()

Dim ws As Worksheet, SearchArr, SearchString As String, i As Long, j As Long
Dim iGroup, iMatch As Long, iCounter As Long, iCol As Long

Set ws = ThisWorkbook.ActiveSheet

iCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column
'Below turns the search list into a 1d array
SearchArr = Application.Transpose(Application.Transpose(ws.Range(ws.Cells(1, 1), ws.Cells(1, iCol)).Value))
SearchString = Join(SearchArr, ",") 'Turn the 1d array into a string which can be searched using Instr

For i = 7 To 10 'Use row numbers the groups are on
    iGroup = Split(ws.Range("C" & i).Value, ",") 'Split the group into an array
    iMatch = 0 'Reset the counters
    iCounter = 0
    For j = LBound(iGroup) To UBound(iGroup) 'From start of array to end
        iGroup(j) = Replace(iGroup(j), " ", "") 'remove any spaces if it does
        If iGroup(j) <> "" Then 'Checks it is not blank
            iCounter = iCounter + 1
            If InStr(SearchString, iGroup(j)) > 0 Then 'See if current item is found in search string
                iMatch = iMatch + 1
            End If
        End If
    Next j
    ws.Range("J" & i).Value = iCounter 'Send results to the sheet for the group
    ws.Range("K" & i).Value = iMatch
Next i
    
End Sub

使用For i = 7 To 10,如果您有更多的组要进入,您当然可以找到最后一行,因此这可能是一个变量。

【讨论】:

  • 谢谢。它几乎完全解决了我的问题,但可能是我没有清楚地描述它。我使用我的个人宏检查另一个工作表,因此结果不需要显示在审查表中。我只从我的个人宏中运行,包括组列表,并在消息框中得到该审查表与 B 组相关的结果。
  • 对不起,我不太明白你在说什么。我是根据您提供的图片制作的。
  • 另一个问题:如果我想知道第二个匹配单元格值的列号,我是否写:如果 iCounter = 1 那么 matchVal1 = ws.Cells(1, iCounter) ,如果 iCounter = 2 然后 matchval2 = ws.Cells(1, iCounter) 等。你有最好的方法吗?
  • 我的意思是搜索范围 (ws.range("A1:J1") 是另一个供审查的工作簿。我将宏和组条件保存在 Personal.xls 中。但没关系我会找到完成它的方法。谢谢你的帮助。你能帮我解决之前评论中的其他问题吗
  • 它不起作用,因为 iCounter 和 iMatch 只是计数器,它们不返回匹配水果的列。添加到我的代码中的最简单方法是创建一个数组,该数组在每次匹配时都会被重新调暗和扩展。所以你会得到 MatchArray(1,1) = first match 和 MatchArray(2,1) = second match 等等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2015-08-12
相关资源
最近更新 更多