Help with a loop

wroze27

New Member
Joined
Apr 23, 2021
Messages
21
Office Version
  1. 2019
Platform
  1. Windows
Can someone please help me understand why I cannot get this. I need this code to run through a undetermined amount of rows and filter them by a, b, or c then adds the filtered values together and posts the sum in another sheets cells. So if I had my data as: a = 4, b = 5, a = 6 then the other sheet would show a = 10 and b = 5 in total.
Thanks in advance for helping guys!


VBA Code:
Sub Test()
Dim A As Integer
Dim B As Integer
Dim C As Integer
A = 0
B = 0
C = 0

Do Until IsEmpty(Sheet2.Cells(i, 1))
i = 1
If Sheet2.Cells(i, 1) = "a" Then
A = A + Sheet2.Cells(i, 1)
Sheet1.Range("A2") = A
i = i + 1
ElseIf Sheet2.Cells(i, 1) = "b" Then
B = B + Sheet2.Cells(i, 1)
Sheet1.Range("B2") = B
i = i + 1
ElseIf Sheet2.Cells(i, 1) = "c" Then
C = C + Sheet2.Cells(i, 1)
Sheet1.Range("C2") = C
i = i + 1
End If
Loop
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,645
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Test()
Dim A As Integer
Dim B As Integer
Dim C As Integer
A = 0
B = 0
C = 0
i = 1

Do Until IsEmpty(Sheet2.Cells(i, 1))
   If Sheet2.Cells(i, 1) = "a" Then
      A = A + 1
      Sheet1.Range("A2") = A
   ElseIf Sheet2.Cells(i, 1) = "b" Then
      B = B + 1
      Sheet1.Range("B2") = B
   ElseIf Sheet2.Cells(i, 1) = "c" Then
      C = C + 1
      Sheet1.Range("C2") = C
   End If
   i = i + 1
Loop
End Sub
 
Solution

Forum statistics

Threads
1,143,677
Messages
5,720,259
Members
422,273
Latest member
linds75

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
Top