Occurrence of first two words and next two and so on

Sak_Guest12

New Member
Joined
Apr 30, 2018
Messages
7
I have below scenario but i am not sure if this can be extracted applying any formula/function. Please help

Need to find occurrence of first two words in a sentence and then next two and so on.

Example:

These are environmental related issues
These are code related issues

Occurrence:

These are - 2
are environmental - 1
environmental related - 1
related issues - 2
are code - 1
code related - 1
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
I have below scenario but i am not sure if this can be extracted applying any formula/function. Please help

Need to find occurrence of first two words in a sentence and then next two and so on.

Example:

These are environmental related issues
These are code related issues

Occurrence:

These are - 2
are environmental - 1
environmental related - 1
related issues - 2
are code - 1
code related - 1

Your "layout" is not entirely clear to me...

1) Is your example text all in one cell or is each line in its own cell?

2) Where is that output supposed to go... each two words in its own cell with the number of occurrences in the next column?

3) Is a VBA solution acceptable?
 
Last edited:
Upvote 0
Your "layout" is not entirely clear to me...

1) Is your example text all in one cell or is each line in its own cell? In different cells

2) Where is that output supposed to go... each two words in its own cell with the number of occurrences in the next column?

Below is my text in different cells

A
CC WRM PC SolMan WRM BI Monitoring WRM ABAP ZTD MS INTF Duration
CC WRZ SolMan WRM ABAP bpxgbap WRZ reuxeuux File System
CC WRW SolMan reuxeuux File System

<tbody>
</tbody>

Output should be like below

BC
Top occurring 2 wordsTop Occurring 3 words
SolMan WRMreuxeuux File System

<tbody>
</tbody>

Note: Counting of words should be from left to right


3) Is a VBA solution acceptable? Yes
 
Upvote 0
Your "layout" is not entirely clear to me...

1) Is your example text all in one cell or is each line in its own cell? In different cells

2) Where is that output supposed to go... each two words in its own cell with the number of occurrences in the next column?

Below is my text in different cells

A
CC WRM PC SolMan WRM BI Monitoring WRM ABAP ZTD MS INTF Duration
CC WRZ SolMan WRM ABAP bpxgbap WRZ reuxeuux File System
CC WRW SolMan reuxeuux File System

<tbody>
</tbody>

Output should be like below

BC
Top occurring 2 wordsTop Occurring 3 words
SolMan WRMreuxeuux File System

<tbody>
</tbody>

Note: Counting of words should be from left to right


3) Is a VBA solution acceptable? Yes


If VBA solution gives the pivot showing all the occurrences , then i can get top occurring 2 or 3 words out of it.
 
Upvote 0

If VBA solution gives the pivot showing all the occurrences , then i can get top occurring 2 or 3 words out of it.

I went this way.

Please try this in a copy of your workbook:

Code:
Sub phraseCtr()

Dim phrase As String
Dim lstRow As Integer
Dim lstCol As Integer
Dim r As Integer, c As Integer, x As Integer, y As Integer


Application.DisplayAlerts = False
ActiveWorkbook.Worksheets.Add After:=ActiveSheet


lstRow = Worksheets(1).Range("A1").End(xlDown).Row


Worksheets(1).Range("A1:A" & lstRow).Copy Destination:=ActiveSheet.Range("A1")
ActiveSheet.Range("A1:A" & lstRow).TextToColumns DataType:=xlDelimited, Space:=True, other:=False
Worksheets(1).Columns("B:C").Delete


x = 1
For r = 1 To lstRow
    lstCol = ActiveSheet.Cells(r, 1).End(xlToRight).Column
For c = 1 To lstCol - 1
    phrase = ActiveSheet.Cells(r, c).Value & " " & ActiveSheet.Cells(r, c + 1).Value
    Worksheets(1).Cells(x, 2).Value = phrase
    x = x + 1
Next c
Next r


With Worksheets(1)
    For y = 1 To x - 1
        .Cells(y, 3).Value = Application.WorksheetFunction.CountIf(.Range("B$1:B$" & x), .Range("B" & y).Value)
    Next y


    .Range("B1:C" & y).RemoveDuplicates Columns:=(1), Header:=xlNo
    
    ActiveSheet.Delete
    .Columns(2).AutoFit
    .Activate
End With


Application.DisplayAlerts = True
End Sub

Regards,

CJ
 
Last edited:
Upvote 0
I went this way.

Please try this in a copy of your workbook:

Code:
Sub phraseCtr()

Dim phrase As String
Dim lstRow As Integer
Dim lstCol As Integer
Dim r As Integer, c As Integer, x As Integer, y As Integer


Application.DisplayAlerts = False
ActiveWorkbook.Worksheets.Add After:=ActiveSheet


lstRow = Worksheets(1).Range("A1").End(xlDown).Row


Worksheets(1).Range("A1:A" & lstRow).Copy Destination:=ActiveSheet.Range("A1")
ActiveSheet.Range("A1:A" & lstRow).TextToColumns DataType:=xlDelimited, Space:=True, other:=False
Worksheets(1).Columns("B:C").Delete


x = 1
For r = 1 To lstRow
    lstCol = ActiveSheet.Cells(r, 1).End(xlToRight).Column
For c = 1 To lstCol - 1
    phrase = ActiveSheet.Cells(r, c).Value & " " & ActiveSheet.Cells(r, c + 1).Value
    Worksheets(1).Cells(x, 2).Value = phrase
    x = x + 1
Next c
Next r


With Worksheets(1)
    For y = 1 To x - 1
        .Cells(y, 3).Value = Application.WorksheetFunction.CountIf(.Range("B$1:B$" & x), .Range("B" & y).Value)
    Next y


    .Range("B1:C" & y).RemoveDuplicates Columns:=(1), Header:=xlNo
    
    ActiveSheet.Delete
    .Columns(2).AutoFit
    .Activate
End With


Application.DisplayAlerts = True
End Sub

Regards,

CJ


Thanks a lot. It works :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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