0

我有一个Sub运行良好的程序。我想将其转换为自定义函数,但在 Excel 中使用此函数时#VALUE!出现错误 ( )

Function ТридцатьТРи(Diapozon As Integer)
    'для п/пр

    Dim k, n As Integer
    Dim parRange As Range

    Set parRange = Range("Diapozon")
    k = 0
    n = 0
    For Each Cell In parRange.Rows
        If Cell.Offset(0, 1).Value = 1 And k = -1 Then
            n = n - 1
        End If
        If Cell.Value = 1 And k = -1 Then
            n = n + 1
        End If

        If Cell.Value = 1 Then
            k = k + 1
            If k = 2 Then
                k = -1

            End If
        End If
        If Cell.Value = 2 Or Cell.Value = 3 Then
            k = 0
        End If
    Next Cell

    ТридцатьТРи = n

End Function

功能

4

2 回答 2

1

这将作为一个工作表功能,您需要输入您之前使用命名范围“diapozon”定义的范围作为输入范围。

Function cyrillic(rng As Range)

    Dim k, n As Integer
    Dim parRange As Range

    Set parRange = rng
    k = 0
    n = 0
    For Each Cell In parRange.Rows

    If Cell.Offset(0, 1).Value = 1 And k = -1 Then
    n = n - 1
    End If

    If Cell.Value = 1 And k = -1 Then
    n = n + 1
    End If

    If Cell.Value = 1 Then
    k = k + 1
    If k = 2 Then
    k = -1

    End If
    End If
    If Cell.Value = 2 Or Cell.Value = 3 Then
    k = 0
    End If


    Next Cell

    cyrillic = n

End Function

只需输入 :=cyrillic("R1:RX")它应该可以工作。

于 2017-06-27T06:12:54.403 回答
1

试试下面的UDF代码(不确定你想用 UDF 中的逻辑实现什么),但它可以工作(没有得到#VALUE!)。

由于您想将Range对象传递给 UDF(根据您的屏幕截图),因此您还需要在Function代码中定义它。

代码

Function cyrilic(Diapozon As Range) As Long

    Dim k As Long, n As Long
    Dim C As Range

    k = 0
    n = 0

    For Each C In Diapozon.Rows
        If C.Offset(0, 1).Value = 1 And k = -1 Then
            n = n - 1
        End If

        If C.Value = 1 And k = -1 Then
            n = n + 1
        End If

        If C.Value = 1 Then
            k = k + 1
            If k = 2 Then
                k = -1
            End If
        End If
        If C.Value = 2 Or C.Value = 3 Then
            k = 0
        End If
    Next C

    cyrilic = n

End Function
于 2017-06-27T07:02:33.307 回答