In excel, I have (table containing many columns of categorical variables):
A E
S E
D YU
R T
FD R
FD RT
D YU
S T
AS R
D RR
D R
I want (the frequency of each separate term):
A 1 E 2
S 2 YU 2
D 4 T 2
R 1 R 3
FD 2 RT 1
AS 1 RR 1
I am computing the frequency of each term. I can compute this individually by hand with:
Sub ff()
Range("B1:B6").Select
Selection.FormulaArray = "=COUNTIFS(Sheet1!C[-1],Sheet2!RC[-1]:R[5]C[-1])"
Range("D1").Select
End Sub
but I want to loop through each column and put the frequency count on another sheet. so far, I have:
Sub Macro1()
Sheets("Sheet1").Select
lcol = Cells(1, Columns.Count).End(xlToLeft).Column
N = Cells(Rows.Count, 1).End(xlUp).Row
For j = 1 To lcol
Range(Cells(1, j), Cells(N, j)).Select
Selection.Copy
Sheets("Sheet2").Select
Cells(1, 2 * j - 1).Select
ActiveSheet.Paste
Columns(2 * j - 1).RemoveDuplicates Columns:=1, Header:=xlNo
nr = Cells(Rows.Count, 2 * j - 1).End(xlUp).Row
Range(Cells(1, 2 * j), Cells(nr, 2 * j)).Select
*Selection.FormulaArray = "=COUNTIFS(Sheet1!C[" & j & "],Sheet2!RC[-1]:R[5]C[-1])"
Sheets("Sheet1").Select
Next j
End Sub
I followed advice from Refereing to a range of cell in another sheet but they reference explicit cells. In my case, I will reference explicit cells, but by an index reference (j).
note that
Sheets(2).Range("F15:AK46").Select
gave me an error as well
As mentioned in the comments using the dictionary is the best approach. So your code will be as below. I don't know how you want your results to be printed or used, but for this example, the code will create a sheet called "Results" and if it existed it will just clear it and write new results:
Option Explicit
Sub CountOccurrences()
Dim i As Long
Dim j As Long
Dim cnt As Long
Dim lCol As Integer
Dim N As Long
Dim key As Variant
Dim dict As Object
Dim WS As Worksheet
Dim WS_Result As Worksheet
'Set objects and create a sheet to print results
Set WS = ThisWorkbook.Worksheets("Sheet1") 'change the name of the sheet to whatever you have
On Error GoTo Handler
Set WS_Result = ThisWorkbook.Worksheets("Results")
On Error GoTo 0
WS_Result.Cells.Clear
'Count the columns
lCol = WS.Cells(1, WS.Columns.Count).End(xlToLeft).Column
'Loop thru all columns and count occurrences
For j = 1 To lCol
'Find the last row
N = WS.Cells(WS.Rows.Count, lCol).End(xlUp).Row
'Create a new dictionary
Set dict = CreateObject("scripting.dictionary")
For i = 1 To N
key = WS.Cells(i, j).Value
If dict.exists(key) = False Then
dict.Add key, 1 'key=cell value, item=count of that value
Else
dict(key) = dict(key) + 1
End If
Next i
'Print the results
cnt = 0
For Each key In dict.keys
cnt = cnt + 1
WS_Result.Cells(cnt, (j - 1) * 2 + 1).Value = key
WS_Result.Cells(cnt, (j - 1) * 2 + 2).Value = dict(key)
Next key
'Destroy the dictionary to startover
Set dict = Nothing
Next j
Exit Sub
Handler:
Set WS_Result = ThisWorkbook.Worksheets.Add
WS_Result.Name = "Results"
Resume
End Sub
snapshot of the result
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.