在vba excel中搜索数字

时间:2012-11-30 10:39:27

标签: excel vba

我希望一起搜索几个数字(国家彩票);我必须每次看5个cloumns,有90个数字。 可能性为1.2 1.3 1.4 ... 89.90,我有4005种组合。 algorythm工作得很好,但搜索的时间绝对无法管理。是否有可能加快研究进程?

对于amb = 2到4006

    primo = Foglio3.Cells(amb, 1)
    secondo = Foglio3.Cells(amb, 2)

    ritardo = 0

    For cont = 8618 To 2 Step -1

        est1 = Foglio2.Cells(cont, 2)
        est2 = Foglio2.Cells(cont, 3)
        est3 = Foglio2.Cells(cont, 4)
        est4 = Foglio2.Cells(cont, 5)
        est5 = Foglio2.Cells(cont, 6)

        If (primo = est1) Or (primo = est2) Or (primo = est3) Or (primo = est4) Or (primo = est5) Then
            If (secondo = est1) Or (secondo = est2) Or (secondo = est3) Or (secondo = est4) Or (secondo = est5) Then
                Foglio3.Cells(amb, 3) = ritardo     '3 = nazionale
                Exit For
            End If

        End If

        ritardo = ritardo + 1

    Next cont

Next amb

1 个答案:

答案 0 :(得分:1)

第一步是在每个循环中停止使用工作表和VBA。因此,将数据存储在一个数组中,然后迭代内存数组。如果需要,可以更改变体以在工作表上包含数据类型。注意:需要更改Foglio2和Foglio3的范围参考以适应您的数据集。

Dim foglio2() As Variant, foglio3() As Variant
Dim i As Double

Dim primo As Variant, secondo As Variant
Dim est1 As Variant, est As Variant, est3 As Variant, est4 As Variant, est5 As Variant

Dim resultArray() As Variant

foglio3 = Foglio3.Range("A2").CurrentRegion
foglio2 = Foglio2.Range("A2").CurrentRegion

For i = 2 To UBound(foglio2) ' maybe change to 4006?

    primo = foglio2(1, 1)
    secondo = foglio2(1, 2)

    ' change J to 8616?
    For j = UBound(foglio3) To 2 Step -1

        est1 = foglio3(j, 2)
        est2 = foglio3(j, 3)
        est3 = foglio3(j, 4)
        est4 = foglio3(j, 5)
        est5 = foglio3(j, 6)

        ReDim Preserve resultArray(i)
        If (primo = est1) Or (primo = est2) Or (primo = est3) Or (primo = est4) Or (primo = est5) Then
            If (secondo = est1) Or (secondo = est2) Or (secondo = est3) Or (secondo = est4) Or (secondo = est5) Then

                resultArray(i) = ritardo     '3 = nazionale
                Exit For
            End If

        Else
            resultArray(i) = vbNullString
        End If

        ritardo = ritardo + 1


    Next j

Next i

Foglio3.Cells(2, 3).Resize(UBound(resultArray), 1) = resultArray