VBA - Wrong results for a for next loop.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
984
Office Version
  1. 2010
Platform
  1. 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:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,214,619
Messages
6,120,550
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top