检查列是否匹配值的数组
我想寻求您对我的问题的帮助:
我想检查工作簿 A 的 Q 列中的行值是否为“WIN”。
如果是,则在工作簿 A 的 BL 列的相应行中返回 1。
如果否,则在工作簿 A 的 BL 列的相应行中返回 0。
我已经应用了基于 VBA 的数组来执行检查,但不幸的是,我只得到 1,而不是 0...
我的(非工作)代码如下:
Dim ws As Worksheet
Dim j, LastRowOutcomeCleaned As Long
Dim arrQ, arrBL As Variant
Dim answer, found As Range
Set ws = Workbooks("A.xlsx").Sheets(2)
LastRow = ws.Range("Q" & Rows.Count).End(xlUp).Row
arrQ = ws.Range("Q2:Q" & LastRow).Value
arrBL = ws.Range("BL2:BL" & LastRow).Value
ReDim arrBL(1 To UBound(arrQ), 1 To 1)
For j = 1 To UBound(arrQ)
answer = "WIN"
Set found = ws.Columns("Q:Q").Find(what:=answer)
If found Is Nothing Then
arrBL(j, 1) = "0"
Else
arrBL(j, 1) = "1"
End If
Next j
ws.Range("BL1").Resize(UBound(arrBL), 1).Value = arrBL
非常感谢!
I would like to seek your help for my issue:
I would like to check if the row value in Column Q of Workbook A is "WIN".
If yes, then return 1 in the corresponding row in Column BL of Workbook A.
If no, then return 0 in the corresponding row in Column BL of Workbook A.
I have applied a VBA-based array to carry out the check but unfortunately, I am only getting 1, not 0...
My (non-working) code is below:
Dim ws As Worksheet
Dim j, LastRowOutcomeCleaned As Long
Dim arrQ, arrBL As Variant
Dim answer, found As Range
Set ws = Workbooks("A.xlsx").Sheets(2)
LastRow = ws.Range("Q" & Rows.Count).End(xlUp).Row
arrQ = ws.Range("Q2:Q" & LastRow).Value
arrBL = ws.Range("BL2:BL" & LastRow).Value
ReDim arrBL(1 To UBound(arrQ), 1 To 1)
For j = 1 To UBound(arrQ)
answer = "WIN"
Set found = ws.Columns("Q:Q").Find(what:=answer)
If found Is Nothing Then
arrBL(j, 1) = "0"
Else
arrBL(j, 1) = "1"
End If
Next j
ws.Range("BL1").Resize(UBound(arrBL), 1).Value = arrBL
Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Find 每次都会在循环中搜索整个列,因此如果该值存在于列中的任何位置,整个输出数组将返回 1。如果您想要逐行测试,则测试每一行:
Find will search the entire column each time in the loop so if the value exist anywhere in the column the whole output array will return 1. If you want a row by row test then test each row: