简体   繁体   English

Excel:使用 UDF 时出现单个 #VALUE 错误

[英]Excel: single #VALUE error when using UDF

I know the topic "Excel formulas not updating" has been discussed a lot on many forums but I haven't found a useful solution to my problem.我知道“Excel 公式未更新”这一主题已在许多论坛上进行了大量讨论,但我还没有找到解决我的问题的有用方法。

In a worksheet, I am using built-in Excel formulas as well as own functions written with VBA in the module of the worksheet and I am referencing them within the worksheet.在工作表中,我在工作表的模块中使用内置的 Excel 公式以及自己用 VBA 编写的函数,我在工作表中引用它们。

EDIT:编辑:

There is a binary code which gets generated from a hexadecimal code in cell A1.有一个二进制代码是从单元格 A1 中的十六进制代码生成的。 The binary code gets calculated in cell B1.二进制代码在单元格 B1 中计算。

Let's take following code as an example: 100001101110我们以下面的代码为例:100001101110

Cell C1 contains following:单元格 C1 包含以下内容:

=DecodeVal(B1;0;20) =DecodeVal(B1;0;20)

If I now paste a hex code into A1 and the binary code gets created in B1, cell C1 is displaying an #VALUE!如果我现在将十六进制代码粘贴到 A1 中并在 B1 中创建二进制代码,则单元格 C1 将显示#VALUE! error.错误。

If I go back to cell A1, click in the textbox and press enter again, the correct value (= 2158) gets displayed.如果我回到单元格 A1,单击文本框并再次按 Enter,将显示正确的值 (= 2158)。

Why is there a Value error at first, but not if I press enter one more time?为什么一开始会出现 Value 错误,但如果我再按一次 Enter 就不会了?

This is the function I'm referring to:这是我所指的功能:

Public Function DecodeVal(value, start As Integer, length As Integer) As Long
Dim abschnitt As String
Dim i As Integer
Dim valueText As String

    valueText = value.Text
    If (Len(valueText) - start - length + 1 > 0) Then
        abschnitt = Mid(valueText, Len(valueText) - start - length + 1, length)
    Else
        If (Len(valueText) > start) Then
            abschnitt = Left(valueText, Len(valueText) - start)
            length = Len(valueText) - start
        End If
    End If

    Do
        If (Int(Left(abschnitt, 1)) = 1) Then
            DecodeVal = DecodeVal * 2 + 1
        Else
            DecodeVal = DecodeVal * 2
        End If
        abschnitt = Right(abschnitt, length - 1)
        length = length - 1
    Loop While length > 0


End Function

Yes, calculation options are set to automatic.是的,计算选项设置为自动。

Any suggestions?有什么建议?

Thanks谢谢

I recommend to declare the variables properly and don't use Value as variable name as it can easily get mixed up with Range().Value .我建议正确声明变量,不要使用Value作为变量名,因为它很容易与Range().Value混淆。 Also I recommend always use Long as there is no benefit in using Integer in VBA.此外,我建议始终使用Long因为在 VBA 中使用Integer没有任何好处。

Finally there seems to be some issue with valueText = value.Text .最后, valueText = value.Text似乎存在一些问题。 If you use the .Text of a cell/range object it could interfere with the numberformat that was set to that cell.如果您使用单元格/范围对象的.Text ,它可能会干扰为该单元格设置的数字格式。 I recommend to use ValueText = CStr(ValueCell.Value) instead.我建议改用ValueText = CStr(ValueCell.Value)

So you end up with something like:所以你最终会得到类似的东西:

Option Explicit

Public Function DecodeVal(ValueCell As Range, Start As Long, Length As Long) As Long
    Dim Abschnitt As String
    Dim i As Long
    Dim ValueText As String

    ValueText = CStr(ValueCell.Value)
    If (Len(ValueText) - Start - Length + 1 > 0) Then
        Abschnitt = Mid$(ValueText, Len(ValueText) - Start - Length + 1, Length)
    Else
        If (Len(ValueText) > Start) Then
            Abschnitt = Left$(ValueText, Len(ValueText) - Start)
            Length = Len(ValueText) - Start
        End If
    End If

    Do
        If (CLng(Left$(Abschnitt, 1)) = 1) Then
            DecodeVal = DecodeVal * 2 + 1
        Else
            DecodeVal = DecodeVal * 2
        End If
        Abschnitt = Right$(Abschnitt, Length - 1)
        Length = Length - 1
    Loop While Length > 0
End Function

If that doesn't solve the issue, then your issue is not related to that code but to how you generate the binary and write that into the cell (or if it is a formula then the issue is in the code of that UDF).如果这不能解决问题,那么您的问题与该代码无关,而是与您如何生成二进制文件并将其写入单元格的方式有关(或者如果它是公式,则问题出在该 UDF 的代码中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM