I am trying to get a loop to work.
我正试图找到一个循环来工作。
What i need to happen is to loop over a sheet and if address matches in column A does column W have a value, if all instances of address have values in column W then GoTo end_nothing
. If only some matches have data in column W then continue with sub.
我需要做的是对一个表进行循环,如果a列中的地址匹配,那么W列是否有值,如果所有地址的实例都有W列中的值,那么GoTo end_nothing。如果只有一些匹配在列W中有数据,然后继续使用sub。
The problem I am having with my code is that the first entry it matches, it exits the sub without looping over the rest.
我的代码存在的问题是它匹配的第一个条目,它退出子条目而不循环遍历其他条目。
My Code:
我的代码:
Private Sub CommandButton3_Click()
Dim add_WIR_check, lastRow_WIR As Long
Dim address_Check As String: address_Check = sheets("Auto Checklist").cells(3, 2).value
lastRow_WIR = sheets("Works Instruction Record").cells(Rows.count, "A").End(xlUp).Row
***** Unprotect Sheet
For add_WIR_check = 3 To lastRow_WIR
If sheets("Works Instruction Record").cells(add_WIR_check, 1) = address_Check Then
If sheets("Works Instruction Record").cells(add_WIR_check, 23) <> "" Then
GoTo end_nothing
End If
End If
Next add_WIR_check
***** Additional code here
end_nothing:
***** Protect sheet
End Sub
1 个解决方案
#1
1
Your VBA code snippet does not comply with the business logic as described: you must loop through all matching rows in the range to find out if all cells in the Column "W" contain value. It may be achieved by using auxiliary Boolean
var ok
, like shown below:
您的VBA代码片段不符合所描述的业务逻辑:您必须循环遍历范围内的所有匹配行,以查明“W”列中的所有单元格是否包含值。可以通过使用辅助布尔var ok实现,如下所示:
Dim ok as boolean
ok = True
For add_WIR_check = 3 To lastRow_WIR
If sheets("Works Instruction Record").cells(add_WIR_check, 1) = address_Check Then
If Not (sheets("Works Instruction Record").cells(add_WIR_check, 23) <> "") Then
ok = False
End If
End If
Next add_WIR_check
Upon completion, the var ok
value will indicate if all cells in Column "W" of the matching rows contain values.
完成后,var ok值将指示匹配行的“W”列中的所有单元格是否包含值。
Hope this may help.
希望这可以帮助。
#1
1
Your VBA code snippet does not comply with the business logic as described: you must loop through all matching rows in the range to find out if all cells in the Column "W" contain value. It may be achieved by using auxiliary Boolean
var ok
, like shown below:
您的VBA代码片段不符合所描述的业务逻辑:您必须循环遍历范围内的所有匹配行,以查明“W”列中的所有单元格是否包含值。可以通过使用辅助布尔var ok实现,如下所示:
Dim ok as boolean
ok = True
For add_WIR_check = 3 To lastRow_WIR
If sheets("Works Instruction Record").cells(add_WIR_check, 1) = address_Check Then
If Not (sheets("Works Instruction Record").cells(add_WIR_check, 23) <> "") Then
ok = False
End If
End If
Next add_WIR_check
Upon completion, the var ok
value will indicate if all cells in Column "W" of the matching rows contain values.
完成后,var ok值将指示匹配行的“W”列中的所有单元格是否包含值。
Hope this may help.
希望这可以帮助。