我是 Excel 中的宏和 vba 的新手。目前,我正在为工作中的发票模板开发 vba 宏。但是,我在除以零错误的情况下运行,我无法追踪其原因。
有时会弹出两行特定的代码。
第一部分:
VATRMB = 0
第二部分:
VATRMB = VATRMB + (0.0593 * (ActiveSheet.Range("I" & i).Value / (1 + 0.0593)))
Dim VATRMB 存储如下:
Dim startRow As Integer, endRow As Integer, VATRMB As Single, VATEUR As Single, VATUSD As Single, VATRMBCell As Range, VATEURCell As Range, VATUSDCell As Range
在我看来,这些行不应该引发除以零错误。在第一种情况下,没有任何除数,而在第二种情况下,它总是正数。
你们中有人知道为什么这可能会导致错误吗?是否与 sub 被多次调用,重复使用相同的 VATRMB Dim 的事实有关?每次调用 sub 后都应该重置它,对吗?还是与我将 VATRMB 指定为 Single 的事实有关?这适用于“小”(低于 1,000,000)浮点数,对吗?
编辑:
1. 添加了用于调用 Dim storage 的确切行
2. 这是使用的完整代码块,也许有助于澄清一两件事:
'Debug.Print Tab(10); ("Items will be searched in rows " & startRow & " thru " & endRow) 'Serves for debugging and testing
For i = startRow To endRow 'Loop the following code through all rows mentioned above
If ActiveSheet.Range("B" & i).Find("Membership") Is Nothing Then 'If nothing is returned when searching for "Membership"; i.e. if the item in this row is not a membership payment
If Not ActiveSheet.Range("H" & i).Find("RMB") Is Nothing Then 'If the value for this item is RMB denoted
'Debug.Print Tab(20); "Item on Row " & i & " is RMB denoted, VAT = " & ((ActiveSheet.Range("I" & i).Value / (1 + 0.0593)) * 0.0593) 'Serves for debugging and testing
VATRMB = VATRMB + (0.0593 * (ActiveSheet.Range("I" & i).Value / (1 + 0.0593))) 'Add row's VAT to VAT total
End If
If Not ActiveSheet.Range("H" & i).Find("EUR") Is Nothing Then 'If the value for this item is EUR denoted
'Debug.Print Tab(20); "Item on Row " & i & " is EUR denoted, VAT = " & ((ActiveSheet.Range("I" & i).Value / (1 + 0.0593)) * 0.0593) 'Serves for debugging and testing
'MsgBox VATEUR + 0.0593 * ActiveSheet.Range("I" & i).Value / (1 + 0.0593)
VATEUR = VATEUR + (0.0593 * (ActiveSheet.Range("I" & i).Value / (1 + 0.0593))) 'Add row's VAT to VAT total
End If
If Not ActiveSheet.Range("H" & i).Find("USD") Is Nothing Then 'If the value for this item is USD denoted
'Debug.Print Tab(20); "Item on Row " & i & " is USD denoted, VAT = " & ((ActiveSheet.Range("I" & i).Value / (1 + 0.0593)) * 0.0593) 'Serves for debugging and testing
VATUSD = VATUSD + (0.0593 * (ActiveSheet.Range("I" & i).Value / (1 + 0.0593))) 'Add row's VAT to VAT total
End If
Else 'Else, i.e. if the row contains a membership payment, then essentially nothing happens
'Debug.Print Tab(20); ("Item on Row " & i & " is a membership payment; no VAT paid.") 'Serves for debugging and testing
End If
Next
所以我要做的基本上是遍历发票中的所有项目,从 startRow 到 endRow,并通过解析“类型”字符串(B 列)确定该项目是否为会员付款。然后,根据是否为会员付款确定增值税,还要检查支付的货币。付款金额以浮点数形式存储在 I 列中。