VBA count the number of multiple occurrences

sr1111

New Member
Joined
Sep 2, 2022
Messages
46
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
I am not very much an expert in excel. I am trying to count the number of occurrences of keys in columns A as in the below example.
The VBA needs to count how many times am1, anip1, zora1, riP-1, una1, aya1, im1, nd#1 appers under Com, eae and sae.
Is it possible?

Input:

DADPNI.xlsm
AB
1Column AColumn B
2am1 anip1 zora1 riP-1 una1 una1 aya1 im1 nd#1Com ; sae ;eae ;%&*()1;
3una1 aya1 im1 nd1 am1 anip1 zora1 riP-1 Com ;Com ;sae; eae; sae ;eae ;
4am1 anip1 zora1 am1 anip1 zora1sae ;eae ;%&*()1;
5una1 aya1eae ;sae
6anip1 zora1
7im1;;; ;
8
9aya1eae ;sae ;eae ;sae sae;;
10aya1 im1 riP-1 Com
11im1 Com
Sheet1


Output:

DADPNI.xlsm
ABCDEFGH
1Comeaesae
2am145?
3anip1144
4zora12??
5riP-13??
6una13??
7aya13??
8im16??
9nd#11??
Sheet2
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I am not very much an expert in excel. I am trying to count the number of occurrences of keys in columns A as in the below example.
The VBA needs to count how many times am1, anip1, zora1, riP-1, una1, aya1, im1, nd#1 appers under Com, eae and sae.
Is it possible?

Input:

DADPNI.xlsm
AB
1Column AColumn B
2am1 anip1 zora1 riP-1 una1 una1 aya1 im1 nd#1Com ; sae ;eae ;%&*()1;
3una1 aya1 im1 nd1 am1 anip1 zora1 riP-1 Com ;Com ;sae; eae; sae ;eae ;
4am1 anip1 zora1 am1 anip1 zora1sae ;eae ;%&*()1;
5una1 aya1eae ;sae
6anip1 zora1
7im1;;; ;
8
9aya1eae ;sae ;eae ;sae sae;;
10aya1 im1 riP-1 Com
11im1 Com
Sheet1


Output:

DADPNI.xlsm
ABCDEFGH
1Comeaesae
2am145?
3anip1144
4zora12??
5riP-13??
6una13??
7aya13??
8im16??
9nd#11??
Sheet2

Ps. "?" means, I have not counted the number of occurrences here in this demo data.
Data ( more than 10 thousand rows ) in column B is unique (no more repetitions. I have deleted all the duplicate data in column B).
Looking for help from a VBA wizard to solve this.
 
Upvote 0
VBA Code:
Public Sub WordCounter()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim n As Integer
Dim sum1 As Long
Dim str1 As String
Dim LookingForWord As String
Dim RequiredWord As String
Dim rounds As Integer
Dim RowsOfData As Long: RowsOfData = 10 'Make this Exactly What You Need.
Dim DataSheetName As String: Let DataSheetName = "Sheet1" 'Make sure this is changed to the sheet where the data is.
Dim ResultSheetName As String: Let ResultSheetName = "Sheet2" ' Make sure this is changed to the sheet where the results should be posted.

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

For i = 2 To WorksheetFunction.CountA(Worksheets(ResultSheetName).Range("a1:az1")) + 1
    If Worksheets(ResultSheetName).Cells(1, i).Text <> "" Then
   
    RequiredWord = Worksheets(ResultSheetName).Cells(1, i).Text

    For j = 2 To WorksheetFunction.CountA(Worksheets(ResultSheetName).Range("a1:a100")) + 1
        If Worksheets(ResultSheetName).Cells(j, 1).Text <> "" Then
       
        sum1 = 0
        LookingForWord = Worksheets(ResultSheetName).Cells(j, 1).Text
       
        For k = 2 To RowsOfData
            If Worksheets(DataSheetName).Cells(k, 2).Text <> "" Then
                rounds = 0
               
            For m = 1 To Len(Worksheets(DataSheetName).Cells(k, 2).Text)
                If Mid(Worksheets(DataSheetName).Cells(k, 2).Text, m, Len(RequiredWord)) = RequiredWord Then
                    If rounds = 0 Then
                   
                        rounds = 1
                       
                    For n = 1 To Len(Worksheets(DataSheetName).Cells(k, 1).Text)
                        If Mid(Worksheets(DataSheetName).Cells(k, 1).Text, n, Len(LookingForWord)) = LookingForWord Then
                       
                            sum1 = sum1 + 1
                            Worksheets(ResultSheetName).Cells(j, i).Value = sum1
                           
                        End If
                    Next n
                   
                    End If
                End If
           
            Next m
           
            End If
        Next k
       
        End If
    Next j
   
    End If
Next i

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
                   
End Sub

Let me know if this works the way it should. Make sure you change those variables in the VBA so that you get the correct answers.
 
Upvote 0

Forum statistics

Threads
1,215,204
Messages
6,123,630
Members
449,109
Latest member
Sebas8956

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