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

我不明白为什么它不添加两个双精度值。这与变量的生命周期有关吗?

4

2 回答 2

1

这不是一个真正的答案,但我不知道如何发布这个。

您可以使用 SUMIF 函数一起避免 VBA。

例如,如果不是家具,则在 L24 中使用以下代码替换用于家具的实际代码。

=SUMIF(F:F,"Furniture",G:G)

使用它,您无需为该单元格再次计算,因为公式将为您处理它。您可以使用类似的公式更新所有其他计算单元格,并且不再需要运行宏。

只要您在 F 列中没有完全包含“家具”且不应包含的具有相应值的行,此特定公式就会为您提供正确的结果。如果你这样做,只需手动输入一个合理的范围。

顺便说一句,您的 Function getCategory 没有做任何有用的事情。如所写,它将返回提供的值(如果所选单元格与提供的值相同,则返回所选单元格的值)或循环,直到您最终尝试选择一个单元格时出现运行时错误不存在。

于 2012-07-03T20:53:17.543 回答
1

我看到的两件事-您使用total,但这是 listcolumn 对象中使用的关键字,并且当 vlookup 可以替换代码时,您反复调用 getcategory 。

这是代码,稍作改写:

Sub Calculate()
Dim furnitureTot As Double
Dim furQuantity As Integer
Dim applianceTot As Double
Dim appQuantity As Integer
Dim whiteGoodsTot As Double
Dim whiteGoodQuantity As Integer
Dim softFurTot As Double
Dim softFurQuantity As Integer
Dim description As String
Dim Tot As Double
Dim quantity As Integer

furnitureTot = 0
furQuantity = 0
applianceTot = 0
appQuantity = 0
whiteGoodQuantity = 0
whiteGoodsTot = 0
softFurTot = 0
softFurQuantity = 0

Sheets("Invoice").Select
Range("F64").Select
Do Until (ActiveCell.Value = "Finish" Or ActiveCell.Value = "")
    description = Application.WorksheetFunction.VLookup(ActiveCell.Value, Range("'Product Master'!A2:B9999"), 2, False) ' lookup value, rather than do it repeatedly, and use built in function
    ActiveCell.Offset(0, 1).Select
    quantity = Val(Selection.Value) ' try to get number, even if it's input as text
    ActiveCell.Offset(0, 6).Select
    Tot = Val(Selection.Value)

    Select Case description ' only 1 test needed
        Case "Furniture"
            furnitureTot = furnitureTot + Tot
            furQuantity = furQuantity + quantity
        Case "Electronics"
            applianceTot = applianceTot + Tot
            appQuantity = appQuantity + quantity
        Case "White Goods"
            whiteGoodsTot = whiteGoodsTot + Tot
            whiteGoodQuantity = whiteGoodQuantity + quantity
        Case "Soft Furnishings"
            softFurTot = softFurTot + Tot
            softFurQuantity = softFurQuantity + quantity
    End Select
    Sheets("Invoice").Select
    ActiveCell.Offset(1, -7).Select
Loop

Sheets("Invoice").Select
Range("L24").Select
Selection.Value = furQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = furnitureTot
Range("L25").Select
Selection.Value = appQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = applianceTot
Range("L26").Select
Selection.Value = whiteGoodQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = whiteGoodsTot
Range("L27").Select
Selection.Value = softFurQuantity
ActiveCell.Offset(0, 1).Select
Selection.Value = softFurTot

End Sub
于 2012-07-03T21:06:28.110 回答