检查列是否匹配值的数组

发布于 2025-01-11 06:00:15 字数 858 浏览 0 评论 0原文

我想寻求您对我的问题的帮助:

我想检查工作簿 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2025-01-18 06:00:15

Find 每次都会在循环中搜索整个列,因此如果该值存在于列中的任何位置,整个输出数组将返回 1。如果您想要逐行测试,则测试每一行:

Dim ws As Worksheet
Dim j As Long, LastRowOutcomeCleaned As Long
Dim arrQ as Variant, arrBL As Variant
Dim answer as string

'Note: this is dangerous as the order of sheets may change
'      Consider using the codename instead.  
Set ws = Workbooks("A.xlsx").Sheets(2) 
LastRow = ws.Range("Q" & Rows.Count).End(xlUp).Row

arrQ = ws.Range("Q2:Q" & LastRow).Value 
'Next line not needed as you redim it right after.
'arrBL = ws.Range("BL2:BL" & LastRow).Value 

ReDim arrBL(1 To UBound(arrQ), 1 To 1) 

For j = 1 To UBound(arrQ)
    answer = "WIN"
    
    If arrQ(j,1)=answer Then
        arrBL(j, 1) = 1  
    Else
        arrBL(j, 1) = 0 
    End If
Next j

ws.Range("BL1").Resize(UBound(arrBL), 1).Value = arrBL 

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:

Dim ws As Worksheet
Dim j As Long, LastRowOutcomeCleaned As Long
Dim arrQ as Variant, arrBL As Variant
Dim answer as string

'Note: this is dangerous as the order of sheets may change
'      Consider using the codename instead.  
Set ws = Workbooks("A.xlsx").Sheets(2) 
LastRow = ws.Range("Q" & Rows.Count).End(xlUp).Row

arrQ = ws.Range("Q2:Q" & LastRow).Value 
'Next line not needed as you redim it right after.
'arrBL = ws.Range("BL2:BL" & LastRow).Value 

ReDim arrBL(1 To UBound(arrQ), 1 To 1) 

For j = 1 To UBound(arrQ)
    answer = "WIN"
    
    If arrQ(j,1)=answer Then
        arrBL(j, 1) = 1  
    Else
        arrBL(j, 1) = 0 
    End If
Next j

ws.Range("BL1").Resize(UBound(arrBL), 1).Value = arrBL 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文