VBA - Wrong results for a for next loop.

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
536
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

montecarlo2012

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

Watch MrExcel Video

Forum statistics

Threads
1,102,889
Messages
5,489,551
Members
407,700
Latest member
SimpleJuan

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top