Searching through a column and extracting data

jnagel

New Member
Joined
Jul 10, 2011
Messages
6
Hi,

I am very new to VBA (self taught) so please excuse my ignorance.

I have a column of cells with values of either "Pass" or "Fail"

I want to search through this column, locate all the fails, store what row each one occurs on, and the number of fails that exist.

Is such a thing possible, and any advice how to do this?

Thanks!
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
For data in column A, results appended at the end of the column

Code:
Sub Fails()
Dim LR As Long, i As Long, j As Long, s As String
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    If Range("A" & i).Value = "Fail" Then
        j = j + 1
        s = s & i & ", "
    End If
Next i
If j > 0 Then
    Range("A" & LR + 1).Value = j
    Range("A" & LR + 2).Value = Left(s, Len(s) - 2)
End If
End Sub
 
Upvote 0
Hi jnagel,

Another option below, showing result in column B, assuming you have input data in column A.
Code:
Sub Find_Fails()
Dim Col As String, numFails As Integer, FailsPos As Range

Col = "A"
numFails = WorksheetFunction.CountIf(Columns(Col), "fail")

If numFails > 0 Then
    Set FailsPos = Range(Col & 1)
    For i = 1 To numFails
        Set FailsPos = Columns(Col).Find(What:="fail", After:=FailsPos, SearchOrder:=xlByRows)
        Cells(m + 1, "B") = Col & FailsPos.Row
        m = m + 1
    Next
    Cells(m + 1, "B") = "Total of ""fails""=" & numFails
Else
    MsgBox "No ""fails"" found"
End If
End Sub
Hope this helps,

Regards.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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