Help with a loop

wroze27

New Member
Joined
Apr 23, 2021
Messages
31
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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
 
Upvote 0
Solution

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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