0

I have written a small program to scan through a spreadsheet and calculate the total number of different item types based on a value in a column, then to print the results to specific column locations. My code is below

    Sub How_Many_items()

'define variables. A, B, C refer to letter coding. O is other. counter is for the count
    Dim A As Integer
    Dim B As Integer
    Dim C As Integer
    Dim O As Integer
    Dim Sum As Integer
    Dim Counter As Integer

'resetting variables
    A = 0
    B = 0
    C = 0
    O = 0
    Sum = 0
    Counter = 2
    'do while loop to continue going down rows until out of new data
    'if loops to accumulate values
    Worksheets("Values").Select

Do While Worksheets("Values").Cells(Counter, 54) <> Empty
    If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
    A = A + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value = "B" Then
    B = B + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value = "C" Then
    C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1

    ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then
    Sum = Sum + 1 And Counter = Counter + 1
    End If

Loop

'print values in PivotTables worksheet

Worksheets(PivotTables).Cells(I, 20) = Sum
Worksheets(PivotTables).Cells(I, 21) = A
Worksheets(PivotTables).Cells(I, 22) = B
Worksheets(PivotTables).Cells(I, 23) = C


  End Sub

Based on looking at other information on this site, I don't actually need the worksheets().select action, but I don't think that is stalling the program.

When stepping through my program it stalls on the loop; the current data in use has "B" in the first column of interest, but it doesnt ever increment to the next row, which makes me think it isn't actually adding to the count or to B for some reason. The column that I am scanning is also a calculated column using a VLOOKUP, but I don't know if that affects what I am doing. The excel worksheet will crash if I try to run the program. Thanks for any help!

3
  • 2
    C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1 is not how it's done... remove the And's and put each one on its own line. Commented Feb 27, 2020 at 14:51
  • 1
    Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" doesn't do what you think it does. Commented Feb 27, 2020 at 14:53
  • You don't need a loop here - just use Application.CountIf to get the counts of A, B, C. Commented Feb 27, 2020 at 14:59

2 Answers 2

1

Really more a comment but can't easily describe as such, but you need to split your actions into separate lines, viz

If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
    A = A + 1
    Sum = Sum + 1
    Counter = Counter + 1

and ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then

needs to be

ElseIf NOT(Worksheets("Values").Cells(Counter, 54).Value = "A" Or Worksheets("Values").Cells(Counter, 54).Value ="B" Or Worksheets("Values").Cells(Counter, 54).Value = "C") Then

You could use a With clause or a variable to shorten your code.

In fact your code could be simplified (I think) to

Sub How_Many_items()

'define variables. A, B, C refer to letter coding. O is other. counter is for the count
Dim A As Long
Dim B As Long
Dim C As Long
Dim O As Long
Dim Sum As Long
Dim Counter As Long

Counter = 2
'do while loop to continue going down rows until out of new data
'if loops to accumulate values
With Worksheets("Values")
    Do While .Cells(Counter, 54) <> Empty
        If .Cells(Counter, 54).Value = "A" Then
            A = A + 1
        ElseIf .Cells(Counter, 54).Value = "B" Then
            B = B + 1
        ElseIf .Cells(Counter, 54).Value = "C" Then
            C = C + 1
        End If
        sum = Sum + 1
        Counter = Counter + 1
    Loop
End With

Why not use COUNTIF formulas?

Sign up to request clarification or add additional context in comments.

10 Comments

Another idea: Select Case would be great here.
My last bit of logic might be wrong - someone clever please check!
Or agree with @BigBen - Select Case is easier to grasp.
@BigBen - agreed, have suggested a rewrite.
This appears to have worked, but I have errors in my calculated column to fix. Thank you all so much for the help!
|
0

Does it have to be a Do While Loop? You could use the special cells function the get the last row containing data and just do a finite for loop.

Dim LastDataRow As Long 'Variable to store row number

LastDataRow = Cells.SpecialCells(xlCellTypeLastCell).Row

For i = 1 To LastDataRow

If Worksheets("Values").Cells(Counter, 54).Value = "A" Then
A = A + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value = "B" Then
B = B + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value = "C" Then
C = C + 1 And Sum = Sum + 1 And Counter = Counter + 1

ElseIf Worksheets("Values").Cells(Counter, 54).Value <> "A" Or "B" Or "C" Then
Sum = Sum + 1 And Counter = Counter + 1
End If

Next i

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.