提问人:stevie 提问时间:8/14/2023 最后编辑:stevie 更新时间:8/14/2023 访问量:72
谁能告诉我这个 vb.net 代码有什么问题?
Can anyone tell me what is wrong with this vb.net code?
问:
我写了以下内容。
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel
Public Function EachSubTotal(ByVal x As Excel.Range, ByVal y As Excel.Range) As Double
Dim ReturnValue As Double
Dim Done As Boolean
Done = False
ReturnValue = -1
If y.Value2 = "*" And IsNumeric(x.Value2) Then
ReturnValue = x.Value2 / 1.1025
Done = True
ElseIf (y.Value2 >= 0) And IsNumeric(x.Value2) Then
ReturnValue = x.Value2 * (1 - y.Value2)
Done = True
ElseIf (y.Value2 < 0) And IsNumeric(x.Value2) Then
ReturnValue = x.Value2 * (1 + y.Value2)
Done = True
End If
If Not Done And String.IsNullOrEmpty(x.Value2) Then
ReturnValue = -2
End If
Return Math.Round(ReturnValue, 2)
End Function
现在的问题是,一旦 y 有一些数值,我就会得到 #Value 错误。为什么? 提前致谢
以上代码将在导入到Excel的dll中使用。
因此,我为可能遇到类似问题的其他人发布此内容。
Public Function EachSubTotal(ByVal x As Excel.Range, ByVal y As Excel.Range) As Double
Dim ReturnValue As Double
Dim Done As Boolean
Done = False
ReturnValue = -1
If IsNumeric(y.Value2) Then
If (y.Value2 >= 0) And IsNumeric(x.Value2) Then
ReturnValue = x.Value2 * (1 - y.Value2)
Done = True
Else
ReturnValue = x.Value2 * (1 + y.Value2)
Done = True
End If
Else
If y.Value2 = "*" And IsNumeric(x.Value2) Then
ReturnValue = x.Value2 / 1.1025
Done = True
End If
End If
If Not Done And String.IsNullOrEmpty(x.Value2) Then
ReturnValue = -2
End If
Return Math.Round(ReturnValue, 2)
End Function
答: 暂无答案
评论
y.Value2
"*"
y.Value2 = "*"
y.Value2.ToString = "*"