将 Sub 转换为 UDF 获取 (#VALUE!)
Transform Sub to UDF getting (#VALUE!)
我有一个程序 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
这显然可以用作工作表函数,您需要输入您之前使用命名范围 "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")
它应该可以工作。
试试下面的 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
我有一个程序 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
这显然可以用作工作表函数,您需要输入您之前使用命名范围 "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")
它应该可以工作。
试试下面的 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