【问题标题】:excel check whether a combination of cells in a row exists in a rangeexcel检查一个区域内是否存在一行中的单元格组合
【发布时间】:2015-06-24 17:53:30
【问题描述】:

我环顾四周找不到它。

假设我有一个由这些唯一字符串组成的数组:

Dim uniques(10) As String

现在这些字符串中的每一个都表示在电子表格的 A 列中。

我还有一个从 1 到 365 的整数范围,代表 B 列中的唯一值。第 i 天 i

我的难题是: 我想知道是否存在 A 为 uniques(1) 且 b 为 i 的行 我想通过所有独特的和所有可能的 i 来迭代这个过程

我正在寻找一种有效的方法来做到这一点,比循环遍历所讨论的范围、每个唯一的、每一天更好的方法。 作为记录,我的实际数据正在计算一年中的每一天以及某个事件发生后的天数,因此我的行数可以增长到 365^2 甚至更多,如果事件发生在一年前.

COUNTIFS 似乎工作得很好。我希望我可以去:

numOfOccurences = Sheet1.Countifs(countrange,dayrange,uniques(1),irange,i)

如果 numOfOccurences 大于 0,那么我知道它存在。

或者是否有一个函数可以将 Range 分成 vba 数组中的行? 所以它看起来像这样 {[A2,B2],[A3,B3],...} 我可能会造成一些损害,因为两列都是排序的,A 然后 B。我只是不期待我自己的函数。

感谢所有想法

【问题讨论】:

  • “第 i 天”是什么意思?你的意思是,例如B100 包含数字 100?
  • 没有 B100 可以是任何东西。 i 只是 0-365 范围内的数字,如果事件发生已超过一年,则更多

标签: vba excel


【解决方案1】:

类似这样的工作。它可能不是最佳的,但显示了如何遍历范围内的行:

Sub test()
    Dim count As Long, i As Long
    Dim myRow As Range
    Dim uniques As Variant
    uniques = Array("a", "b", "c", "d", "e", "f", "g", "h", "i") 'for example
    i = 12 'for example
    For Each myRow In Range("A1:B365").Rows
        If myRow.Cells(1, 1).Value = uniques(1) And myRow.Cells(1, 2).Value = i Then
            count = count + 1
        End If
    Next myRow
    MsgBoxS count
End Sub

【讨论】:

    【解决方案2】:

    这样的事情应该对你有用,或者至少让你朝着正确的方向开始。它找到所有出现的 10 个唯一值,然后从 B 列中为它们获取相应的数字。我不知道你是如何填充你的唯一值的,所以我只是编造了一些东西。 Range.Find 循环在很大程度上应该与您相关。

    Sub tgr()
    
        Dim rngFound As Range
        Dim arrUnq(1 To 10) As String
        Dim varUnq As Variant
        Dim strFirst As String
        Dim strResults As String
    
        arrUnq(1) = "aaa"
        arrUnq(2) = "bbb"
        arrUnq(3) = "ccc"
        arrUnq(4) = "ddd"
        arrUnq(5) = "eee"
        arrUnq(6) = "fff"
        arrUnq(7) = "ggg"
        arrUnq(8) = "hhh"
        arrUnq(9) = "iii"
        arrUnq(10) = "jjj"
    
        For Each varUnq In arrUnq
            Set rngFound = Columns("A").Find(varUnq, Cells(Rows.Count, "A"), xlValues, xlPart)
            If Not rngFound Is Nothing Then
                strFirst = rngFound.Address
                Do
                    'Here you can check what value is in column B by using rngFound.Offset(, 1).Value
                    'You can use this to compare to something you're looking for, or just record the result as shown in this code
    
                    strResults = strResults & Chr(10) & varUnq & ": " & rngFound.Offset(, 1).Value
    
                    'Advance to the next found instance of the unique string
                    Set rngFound = Columns("A").Find(varUnq, rngFound, xlValues, xlPart)
                Loop While rngFound.Address <> strFirst
            End If
        Next varUnq
    
        If Len(strResults) > 0 Then
            strResults = Mid(strResults, 2) 'Get rid of opening chr(10)
            MsgBox strResults
        Else
            MsgBox "No matches found for any of the 10 unique values"
        End If
    
    End Sub
    

    【讨论】:

    • 我能看到你修改你的答案以制作结果地图,以便 dict["ggg"][7] 等于 true 或类似的东西吗?原谅语法,我是 vb 的新手,以前从未使用过它的地图。
    猜你喜欢
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多