I'm using VBA to try and count the number of rows that have the correct information in the column headings. For example, say each row is a different location, and I want to count the number of locations that have a blue roof, wooded build, located within a certain distance (say 200m - 300m) away from a structure.
I work for a train company and I'm trying to see how many defects we have in an excel spreadsheet.
Say rows F2 to F50 are different locations, J2 to J50 are reference location codes, K2 is the Track ID, and column J and M are distance information. I've been trying to use VBA to search a excel sheet and see how many rows have the correct F,J and K values I'm searching for but can't think of a way to do it. the problem is i need the number of rows that have the correct information in sequence. say row 5 has all the correct variables, so i would count row 5 in the total.
I'v explained this terribly but hope it makes a small bit of sense.
Any help is greatly appreciated.
答案 0 :(得分:0)
If I get you correctly, you just need to count the number of rows where all of the conditions should be matched. Please try this code and tell me if it works for you or not:
Sub NewCode()
Dim lastRow As Variant
lastRow = Cells(Rows.Count, "F").End(xlUp).Row
Dim counter As Integer
counter = 0
For i = 2 To lastRow
If Cells(i, "F").Value = "CORRECT VALUE 1" And _
Cells(i, "J").Value = "CORRECT VALUE 2" And _
Cells(i, "K").Value = "CORRECT VALUE 3" Then
counter = counter + 1
End If
Next i
MsgBox "No of rows: " & counter
End Sub