-2

我正在尝试自学有关数组的知识。有人可以使用数组翻译下面的代码并解释吗?

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

谢谢!

4

1 回答 1

0

此代码使用数组来存储您可能根据 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
于 2013-02-20T20:58:31.850 回答