私は次のコードを持っています。2つの数値を加算しようとすると、ゼロになります。
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
2つのdouble値が追加されない理由がわかりません。これは変数の寿命に関連していますか?