从内存中的数组计算

时间:2014-02-03 17:52:47

标签: excel excel-vba vba

我对VBA相当新,但了解基础知识。我的问题如下:

我需要将数组的各个单元格与其对应的偏移单元格(E3 / E2,F3 / F2,G3 / G2等)分开并将其存储在一个数组中。然后,我需要找到该数组的第1,第2和第3个最小数字,并突出显示该列第一行中的单元格。这就是我所拥有的:

Option Base 1

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    'Private Sub test5()

    Dim row As Integer
    Dim column As Integer
    Dim myArray(10) As Double
    Dim myArray1(3) As String
    Dim a As Long
    Dim b As Long
    Dim intQuizNumber As Integer
    Dim intTestNumber As Integer
    Dim intProjectNumber As Integer


    intQuizNumber = 3
    intTestNumber = 3
    intProjectNumber = 3

    On Error Resume Next
    If Not Intersect(Target, Range(Range("D3"), Range("D3").End(xlDown))) Is Nothing Then

        Range("1:1").Interior.Color = xlNone
        row = ActiveCell.row
        column = ActiveCell.column

        For a = 1 To 10
            myArray(a) = Cells(row, column + 1) / Cells(2, column + 1)
            column = column + 1
        Next a

        row = ActiveCell.row
        column = ActiveCell.column

        'Evaluate("=RANK(E3,$E$3:$N$3,0)+COUNTIF($E$3:E3,E3)-1")
        For b = 1 To 3
            myArray1(b) = Evaluate("=CELL(""address"",OFFSET(" & Target.Offset(0, 1).Address & ",0,MATCH(SMALL(" & Target.Offset(0, 1).Address & ":" & Target.Offset(0, 3 + 3 + 3 + 1).Address & "," & b & ")," & Target.Offset(0, 1).Address & ":" & Target.Offset(0, 3 + 3 + 3 + 1).Address & ",0)-1))")
        Next b

        Union(Range(myArray1(1)).Offset(-row + 1, 0), Range(myArray1(2)).Offset(-row + 1, 0), Range(myArray1(3)).Offset(-row + 1, 0)).Interior.Color = 65535

    Else
        Range("1:1").Interior.Color = xlNone
    End If

End Sub

我想将“b”循环中的Evaluate语句替换为我已注释掉的但似乎无法执行此操作的语句。我首先需要除法的价值,然后我需要得到三个最低点并突出显示单元格。我彻底搜索了谷歌并且无法解决这个问题。任何帮助将不胜感激!!

谢谢

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要使用RANK代替你拥有的东西,但这是获得你想要的另一种方式。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim i As Long
    Dim vaNums As Variant, vaDenoms As Variant, aDivs() As Double
    Dim wf As WorksheetFunction
    Dim lSmall As Long
    Dim rRow As Range
    Dim rStart As Range

    Const lCOLS As Long = 10
    Const lMARKCNT As Long = 3

    If Not Intersect(Target.Cells(1), Me.Range("D3", Me.Range("D3").End(xlDown))) Is Nothing Then

        Set wf = Application.WorksheetFunction ' this just makes our code easier to read

        'If these ever change, you only have to change them in one place
        Set rRow = Target.Cells(1).Offset(0, 1).Resize(1, lCOLS)
        Set rStart = Me.Cells(1, 5)

        'Clear existing colors
        rStart.Resize(1, lCOLS).Interior.ColorIndex = xlNone

        'Read the current line and the 2nd line into arrays
        'This shortcut creates two-dimensional arrays
        vaNums = rRow.Value
        vaDenoms = rStart.Offset(1, 0).Resize(1, lCOLS).Value

        'Do the division and store it in aDivs()
        ReDim aDivs(LBound(vaNums, 2) To UBound(vaNums, 2))
        For i = LBound(vaNums, 2) To UBound(vaNums, 2)
            aDivs(i) = vaNums(1, i) / vaDenoms(1, i)  + (i / 10000)
        Next i

        'Find the nth smallest value and gets its position with MATCH
        'Then use that position to color the cell
        For i = 1 To 3
            lSmall = wf.Match(wf.Small(aDivs, i), aDivs, False)
            rStart.Offset(0, lSmall - 1).Interior.Color = vbYellow
        Next i

    End If

End Sub