线性到阵列

时间:2013-02-20 20:33:05

标签: excel-vba vba excel

我正在努力教自己关于阵列。有人可以使用数组翻译下面的代码并解释吗?

Dim i As Integer
For i = 1 To 100

If Sheets("Sheet1").Range("A" & i) < Sheets("Sheet1").Range("B1") Then
    Sheets("Sheet1").Range("C" & i) = "Less than"

ElseIf Sheets("Sheet1").Range("A" & i) > Sheets("Sheet1").Range("B1") Then
    Sheets("Sheet1").Range("C" & i) = "Greater than"

Else: Sheets("Sheet1").Range("C" & i) = "Equal to"

End If


Next i

谢谢!

1 个答案:

答案 0 :(得分:0)

此代码使用数组来存储您可能根据A列和A列中的单元格比较给出的结果。 B ...

Option Explicit

Sub marine()
    Dim rowNumber As Integer

    ' create an array of strings here
    Dim values(-1 To 1) As String

    ' populate the values
    values(-1) = "less than"
    values(0) = "equal to"
    values(1) = "greater than"


    For rowNumber = 1 To 100
        Dim signValue As Integer
        signValue = Math.Sgn(Sheets("Sheet1").Cells(rowNumber, 1) - Sheets("Sheet1").Cells(rowNumber, 2))

        ' copy the correct value from the array into the cell
        Sheets("Sheet1").Cells(rowNumber, 3).Value = values(signValue)
    Next rowNumber
End Sub