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

Nick70

Active Member
Joined
Aug 20, 2013
Messages
306
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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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