VBA - Wrong results for a for next loop.

montecarlo2012

Active Member
Joined
Jan 26, 2011
Messages
364
Hello.
This code do not give me any error return; but do not give me the right results either.
I am trying to get the total of oranges sold only, on "C1". My data Fruit's names A, and quantity sold B
and this is the code I tried.
Code:
Sub Fr_Sd()       
  Dim LastRow As Long
                  LastRow = Cells(Rows.Count, 1).End(xlUp).Row
         Dim i As Long, Total As Long
                  For i = 2 To LastRow
                           If Cells(i, 1).Value = "Oranges" Then
                           Total = Total + Cells(i, 2).Value
                           End If
                  Next i
Range("C1").Value = Total
End Sub
Code:
    [TABLE="width: 264"]
<tbody>[TR]
[TD="class: xl63, width: 88"]Fruits[/TD]
[TD="class: xl63, width: 88"]Sold[/TD]
[TD="class: xl64, width: 88"][/TD]
[/TR]
[TR]
[TD="class: xl64"]apples[/TD]
[TD="class: xl63"]8[/TD]
[TD="class: xl63"][/TD]
[/TR]
[TR]
[TD="class: xl64"]bananas[/TD]
[TD="class: xl63"]16[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]24[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]pears[/TD]
[TD="class: xl63"]32[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]mangos[/TD]
[TD="class: xl63"]40[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]papayas[/TD]
[TD="class: xl63"]48[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]mangos[/TD]
[TD="class: xl63"]56[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]64[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]72[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]apples[/TD]
[TD="class: xl63"]80[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]88[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]bananas[/TD]
[TD="class: xl63"]96[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]mangos[/TD]
[TD="class: xl63"]104[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]papayas[/TD]
[TD="class: xl63"]112[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]mangos[/TD]
[TD="class: xl63"]120[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]128[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]136[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl64"]apples[/TD]
[TD="class: xl63"]144[/TD]
[TD="class: xl64"][/TD]
[/TR]
[TR]
[TD="class: xl65"]oranges[/TD]
[TD="class: xl63"]152[/TD]
[TD="class: xl64"][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:

Some videos you may like

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,089,341
Messages
5,407,701
Members
403,158
Latest member
Limerick2030

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top