无法在自定义函数中获取单元格范围的值

时间:2019-05-23 18:38:22

标签: excel vba excel-formula

我正在研究一个需要输入数组并且输出也是数组的问题。我编写了VBA代码(在笔记本上完成逻辑工作之后),但我不知道为什么该代码无法获取输入值。

我已经编写了代码。只要帮我语法! 由于我不是专业编码人员,因此请忽略变量的命名。另外,我是这个平台的新手,所以我不知道很多规则。任何帮助将不胜感激。谢谢!

测试用例和错误:

enter image description here

Public Function Get1_GEM(vector As Range)
Dim i As Long, j As Long, i1 As Long, j1 As Long
Dim a As Long, b As Long, c As Long
Dim GEM() As Double
ReDim GEM(1 To 3, 1 To 2) As Double
Dim res() As Double
ReDim res(1 To 3) As Double
Dim row() As Double
ReDim row(1 To 3) As Double
For i = 1 To 3 Step 1
    For j = 1 To 2 Step 1
        GEM(i, j) = 2 * i + j - 2
        row(i) = 0
    Next j
Next i
Dim inp() As Double
ReDim inp(1 To 3) As Double
i = 0
j = 0
Dim x() As Double
ReDim x(1 To 3) As Double
Dim y() As Double
ReDim y(1 To 3) As Double
i = 0
For i = 1 To 3 Step 1
    inp(i) = vector.Cells(i, 1)
Next i

For i = 1 To 3 Step 1
    For j = 1 To 2 Step 1
        If GEM(i, j) = inp(1) Then
            x(1) = i
            y(1) = j
            row(i) = row(i) + 1
        ElseIf GEM(i, j) = inp(2) Then
            x(2) = i
            y(2) = j
            row(i) = row(i) + 1
        ElseIf GEM(i, j) = inp(3) Then
            x(3) = i
            y(3) = j
            row(i) = row(i) + 1
        End If
    Next j
Next i
i = 0
j = 0
If row(1) > 0 And row(2) > 0 And row(3) > 0 Then
    For i = 1 To 3 Step 1
        res(i) = GEM(x(i), 2 - y(i))
    Next i
Else

    If row(1) = 0 Then
        a = 0
    ElseIf row(2) = 0 Then
        a = 1
    ElseIf row(3) = 0 Then
        a = 2
    End If

    If row(1) = 1 Then
        b = 0
    ElseIf row(2) = 1 Then
        b = 1
    ElseIf row(3) = 1 Then
        b = 2
    End If
    c = 3 - a - b
    Dim d As Double
        If x(1) = b Then
            d = 0
        ElseIf x(2) = b Then
            d = 1
        ElseIf x(3) = b Then
            d = 2
        End If
i = 0
For i = 1 To 3 Step 1
    If i = d Then
        res(i) = GEM(x(i), 2 - y(i))
    Else
        res(i) = GEM(a, y(i))
    End If
Next i
End If
Get1_GEM = res
End Function

0 个答案:

没有答案