VBA - Wrong results for a for next loop.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
2010
Platform
Windows
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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
612
Office Version
2010
Platform
Windows
Sorry, False Alarm, I found the mistake. Thanks.
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,240
Messages
5,510,006
Members
408,771
Latest member
Sigmawolf101

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top