excel - 如果大于 0,则返回单元格值
问题描述
使用嵌套公式或 VBA,我想在每个评分列中为所有个人返回大于 0 的评分。每列的结果应位于最后填充行的正下方,如下所示: 计算前的电子表格:
Rating A Ratings B Ratings C
Jane 0 -1 0
Rick 1 -2 1
Johnny -1 2 5
James 3 2 3
后:
Rating A Ratings B Ratings C
Jane 0 -1 0
Rick 1 -2 1
Johnny -1 2 5
James 3 2 3
Rick Johnny Johnny
1 2 5
James James James
3 2 3
Rick
1
如上所示,Rick 和 James 的评级 A >0,因此公式从评级 A 列的下一个空行开始输出结果。评级 B 和 C 的技术相同。请注意,这是对较大电子表格的一个小得多的豁免。我无法发布包含数千行和列的完整电子表格。
解决方案
首先,正如urdearboy建议的那样,我会考虑稍微改变输出格式并使用数据透视表。但是,如果它不是一个选项,您可以尝试for...next
循环。
下面的代码将循环思考您的示例并以您想要的方式收集结果,但我冒昧地将结果放在单独的列中,因为它对于将来的使用会更加通用。
Sub Check_rating()
Dim x As Long
Dim ws As Worksheet
Dim counterA As Long
Dim counterB As Long
Dim counterC As Long
Set ws = Worksheets("Sheet1")
For x = 2 To Range("Table1").Rows.Count + 1
If ws.Cells(x, "B").Value > 0 Then
counterA = counterA + 1
ws.Cells(counterA, "F").Value = ws.Cells(x, "A").Value
ws.Cells(counterA, "G").Value = ws.Cells(x, "B").Value
End If
If ws.Cells(x, "C").Value > 0 Then
counterB = counterB + 1
ws.Cells(counterB, "H").Value = ws.Cells(x, "A").Value
ws.Cells(counterB, "I").Value = ws.Cells(x, "C").Value
End If
If ws.Cells(x, "D").Value > 0 Then
counterC = counterC + 1
ws.Cells(counterC, "J").Value = ws.Cells(x, "A").Value
ws.Cells(counterC, "K").Value = ws.Cells(x, "D").Value
End If
Next x
End Sub
推荐阅读
- google-sheets - 如何使用索引、匹配和搜索在同一单元格中返回多个结果
- python - 结合使用 lmfit 中的 Model 类和 Parameter 类
- c - 如何从缓冲区中逐字读取仅由“:”分隔的单词?
- c# - 如何手动为控制器创建配置,以便我可以使用通用的 try catch 函数
- c# - Xamarin.Forms - 在显示之前从 SQLite 更改值(日期时间)
- elasticsearch - 条款查询(ElasticSearch)不适用于某些字段值
- c++ - 为什么错误 C6386 缓冲区溢出 strsafe.h StringCch 函数?
- svn - 基于路径的授权问题
- javascript - 在 React 项目中注册服务工作者时出现问题(来自 npx create-react-app)
- c# - 在 EF 中参数化多个连接