将Sub转换为UDF获取(#VALUE!)

时间:2017-06-27 06:02:18

标签: excel vba excel-vba user-defined-functions custom-function

我有一个运作良好的程序#VALUE!。我想将它转换为自定义函数,但是当我在Excel中使用此函数时出现错误(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

{{1}}

Function

2 个答案:

答案 0 :(得分:1)

这将作为工作表函数显示,您需要输入您之前定义的范围,并将命名范围“diapozon”作为输入范围。

RecyclerView

只需输入: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 即可。

答案 1 :(得分: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