how to count repeate words in VBA with msgbox

dilipgr

New Member
Joined
Jan 30, 2015
Messages
43
Dear Team,

Good Day,

Ref NoComp NatureDateTime
123Incoming30/01/20155:37:03 PM
143South30/01/20155:37:03 PM
1421Incoming30/01/20156:01:06 PM
1422Incoming30/01/20156:02:28 PM
1423South30/01/20156:02:28 PM
1424South30/01/20156:02:28 PM
1425Tamilnadu30/01/20156:02:28 PM
1426Query30/01/20156:02:28 PM
1427Incoming30/01/20156:02:28 PM
1428Tamilnadu30/01/20156:02:28 PM
1429South30/01/20156:02:28 PM
in the above details i want to know how count the repated words in (comp nature) B column in vba with msg box. can any one help me

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This will display the count of the active cell in column B. So, if your active cell is B2, it will count the number of Incoming in coumn B.
Code:
Sub CountActiveCell()
Dim LastRow As Long
Dim Cnt As Long
Dim C As Range
Dim FirstAddress As String
With Worksheets(1)
    LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
    
    With .Range("B1:B" & LastRow)
        Set C = .Find(ActiveCell, LookIn:=xlValues)
        If Not C Is Nothing Then
            FirstAddress = C.Address
            Do
                Cnt = Cnt + 1
                Set C = .FindNext(C)
            Loop While Not C Is Nothing And C.Address <> FirstAddress
        End If
    End With
End With
MsgBox Cnt
End Sub
 
Upvote 0
dilipgr,

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


I am having a problem putting the results (aligning corectly) in a Message Box. And, I am not sure how much information can be placed in a Message Box.


Here is another macro solution for you to consider (it will only display the results where the number of repeats is greater than 1):

Sample raw data in the active worksheet:


Excel 2007
ABCDEFGH
1Ref NoComp NatureDateTime
2123Incoming30/01/20155:37:03 PM
3143South30/01/20155:37:03 PM
41421Incoming30/01/20156:01:06 PM
51422Incoming30/01/20156:02:28 PM
61423South30/01/20156:02:28 PM
71424South30/01/20156:02:28 PM
81425Tamilnadu30/01/20156:02:28 PM
91426Query30/01/20156:02:28 PM
101427Incoming30/01/20156:02:28 PM
111428Tamilnadu30/01/20156:02:28 PM
121429South30/01/20156:02:28 PM
13
Sheet1


After the macro:


Excel 2007
ABCDEFGH
1Ref NoComp NatureDateTimeWordsCount
2123Incoming30/01/20155:37:03 PMIncoming4
3143South30/01/20155:37:03 PMSouth4
41421Incoming30/01/20156:01:06 PMTamilnadu2
51422Incoming30/01/20156:02:28 PM
61423South30/01/20156:02:28 PM
71424South30/01/20156:02:28 PM
81425Tamilnadu30/01/20156:02:28 PM
91426Query30/01/20156:02:28 PM
101427Incoming30/01/20156:02:28 PM
111428Tamilnadu30/01/20156:02:28 PM
121429South30/01/20156:02:28 PM
13
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub CountRepeatWords()
' hiker95, 01/30/2015, ME832519
Dim rng As Range, c As Range, o As Variant, oa As Variant
Dim i As Long, n As Long, m As String
Application.ScreenUpdating = False
oa = Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row)
Set rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
With CreateObject("Scripting.Dictionary")
  .CompareMode = vbTextCompare
  For Each c In rng
    If Not .Exists(c.Value) Then
      .Add c.Value, 1
    Else
      .Item(c.Value) = .Item(c.Value) + 1
    End If
  Next
  n = .Count
  o = Application.Transpose(Array(.Keys, .Items))
End With
Columns("G:H").ClearContents
Range("G1").Resize(, 2).Value = Array("Words", "Count")
Range("G2").Resize(UBound(o, 1), UBound(o, 2)) = o
Range("G2:H" & n + 1).Sort key1:=Range("H2"), order1:=2, key2:=Range("G2"), order1:=1
Columns("G:H").AutoFit
On Error Resume Next
With Range("H2:H" & CStr(Range("H" & Rows.Count).End(xlUp).Row))
  .Replace "1", "#NAME?", xlWhole, xlByRows, False
  .SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete xlUp
End With
Range("A1").Resize(UBound(oa, 1), UBound(oa, 2)) = oa
Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row).NumberFormat = "dd/mm/yyyy"
Range("D2:D" & Range("D" & Rows.Count).End(xlUp).Row).NumberFormat = "[$-F400]h:mm:ss AM/PM"
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CountRepeatWords macro.
 
Upvote 0
I realzed after looking at Hiker's response that you wanted it for all instances.

Here is my version.
Code:
Sub CountActiveCell2()
Dim Unique As New Collection
Dim LastRow As Long
Dim A As Long
Dim Cnt As Long
Dim C As Range
Dim FirstAddress As String
Dim Msg As String
With Worksheets(1)
    LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
    
    For A = 2 To LastRow
        On Error Resume Next
        Unique.Add CStr(.Range("B" & A)), CStr(.Range("B" & A))
        On Error GoTo 0
    Next
   
    For A = 1 To Unique.Count
    With .Range("B1:B" & LastRow)
        Set C = .Find(Unique(A), LookIn:=xlValues)
        If Not C Is Nothing Then
            FirstAddress = C.Address
            Do
                Cnt = Cnt + 1
                Set C = .FindNext(C)
            Loop While Not C Is Nothing And C.Address <> FirstAddress
            Msg = Msg & Unique(A) & "-" & Cnt & vbCr
            Cnt = 0
        End If
    End With
    Next
End With
MsgBox Msg
End Sub
 
Upvote 0
Sir

And One More in VBA i'm taking report for performance so i'm creating userform the properties are combobox1, dtpicker1, textbox1 & commandbuttton1.

Station01/01/201502/01/201503/01/201504/01/201505/01/201506/01/2015
MONTUEWEDTHUFRISAT
AKM
APK
ARR
ART
ASD
ATR
BTU
CDM
CGL
CJB
CMM

<colgroup><col><col span="6"></colgroup><tbody>
</tbody>

station in A column is combobox1, dtpicker is date ie, 1/1/15.....5/1/15 if i select combobox1 CJB and dtpicker 3/1/15 then give the value 100 in textbox1 and click commanbutton1 the value of 100 is placed in D13. Can u please help me


regards
Dilipkumar.V
windows7 excel 2007
 
Upvote 0
dilipgr,

When you respond to your helper(s), please use their site ID/username/handle.

This will keep thread clutter to a minimum and make the discussion easier to follow.


In order to continue I will have to see your actual workbook.

You can upload your workbook to Box Net,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Upvote 0
dilipgr,

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0

Forum statistics

Threads
1,215,433
Messages
6,124,863
Members
449,195
Latest member
MoonDancer

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