示例:我想用 vba 知道一年中是否每個人都付錢。那么第一年,第二年等的每個人都付錢了嗎?
| 已付 | 學生 | 年 |
|---|---|---|
| 是的 | 史密斯 | 1 |
| 是的 | 杰克遜 | 1 |
| 不 | 野蠻的 | 2 |
| 不 | 漢密爾頓 | 3 |
| 是的 | 詹納 | 1 |
| 不 | 西 | 2 |
| 是的 | 沙利文 | 2 |
結果:
| 已付 | 學生 | 年 | 年付清 |
|---|---|---|---|
| 是的 | 史密斯 | 1 | 是的 |
| 是的 | 杰克遜 | 1 | 是的 |
| 不 | 野蠻的 | 2 | 不 |
| 不 | 漢密爾頓 | 3 | 不 |
| 是的 | 詹納 | 1 | 是的 |
| 不 | 西 | 2 | 不 |
| 是的 | 沙利文 | 2 | 不 |
1 年級的所有學生都回答“是”,year-completely-paid因為所有這些學生都付費了。
uj5u.com熱心網友回復:
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$2:C$8,C3,A$2:A$8,"no") > 0,"no","yes")
根據需要更改結束行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/456326.html
