Find how many times text in a specific range is found in another specific range

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like a VBA code (not excel formula) that looks at text in a range e.g. range(B4:B8) and then looks at how many times this text is found in another range e.g. range(D4:D20).

So if in range(B4:B8) we have 3 words i.e. Apple, Pear and Cherry and in range(D4:D20) we have 3 times Apple, 2 times Pear and 1 time Cherry then the final result should be 6.

I know we can do this with a Pivot table (or adding COUNTIF) but because the data changes every day (i.e. daily data extract) I would need to rebuild/ repoint my Pivot table/excel formula every day whereas the VBA code would run on new sheet and would require no intervention (as new sheets have same name as old ones).

Let me know if VBA code is easy to create.

Thanks,
Nic :)
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try:
VBA Code:
Sub FindTotal()
    Application.ScreenUpdating = False
    Dim rng As Range, total As Long
    For Each rng In Range("B4", Range("B" & Rows.Count).End(xlUp))
        total = total + WorksheetFunction.CountIf(Range("D4", Range("D" & Rows.Count).End(xlUp)), rng)
    Next rng
    MsgBox total
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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