检查单元格中的值是否在组内 'yes'
Check if value in a cell is 'yes' within a group
示例:
我想知道 vba 是否每个人都支付了一年的费用。那么第 1 年、第 2 年等的每个人都付钱了吗?
payed
student
year
yes
smith
1
yes
jackson
1
no
ferral
2
no
hamilton
3
yes
jenner
1
no
west
2
yes
sullivan
2
结果:
payed
student
year
year-completely-paid
yes
smith
1
yes
yes
jackson
1
yes
no
ferral
2
no
no
hamilton
3
no
yes
jenner
1
yes
no
west
2
no
yes
sullivan
2
no
第 1 年的所有学生在 year-completely-paid
中都有 'yes',因为所有这些学生都支付了费用。
VBA 解决方案使用字典来获取当年的任何否。
Dim i As Long
Dim lr As Long
Dim dict As Object
Dim currentyear As Long
Set dict = CreateObject("Scripting.Dictionary")
With ActiveSheet
lr = .Cells(.Rows.Count, 1).End(xlUp).row
For i = 2 To lr
currentyear = .Cells(i, 3).Value
If dict.exists(currentyear) Then
If dict(currentyear) = "yes" And .Cells(i, 1).Value = "no" Then
dict(currentyear) = "no"
End If
Else
dict.Add currentyear, .Cells(i, 1).Value
End If
Next
For i = 2 To lr
currentyear = .Cells(i, 3).Value
.Cells(i, 4).Value = dict(currentyear)
Next i
End With
公式解:
=IF(COUNTIFS(C:C,C3,A:A,"no") > 0,"no","yes")
根据需要更改结束行。
示例: 我想知道 vba 是否每个人都支付了一年的费用。那么第 1 年、第 2 年等的每个人都付钱了吗?
payed | student | year |
---|---|---|
yes | smith | 1 |
yes | jackson | 1 |
no | ferral | 2 |
no | hamilton | 3 |
yes | jenner | 1 |
no | west | 2 |
yes | sullivan | 2 |
结果:
payed | student | year | year-completely-paid |
---|---|---|---|
yes | smith | 1 | yes |
yes | jackson | 1 | yes |
no | ferral | 2 | no |
no | hamilton | 3 | no |
yes | jenner | 1 | yes |
no | west | 2 | no |
yes | sullivan | 2 | no |
第 1 年的所有学生在 year-completely-paid
中都有 'yes',因为所有这些学生都支付了费用。
VBA 解决方案使用字典来获取当年的任何否。
Dim i As Long
Dim lr As Long
Dim dict As Object
Dim currentyear As Long
Set dict = CreateObject("Scripting.Dictionary")
With ActiveSheet
lr = .Cells(.Rows.Count, 1).End(xlUp).row
For i = 2 To lr
currentyear = .Cells(i, 3).Value
If dict.exists(currentyear) Then
If dict(currentyear) = "yes" And .Cells(i, 1).Value = "no" Then
dict(currentyear) = "no"
End If
Else
dict.Add currentyear, .Cells(i, 1).Value
End If
Next
For i = 2 To lr
currentyear = .Cells(i, 3).Value
.Cells(i, 4).Value = dict(currentyear)
Next i
End With
公式解:
=IF(COUNTIFS(C:C,C3,A:A,"no") > 0,"no","yes")
根据需要更改结束行。