我有以下代码。当我尝试添加两个数字时,我得到一个零:
Public Function getCategory(Value As String, Optional col As Long = 1) As String
Sheets("Product Master").Select
Range("A2").Select
Dim I As Long
Do Until Selection.Value = Value
ActiveCell.Offset(1, 0).Select
Loop
getCategory = Selection.Value
End Function
Sub Calculate()
Dim furnitureTotal As Double
Dim furQuantity As Integer
furnitureTotal = 0
furQuantity = 0
Dim applianceTotal As Double
Dim appQuantity As Integer
applianceTotal = 0
appQuantity = 0
Dim whiteGoodsTotal As Double
Dim whiteGoodQuantity As Integer
whiteGoodQuantity = 0
whiteGoodsTotal = 0
Dim softFurTotal As Double
Dim softFurQuantity As Integer
softFurTotal = 0
softFurQuantity = 0
Dim description As String
Dim total As Double
Dim quantity As Integer
Sheets("Invoice").Select
Range("F64").Select
Do Until (ActiveCell.Value = "Finish" Or ActiveCell.Value = "")
description = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
quantity = Selection.Value
ActiveCell.Offset(0, 6).Select
total = Selection.Value
If (getCategory(description) = "Furniture") Then
furnitureTotal = furnitureTotal + Val(total) <---------- this line
furQuantity = furQuantity + quantity
End If
If getCategory(description) = "Electronics" Then
applianceTotal = applianceTotal + total
appQuantity = appQuantity + quantity
End If
If getCategory(description) = "White Goods" Then
whiteGoodsTotal = whiteGoodsTotal + total
whiteGoodQuantity = whiteGoodQuantity + quantity
End If
If getCategory(description) = "Soft Furnishings" Then
softFurTotal = softFurTotal + total
softFurQuantity = softFurQuantity + quantity
End If
Sheets("Invoice").Select
ActiveCell.Offset(1, -7).Select
Loop
Sheets("Invoice").Select <---------------------- breakpoint was placed here
Range("L24").Select
Selection.Value = furQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = furnitureTotal
Range("L25").Select
Selection.Value = appQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = applianceTotal
Range("L26").Select
Selection.Value = whiteGoodQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = whiteGoodsTotal
Range("L27").Select
Selection.Value = softFurQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = softFurTotal
End Sub
当每个描述都属于家具类别时,我运行了此代码。当我运行它时,所有变量的值都保持为 0。当我打开调试器并查看变量的值时,它显示了变量的值:
Line: `furnitureTotal = furnitureTotal + Val(total)`
Values: 0 0 1750
我不明白为什么它不添加两个双精度值。这与变量的生命周期有关吗?