always_confused
Board Regular
- Joined
- Feb 19, 2021
- Messages
- 68
- Office Version
- 2016
- Platform
- Windows
Hello. I have 3 different types of data in Range("A:A") : warnings, errors, good data. The criteria that makes a value either warning, error or good is stored in cells C1, C2, C3, C4. If the value of a cell in A:A is between C1 and C2 or between C2 and C3 it is a warning. If it is greater than C3 or less than C1 it is an error. And if it is between C2 and C3 it is good. I would like to count the number of good, error and warning data.
I have set up a for loop with a case inside that increments counting variables depending on the value, but I cannot get it to work (Error Next without For). Is it possible to do this kind of counting in this way? I tried CountIf, bu it got really unwieldy pretty quickly.
What I have so far is:
I have set up a for loop with a case inside that increments counting variables depending on the value, but I cannot get it to work (Error Next without For). Is it possible to do this kind of counting in this way? I tried CountIf, bu it got really unwieldy pretty quickly.
What I have so far is:
VBA Code:
Dim count_warning As Integer
Dim count_error As Integer
Dim count_good As Integer
Dim lastrow As Long
count_count_good = 0
count_alarm = 0
count_defaut = 0
lastrow = Sheets("MySheet").Range("A" & Rows.count).End(xlUp).Row
For i = 2 To lastrow
Select Case Range("A" & i).Value
Case (Range("K" & i).Value < Range("C2").Value And Range("K" & i).Value > Range("C1").Value)
count_warning =count_warning + 1
Case (Range("A" & i).Value < Range("C4").Value And Range("K" & i).Value > Range("C3").Value)
count_warning = count_warning + 1
Case (Range("A" & i).Value < Range("C3").Value And Range("K" & i).Value > Range("C2").Value)
count_good = count_good + 1
Case (Range("A" & i).Value < Range("C1").Value)
count_error = count_error + 1
Case (Range("A" & i).Value > Range("C4").Value)
count_errort = count_error + 1
Next i