2017-05-28 37 views
0

有没有什么值可以在VBA中设置一个双精度值,以至于任何尝试与它进行数学运算的尝试都会抛出一个错误,导致代码行不完整,而只是移动到下一行呢?如何设置VBA变量在使用时抛出错误?

语境:

我想计算具有相互(数种)的功能 关系值。

其意图是让excel单元格由用户填写,其余的自动填写一旦有足够的信息进行填写即可定义 - 如果可能,请填写。

的任何字段可以填写,以任意顺序...

(一些更多的上下文这里: https://superuser.com/questions/1213399/how-to-solve-for-missing-value-in-equation-in-excel/1213440#1213440

我计划这样做,像这样:

Sub myCalculator() 
    On Error Resume Next '<- a key part of this 

    Dim startPosition As Double 
    Dim endPosition As Double 
    Dim distanceTravelled As Double 

    'either take the value from the cells OR define it with the 
    ' answer ('???') I'm looking for: 
    If IsEmpty(Range("A1").Value) Then 
     startPosition = ??? 
    Else startPosition = Range("A1").Value 
    EndIf 
    If IsEmpty(Range("A2").Value) Then 
     endPosition = ??? 
    Else endPosition = Range("A2").Value 
    EndIf 
    If IsEmpty(Range("A3").Value) Then 
     distanceTravelled = ??? 
    Else distanceTravelled = Range("A3").Value 
    EndIf 

    'now run through the values and fill them if we can: 
    startPosition = endPosition - distanceTravelled 
    endPosition = startPosition + distanceTravelled 
    distanceTravelled = endPosition - startPosition 

    'My idea here is that if say 'endPosition' was set to be ??? 
    'then the first line of code would simply error, and then 
    'ResumeNext and then endPosition would actually be calculated, 
    'ready for the next line which would then be able to actually 
    'use it. 

    'then fill in the cells with the old (untouched) and new info: 
    Range("A1").Value = startPosition 
    Range("A2").Value = endPosition 
    Range("A3").Value = distanceTravelled 

End Sub 

vbNull和Nothing在某个步骤似乎都不会评估为'0'。这意味着我得到了垃圾值,其中使用了零而不是简单地抛出错误并被错过。

我应该指出,在真实情况下,我会循环执行实际数学的部分,以避免在第一遍中没有填充所有值...(另外,矛盾和未定义的情况会被处理。)

这个例子是基本的,组成,它当然可以用更好的方式处理,我只是试图保持简洁的讨论。

我不打算成为一名VBA专业人员;),如果有这样的解决方案,但它会被视为一个闪光,我会很高兴。希望有一个基本的答案,否则?

回答

3

不要使用Double,使用Variant(更具体地说,Variant/Double也许Variant/Error):

Sub myCalculator() 
    'On Error Resume Next '<- Only use this on the lines when you expect to need it 
    Dim startPosition As Variant 
    Dim endPosition As Variant 
    Dim distanceTravelled As Variant 

    'either take the value from the cells OR define it with 
    ' an error value: 
    If IsEmpty(Range("A1").Value) Then 
     startPosition = CVErr(xlErrNA) 
    Else 
     startPosition = Range("A1").Value 
    End If 
    If IsEmpty(Range("A2").Value) Then 
     endPosition = CVErr(xlErrNA) 
    Else 
     endPosition = Range("A2").Value 
    End If 
    If IsEmpty(Range("A3").Value) Then 
     distanceTravelled = CVErr(xlErrNA) 
    Else 
     distanceTravelled = Range("A3").Value 
    End If 

    'now run through the values and fill them if we can: 
    On Error Resume Next 
    startPosition = endPosition - distanceTravelled 
    endPosition = startPosition + distanceTravelled 
    distanceTravelled = endPosition - startPosition 
    On Error GoTo 0 

    'then fill in the cells with the old (untouched) and new info: 
    Range("A1").Value = startPosition 
    Range("A2").Value = endPosition 
    Range("A3").Value = distanceTravelled 
End Sub 
+0

的伎俩,非常感谢! –

相关问题