Counting the number of occurances of a word in a column of variable length.

midoop

New Member
Joined
Aug 9, 2013
Messages
37
Hello Y'all,
I have a spreadsheet with adverse event data. Column A has the adverse event term exactly as the staff member entered it into the database. I am working on creating a report that counts the number of instances that an AE term appears in column A. The issues that I am having trouble with are that there are MANY AE terms, the AE terms are variable (and unpredictable), and the number of rows in the spreadsheet is variable.
I have figured that the easiest, most organized thing to do is to create a new sheet as the destination for the number of occurances. Below is my code with the troble area in red:

Code:
Sub RM2004_AE_Count()
Dim myrows As Integer
Dim i As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws2row As Integer
Dim LRow As Long
Dim Drng As Range
Application.ScreenUpdating = False
Set ws1 = ActiveSheet
Range("1:1").Delete xlShiftUp
With ws1
    .Name = "AE Count Source Data"
    .Range("A1") = [{"Adverse Event Term"}]
    .Columns("A:L").Sort key1:=.Range("A2"), order1:=xlAscending, Header:=xlYes
End With
With ws1.Columns("A:L").Resize(ws1.UsedRange.Rows.Count)
    .AutoFilter 1, ""
        With .Offset(1).SpecialCells(xlCellTypeVisible)
             .EntireRow.Delete
        End With
        .AutoFilter
