2017-10-14 76 views
0

好的...的下面線放入細胞陣列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 

在這一行之前,我需要一個條件並且不知道如何編寫語法。 我想合計第一個元素的所有7個值(lbound)並檢查總和,並對所有元素執行此操作直到(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 
+0

向我們展示你迄今爲止編寫的代碼和告訴我們你遇到的問題。 – QHarr

+0

按元素你是指排?如果循環行是每行= 100或每行總數= 100?後者似乎更有意義。一些示例數據會有所幫助。 – QHarr

+0

謝謝你的回覆。該代碼與我面臨的問題無關。我在我的工作表中得到結果在範圍A1:G250這是問題中提到的行的輸出。我唯一的問題是,而不是隻填寫250行,只填充行數爲100的行數。 – Sabha

回答

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 
+0

這正是我想要的。非常感謝你爲我付出的時間和精力。上帝保佑! – Sabha

+0

如果數組是多維數組以避免/消除tempArr的使用,我很努力地重寫代碼。你能解釋一下,如果數組是10行7列,那麼你將如何編輯代碼?還有什麼會進入'爲totalsdict.Keys'中的每個鍵內部將結果保存在一個二維數組,並最終輸出整個數組結果在A1?謝謝 – Sabha

相關問題