避免数组中的元素不符合条件

时间:2017-10-14 12:59:10

标签: excel vba excel-vba excel-2010 excel-2007

Ok ...以下行将数组myArr的内容放在单元格A1中

sh.Range("A1").Resize(cnt, 7).Value = myArr

以下是上述行输出的一些示例记录

18  5   1   23  15  7   6
23  5   3   10  18  20  15
19  10  25  12  21  15  23
10  14  11  9   7   25  20
24  15  23  20  11  17  2
7   15  3   16  24  22  13
14  4   15  13  6   23  2
20  11  22  24  14  3   6
17  5   13  15  19  6   22
9   13  15  7   24  3   6

在此之前,我需要一个条件而不确定如何编写语法。 我想将第一个元素(lbound)的所有7个值相加并检查总和并为所有元素(ubound)执行此操作。如果总计等于100

,则运行以上

解决了第一个查询后,我还有一个第二个查询,只列出了总计100个元素加起来的数量,有多少个元素的总和为80个等......通过添加另一个从第一个数组获取信息的数组。计数将介于75到125之间。预期输出应为

75 1
76 0
77 2
.
.
.
125 1

这就是我正在尝试的

Sub foo()
Dim myArr(1 To 10)
Dim cnt As Integer, i As Integer
myArr(1) = "18,5,1,23,15,7,6"
myArr(2) = "23,5,3,10,18,20,15"
myArr(3) = "19,10,25,12,21,15,23"
myArr(4) = "10,14,11,9,7,25,20"
myArr(5) = "24,15,23,20,11,17,2"
myArr(6) = "7,15,3,16,24,22,13"
myArr(7) = "14,4,15,13,6,23,2"
myArr(8) = "20,11,22,24,14,3,6"
myArr(9) = "17,5,13,15,19,6,22"
myArr(10) = "9,13,15,7,24,3,6"
For i = 1 To UBound(myArr)
With Application.WorksheetFunction.Sum = .Sum(.Index(myArr, i, 0))
    'if ....
        '....
    'endif
End With
Range("A1").Resize(cnt, 7).Value = myArr
End Sub

1 个答案:

答案 0 :(得分:1)

请参阅第1部分&继续聊天后的第2部分:Comments moved to chat 这会将您的字符串拆分为其元素,转换为整数和总和。然后它执行检查,看看是否= 100,如果是,则添加到一个数组,然后可以写出到工作表。我不禁想到,使用如此多的阵列可能会有更有效的方法,但还没有想到它。

Sub foo()

Dim myArr(1 To 10)
Dim TotalsArr(1 to 10) 
Dim cnt As Integer, i As Integer

myArr(1) = "18,5,1,23,15,7,6"
myArr(2) = "23,5,3,10,18,20,15"
myArr(3) = "19,10,25,12,21,15,23"
myArr(4) = "10,14,11,9,7,25,20"
myArr(5) = "24,15,23,20,11,17,2"
myArr(6) = "7,15,3,16,24,22,13"
myArr(7) = "14,4,15,13,6,23,2"
myArr(8) = "20,11,22,24,14,3,6"
myArr(9) = "17,5,13,15,19,6,22"
myArr(10) = "9,13,15,7,24,3,6"

Dim tempArr as Variant
Dim Val as Variant
Dim Total as Long
Dim Counter As Long
Dim FinalArr()

第1部分:计算项目总数并检查if = 100,然后添加到结果数组。

Counter = 0
For i = LBound(myArr) to UBound(myArr)
    Total = 0
    tempArr = Split(myArr(i),",")

    For Each Val in tempArr
        Total = Total + Val
    Next Val
    If Total = 100 Then
        ReDim Preserve FinalArr(Counter)
        FinalArr(Counter) = myArr(i)
        Counter = Counter + 1
    End If

    TotalsArr(i) = Total 'store totals for later use in dictionary
Next i    

For i = LBound(FinalArr) to UBound(FinalArr)
    Debug.Print FinalArr(i)
Next i

第2部分:计算每个不同的总数。    我们将使用字典存储每个密钥的计数(总计),并在找到相同的密钥时重写,再次为该值添加一个(即计数)

Dim totalsDict As Scripting.Dictionary 'Tools > references > add in microsoft scripting runtime
Dim key as Variant

Set totalsDict = New Scripting.Dictionary

'If prepopulating with totals to check for in range 75 to 125 ( otherwise comment out next 3 code lines

For i = 75 to 125
    totalsdict.Add i , 0
Next i

For i = LBound(TotalsArr) to UBound(TotalsArr)
        totalsDict(TotalsArr(i)) = totalsDict(TotalsArr(i)) + 1 'will overwrite adding one to value
Next i

For Each key in totalsdict.Keys
    Debug.print key, "," , totalsDict(key)
Next key

End Sub