count cells that string contains X but does not contains Y or Z

gaz_11

New Member
Joined
Jul 4, 2019
Messages
3
Hi, sooo.....

I have list in table 1 (below) and I want to count in table 2, the number times the text in table 2 column A appear in the list from table 1. BUT i need to to exclude any entries that are in A4 or A5 from table2.

I can do count if but i cant figure out how to exclude.

for the count if ive got in table2 B1. =COUNTIF('table1'!$A:$A,"*"&A1&"*")


A
1ab/home/west
2ab/home/east
3ab/home/east/up
4ab/home/up
5ab/home/down
6ab/away/east
7ab/away new/west
8ab/state/east
9ab/state new/east
10

<tbody>
</tbody>

AB
1up
2down
3east
4away
5state
6
7
8
9

<tbody>
</tbody>
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Deleted response. Misread OP's request.
 
Last edited:
Upvote 0
A
B
C
D
1
ab/home/eastup
2​
2
ab/home/east/updown
0​
3
ab/home/upeast
4​
4
ab/home/downaway
1​
5
ab/away/eaststate
2​
6
ab/away new/west
7
ab/state/east
8
ab/state new/east
9

<tbody>
</tbody>


D1=COUNTIFS($A$1:$A$8,"*"&C1&"*",$A$1:$A$8,"<>"&$A$4,$A$1:$A$8,"<>"&$A$5) copy down
 
Upvote 0
Which sheet are you referring to when you wish to exclude A4 and A5. What is the correct answer for each item? Confused by your request.
 
Last edited:
Upvote 0
its form the 2nd table in my original post (it didn't include grid line when i posted so not clear) so in this case its 'away' and 'state' that i want to exclude. But it need to have a wild card to cover 'away new'and 'state new'
 
Upvote 0
Your explanation is still not clear. Suggest you show us your expected results so that we can better understand.
 
Upvote 0

A
B
C
D
1
ab/home/eastup
2​
2
ab/home/east/updown
1​
3
ab/home/upeast
2​
4
ab/home/downaway
2​
5
ab/away/eaststate
2​
6
ab/away new/west
7
ab/state/east
8
ab/state new/east
9

<tbody>
</tbody>


So i want to count the number of times up, down and east appear in B but only if these dont have away or east in the string.
 
Upvote 0
Best I can do is to completely avoid Away and State with this code.
Code:
Option Explicit


Sub Directions()
    Dim s1 As Worksheet, s2 As Worksheet
    Dim lr1 As Long, lr2 As Long
    Dim i As Long, j As Long, x As Integer
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Application.ScreenUpdating = False
    lr1 = s1.Range("A" & Rows.Count).End(xlUp).Row
    lr2 = s2.Range("A" & Rows.Count).End(xlUp).Row
    For i = 1 To lr2
        x = 0
        For j = 1 To lr1
            If InStr(s1.Range("A" & j), s2.Range("A" & i)) > 0 Then
                If InStr(s1.Range("A" & j), s2.Range("A4")) > 0 Or InStr(s1.Range("A" & j), s2.Range("A5")) > 0 Then
                    x = x + 0
                Else: x = x + 1
                End If
            End If
        Next j
        s2.Range("B" & i) = x
    Next i
    Application.ScreenUpdating = True


End Sub

Maybe someone out there can enhance it. In my case, it either or State and Away included and excluded but cannot have it both ways.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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