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&"*")


[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]ab/home/west[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]ab/home/east[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]ab/home/east/up[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]ab/home/up[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]ab/home/down[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]ab/away/east[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]ab/away new/west[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]ab/state/east[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]ab/state new/east[/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]up[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]down[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]east[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]away[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]state[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Deleted response. Misread OP's request.
 
Last edited:
Upvote 0
[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]
A
[/TD]
[TD]
B
[/TD]
[TD]
C
[/TD]
[TD]
D
[/TD]
[/TR]
[TR]
[TD]
1
[/TD]
[TD]ab/home/east[/TD]
[TD][/TD]
[TD]up[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
2
[/TD]
[TD]ab/home/east/up[/TD]
[TD][/TD]
[TD]down[/TD]
[TD]
0​
[/TD]
[/TR]
[TR]
[TD]
3
[/TD]
[TD]ab/home/up[/TD]
[TD][/TD]
[TD]east[/TD]
[TD]
4​
[/TD]
[/TR]
[TR]
[TD]
4
[/TD]
[TD]ab/home/down[/TD]
[TD][/TD]
[TD]away[/TD]
[TD]
1​
[/TD]
[/TR]
[TR]
[TD]
5
[/TD]
[TD]ab/away/east[/TD]
[TD][/TD]
[TD]state[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
6
[/TD]
[TD]ab/away new/west[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
7
[/TD]
[TD]ab/state/east[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
8
[/TD]
[TD]ab/state new/east[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
9
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


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
[TABLE="class: cms_table_grid"]
<tbody>[TR]
[/TR]
[TR]
[TD]

A
[/TD]
[TD]
B
[/TD]
[TD]
C
[/TD]
[TD]
D
[/TD]
[/TR]
[TR]
[TD]
1
[/TD]
[TD]ab/home/east[/TD]
[TD][/TD]
[TD]up[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
2
[/TD]
[TD]ab/home/east/up[/TD]
[TD][/TD]
[TD]down[/TD]
[TD]
1​
[/TD]
[/TR]
[TR]
[TD]
3
[/TD]
[TD]ab/home/up[/TD]
[TD][/TD]
[TD]east[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
4
[/TD]
[TD]ab/home/down[/TD]
[TD][/TD]
[TD]away[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
5
[/TD]
[TD]ab/away/east[/TD]
[TD][/TD]
[TD]state[/TD]
[TD]
2​
[/TD]
[/TR]
[TR]
[TD]
6
[/TD]
[TD]ab/away new/west[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
7
[/TD]
[TD]ab/state/east[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
8
[/TD]
[TD]ab/state new/east[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
9
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


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,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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