List rows that meet certain condition

fraufreda

Board Regular
Joined
Oct 14, 2010
Messages
190
HELLO
I want to list the number of rows that meet a certain condition. I know that the match formula gets the number of a row. however, it only gets the first value that it finds. Example:
<TABLE style="WIDTH: 19px; BORDER-COLLAPSE: collapse; HEIGHT: 128px" border=0 cellSpacing=0 cellPadding=0 width=19><COLGROUP><COL style="WIDTH: 15pt; mso-width-source: userset; mso-width-alt: 731" width=20><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; WIDTH: 15pt; HEIGHT: 15pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20 width=20>A</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" id=td_post_2067860 class=xl65 height=20>S</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>D</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>W</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>A</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>D</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>J</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: windowtext; BORDER-RIGHT: windowtext 0.5pt solid" class=xl65 height=20>A</TD></TR></TBODY></TABLE> if I want to search for rows that contains the value "A", this can't be done using match because it will get only the first A and returns 1. how to get a list of all rows that contains "A" so the result will be:
1
5
7
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Re: List rows that meet cetrain condition

This assumes your non blank rows are contiguous, if not you'll have to edit as applicable. It also works if any cell in a row has the value "a" or whatever you choose.

Code:
Sub delete_rows()
Dim i, j, k As Integer
    i = Selection.CurrentRegion.Rows.Count
    k = ActiveCell.Row

    For j = 1 To i
        If Rows(k).Find("A", , xlValues, xlWhole).Value < 1 Then
            Range("A" & k).EntireRow.Delete
        Else: k = k + 1
        End If
    Next j
End Sub

I guess something like this might be of help if you only want to delete if the active column contains the specified selection

Code:
Sub delete_rows()
Dim i, j, k As Integer
    i = Selection.CurrentRegion.Rows.Count

For j = 1 To i
    If ActiveCell.Value <> "a" Then
        ActiveCell.EntireRow.Delete
    Else: ActiveCell.Offset(1, 0).Select
    End If
Next j
End Sub

HTH
 
Upvote 0
Re: List rows that meet cetrain condition

Try,

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">A</td><td style="text-align: right;;"></td><td style=";">Lookup Value</td><td style="text-align: center;;">A</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">S</td><td style="text-align: right;;"></td><td style=";">Total Count</td><td style="text-align: center;;">3</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">D</td><td style="text-align: right;;"></td><td style=";">Row #:</td><td style=";">            </td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">W</td><td style="text-align: right;;"></td><td style="text-align: right;;">1</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">A</td><td style="text-align: right;;"></td><td style="text-align: right;;">5</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style=";">D</td><td style=";">   </td><td style="text-align: right;;">8</td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style=";">J</td><td style="text-align: right;;"></td><td style=";"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style=";">A</td><td style="text-align: right;;"></td><td style=";"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet4</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">D2</th><td style="text-align:left">=COUNTIF(<font color="Blue">A:A,D1</font>)</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">C4</th><td style="text-align:left">=IF(<font color="Blue">ROWS(<font color="Red">C$4:C4</font>)<=D2,MATCH(<font color="Red">D1,A:A,0</font>),""</font>)</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">C5</th><td style="text-align:left">=IF(<font color="Blue">ROWS(<font color="Red">C$4:C5</font>)<=$D$2,C4+MATCH(<font color="Red">$D$1,INDEX(<font color="Green">A:A,C4+1</font>):$A$65536,0</font>),""</font>)</td></tr></tbody></table></td></tr></table><br />
Copy C5 formula to down...
 
Upvote 0
Seems I misunderstood the question, if you adapted the code to read as follows you can complete it in VBA otherwise the above formulas look nice.

Code:
Sub find_rows()
Dim i, j, k, l As Integer
    i = Selection.CurrentRegion.Rows.Count
    k = ActiveCell.Row
    l = 1
    For j = 1 To i
    On Error Resume Next
        If Rows(k).Find("A", , xlValues, xlWhole).Value < 1 Then
        k = k + 1
        Else: Range("B" & l).Value = k
        k = k + 1
        l = l + 1
        End If
    Next j
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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