[英]Get unique count combining two columns in Excel VBA
我有一個帶列的excel
ID|Gender|Age|Center
1 M 20 Center1
1 M 24 Center1
2 M 25 Center2
3 F 30 Center2
4 F 25 Center1
Total Males - 2 Total Males in Center1 - 1
Total Females - 2 Total Females in Center2 - 1
我想算一下 Id 和 Gender 的獨特組合。 此外,需要與中心相同的信息。
下面是我正在使用的代碼,但我在所需的單元格中獲得了 0 值。
Sub CountPopulation()
Dim rCell As Range
Dim rRng As Range
Dim countM As Integer
Dim countF As Integer
Dim name As String
Set rRng = Sheet1.Range("D2:D100")
For Each rCell In rRng.Cells
If rCell.Value > name Then
If rCell.Offset(0, 2).Value = "MALE" Then
countM = countM + 1
End If
If rCell.Offset(0, 2).Value = "FEMALE" Then
countF = countF + 1
End If
End If
name = rCell.Value
Next rCell
Sheet2.Range("D4").Value = countM Sheet2.Range("D5").Value = countF
End Sub
謝謝
如果您已經擁有新的動態數組,這是一項非常簡單的任務。 計算 ID 和 Gender 的唯一組合的公式是:
=COUNTA(UNIQUE(A2:A6&B2:B6))
這個公式首先形成一個唯一的(連接的)ID 和性別列表,看起來像這樣:
然后COUNTA
函數將計算行數。
要在 VBA 中應用它,您可以使用Evaluate()
,如下所示:
lUniqueCombinationsOfIDandGender = Evaluate("=COUNTA(UNIQUE(A2:A6&B2:B6))")
如果您想擁有中心和 ID 的唯一組合,您可以將公式更改為:
=COUNTA(UNIQUE(A2:A6&D2:D6))
我假設您的數據中沒有任何空白行(否則,您計算的組合數將比實際數多)。
我希望這對你有幫助。
如果你真的想用 VBA 來做,這段代碼可以幫助你:
Option Explicit
Sub test()
Dim a As Integer, b As Integer, c As Integer
Dim D() As String, e As Integer, f As Integer
Range("A2").Select
a = Selection.End(xlDown).Row
ReDim D(2 To a)
For b = 2 To a
D(b) = Cells(b, 1).Value & Cells(b, 2).Value
Next b
e = a - 1
For b = 2 To a
f = D(b)
For c = (b + 1) To a
If f = D(c) Then
e = e - 1
Exit For
End If
Next c
Next b
MsgBox "Unique combinations: " & e
End Sub
步驟如下: 1. 計算使用的最后一行 2. 相應地重新調整 D 數組 3. 對於每一行,用兩列條目的字符串填充每個 D() 4. 設置 e = a - 1(首先假設,所有組合都是唯一的) 5. 對於所有行:將其內容與以下行的內容進行比較,直到匹配發生。 然后繼續下一行。
希望,這對你有幫助。
我認為這將解決您需要的(我理解):
Dim rngVal As Variant, sh As Worksheet, El As Variant, boolM As Boolean, boolF As Boolean, i As Long
Dim colM1 As New Collection, colM2 As New Collection, colF1 As New Collection, colF2 As New Collection
Set sh = ActiveSheet 'not recommended but just for testing sake it is OK
rngVal = sh.Range("A2:D" & sh.Range("A1").SpecialCells(xlCellTypeLastCell).Row).value
For i = 1 To UBound(rngVal)
boolM = False: boolF = False
If rngVal(i, 4) = "Center1" Then
If rngVal(i, 2) = "M" Then
For Each El In colM1
If El = rngVal(i, 1) & rngVal(i, 1) Then boolM = True: Exit For
Next
If Not boolM Then colM1.Add rngVal(i, 1) & rngVal(i, 1): boolM = False
ElseIf rngVal(i, 2) = "F" Then
For Each El In colF1
If El = rngVal(i, 1) & rngVal(i, 1) Then boolF = True: Exit For
Next
If Not boolF Then colF1.Add rngVal(i, 1) & rngVal(i, 1): boolF = False
End If
ElseIf rngVal(i, 4) = "Center2" Then
If rngVal(i, 2) = "M" Then
For Each El In colM2
If El = rngVal(i, 1) & rngVal(i, 1) Then boolM = True: Exit For
Next
If Not boolM Then colM2.Add rngVal(i, 1) & rngVal(i, 1): boolM = False
ElseIf rngVal(i, 2) = "F" Then
For Each El In colF2
If El = rngVal(i, 1) & rngVal(i, 1) Then boolF = True: Exit For
Next
If Not boolF Then colF2.Add rngVal(i, 1) & rngVal(i, 1): boolF = False
End If
End If
Next i
Debug.Print "Total males : " & colM1.Count + colM2.Count
Debug.Print "Total females: " & colF1.Count + colF2.Count
Debug.Print "Males in Center1: " & colM1.Count, "Males in Center 2: " & colM2.Count
Debug.Print "Females in Center1: " & colF1.Count, "Females in Center2: " & colF2.Count
該代碼將工作范圍放在(變體)數組中並開始處理。 如果 Center1 的每個“M”和“F”在適當的集合(colM1、colF1)中尚不存在,並且對 Center2 成員執行相同的操作,則會將它們添加到適當的集合(colM1、colF1)中。 最后,使用它們的 `Count' 屬性從上面提到的集合中返回結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.