End With
Set ws2 = ActiveWorkbook.Worksheets.Add(After:=ws1)
ws2.Name = "MedRA UTR Report"
ws2row = 1
ws2.Cells(ws2row, 1) = "Verbatim"
ws2.Cells(ws2row, 2) = "Term Text"
ws2.Cells(ws2row, 3) = "Term Type"
ws2.Cells(ws2row, 4) = "Term Code"
ws2.Cells(ws2row, 5) = "Count"
ws2.Cells(ws2row, 6) = "PT"
ws2.Cells(ws2row, 7) = "PT Code"
ws2.Cells(ws2row, 8) = "HLT"
ws2.Cells(ws2row, 9) = "HLT Code"
ws2.Cells(ws2row, 10) = "HLGT"
ws2.Cells(ws2row, 11) = "HLGT Code"
ws2.Cells(ws2row, 12) = "SOC"
ws2.Cells(ws2row, 13) = "SOC Code"
ws2.Cells(ws2row, 14) = "Version"
ws1.Select
LRow = Cells(Rows.Count, "A").End(xlUp).Row
Do While LRow > 0
[COLOR=#ff0000]If Cells(LRow, 1).Value = Cells(LRow - 1, 1).Value Then
    ws2.Range("E" & ws2.Rows.Count).End(xlUp).Offset(1) = CountIf(Range("A:A", Cells(LRow, 1).Value)
End If
LRow = LRow - 1
[/COLOR]Loop
End Sub

Any and all help is appreciated! Thanks!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Haven't looked at the rest of your code but for the trouble line try this:
Rich (BB code):
If Cells(LRow, 1).Value = Cells(LRow - 1, 1).Value Then 
ws2.Range("E" & ws2.Rows.Count).End(xlUp).Offset(1) = worksheetfunction.CountIf(Range("A:A"), Cells(LRow, 1).Value) 
End If
Also, be wary of the loop when LRow reaches 1 b/c you are using LRow-1 which is a non-existent row index. You might want to use:
Do While LRow >=2
rather than 0
 
Upvote 0
We are off to a good start, I think! I got some numbers and the macro ran with no error messages. However, I can't tell if the numbers ouput by this macro are correctly counting the number of times a word appears in column A. I have pasted a mock up of what the final report needs to look like (I did not see an option to attach a file)

[TABLE="width: 1265"]
<TBODY>[TR]
[TD][/TD]
[/TR]
[TR]
[TD="align: right"]</SPAN>[TABLE="width: 949"]
<COLGROUP><COL style="WIDTH: 88pt; mso-width-source: userset; mso-width-alt: 4278" width=117><COL style="WIDTH: 52pt; mso-width-source: userset; mso-width-alt: 2523" width=69><COL style="WIDTH: 86pt; mso-width-source: userset; mso-width-alt: 4205" width=115><COL style="WIDTH: 56pt; mso-width-source: userset; mso-width-alt: 2706" width=74><COL style="WIDTH: 101pt; mso-width-source: userset; mso-width-alt: 4900" width=134><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 2998" width=82><COL style="WIDTH: 48pt" width=64><COL style="WIDTH: 92pt; mso-width-source: userset; mso-width-alt: 4461" width=122><COL style="WIDTH: 48pt" width=64><COL style="WIDTH: 62pt; mso-width-source: userset; mso-width-alt: 3035" width=83><COL style="WIDTH: 65pt; mso-width-source: userset; mso-width-alt: 3181" width=87><COL style="WIDTH: 83pt; mso-width-source: userset; mso-width-alt: 4059" width=111><COL style="WIDTH: 58pt; mso-width-source: userset; mso-width-alt: 2816" width=77><COL style="WIDTH: 48pt" width=64><TBODY>[TR]
[TD="class: xl66, width: 117, bgcolor: transparent"]Verbatim[/TD]
[TD="class: xl66, width: 69, bgcolor: transparent"]Term Text[/TD]
[TD="class: xl66, width: 115, bgcolor: transparent"]Term Type[/TD]
[TD="class: xl66, width: 74, bgcolor: transparent"]Term Code[/TD]
[TD="class: xl66, width: 134, bgcolor: transparent"]Count[/TD]
[TD="class: xl66, width: 82, bgcolor: transparent"]PT[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]PT Code[/TD]
[TD="class: xl66, width: 122, bgcolor: transparent"]HLT[/TD]
[TD="class: xl66, width: 64, bgcolor: transparent"]HLT Code[/TD]
[TD="class: xl66, width: 83, bgcolor: transparent"]HLGT[/TD]
[TD="class: xl66, width: 87, bgcolor: transparent"]HLGT Code[/TD]
[TD="class: xl66, width: 111, bgcolor: transparent"]SOC[/TD]
[TD="class: xl66, width: 77, bgcolor: transparent"]SOC Code[/TD]
[TD="class: xl67, width: 64, bgcolor: transparent"]Version[/TD]
[/TR]
[TR]
[TD="class: xl65, width: 117, bgcolor: transparent"]Headaches[/TD]
[TD="class: xl65, width: 69, bgcolor: transparent"]Headache[/TD]
[TD="class: xl65, width: 115, bgcolor: transparent"]LLT[/TD]
[TD="class: xl65, width: 74, bgcolor: transparent, align: right"]10019211[/TD]
[TD="class: xl65, width: 134, bgcolor: transparent, align: right"]5[/TD]
[TD="class: xl65, width: 82, bgcolor: transparent"]HEADACHE[/TD]
[TD="class: xl65, width: 64, bgcolor: transparent, align: right"]10019211[/TD]
[TD="class: xl65, width: 122, bgcolor: transparent"]HEADACHES NEC[/TD]
[TD="class: xl65, width: 64, bgcolor: transparent, align: right"]10019233[/TD]
[TD="class: xl65, width: 83, bgcolor: transparent"]HEADACHES[/TD]
[TD="class: xl65, width: 87, bgcolor: transparent, align: right"]10019231[/TD]
[TD="class: xl65, width: 111, bgcolor: transparent"]NERVOUS SYSTEM DISORDERS[/TD]
[TD="class: xl65, width: 77, bgcolor: transparent, align: right"]10029205[/TD]
[TD="class: xl68, width: 64, bgcolor: transparent, align: right"]16.0[/TD]
[/TR]
[TR]
[TD="class: xl65, width: 117, bgcolor: transparent"]Headache[/TD]
[TD="class: xl65, width: 69, bgcolor: transparent"]Headache[/TD]
[TD="class: xl65, width: 115, bgcolor: transparent"]LLT[/TD]
[TD="class: xl65, width: 74, bgcolor: transparent, align: right"]10019211[/TD]
[TD="class: xl65, width: 134, bgcolor: transparent, align: right"]2[/TD]
[TD="class: xl65, width: 82, bgcolor: transparent"]HEADACHE[/TD]
[TD="class: xl65, width: 64, bgcolor: transparent, align: right"]10019211[/TD]
[TD="class: xl65, width: 122, bgcolor: transparent"]HEADACHES NEC[/TD]
[TD="class: xl65, width: 64, bgcolor: transparent, align: right"]10019233[/TD]
[TD="class: xl65, width: 83, bgcolor: transparent"]HEADACHES[/TD]
[TD="class: xl65, width: 87, bgcolor: transparent, align: right"]10019231[/TD]
[TD="class: xl65, width: 111, bgcolor: transparent"]NERVOUS SYSTEM DISORDERS[/TD]
[TD="class: xl65, width: 77, bgcolor: transparent, align: right"]10029205[/TD]
[TD="class: xl68, width: 64, bgcolor: transparent, align: right"]16.0[/TD]
[/TR]
</TBODY>[/TABLE]
[/TD]
[/TR]
</TBODY><COLGROUP><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL><COL></COLGROUP>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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