Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:46:38+00:00 2026-06-07T03:46:38+00:00

I have the following code. I get a zero when I try to add

  • 0

I have the following code. I get a zero when I try to add two numbers:

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

I ran this code when each description belonged to furniture category. When I run it the values of all the variables remain 0. When I opened the debugger and viewed the values of variables it showed the values of variable in line:

Line:    `furnitureTotal = furnitureTotal + Val(total)`
Values:         0                  0            1750

I cannot understand why it does not add two double values. Is this related to the lifetime of variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:46:39+00:00Added an answer on June 7, 2026 at 3:46 am

    2 things I see – you use total, but this is a keyword used in a listcolumn object, and you call getcategory repeatedly, when a vlookup could replace the code.

    Here’s the code, slightly rewritten:

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: // GET: /PlayRoundHole/Create public ActionResult Create(int id) { DB
If I have the following code in a function, I will not get an
I have the following code to get the object type of a collection: Dim
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have the following code to get details of a contact. data: is the
I have following code that does not work: I never get to goToFoodDetail .
I have a following simple code: def get(): return [lambda: i for i in
I have written a following code to get just the file name without extension
I have used the following code to get the document root. $path = get_file_dir();
I have the following code: $.ajax({ type: 'GET', url: 'index.php?route=checkout/onepagecheckout/getpaypaldata', dataType: 'json', success: function(json)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.