Excelmacros在所有列的所有行中逐个单元格查找“BLOCKED”
在这个小部分的代码中,尽pipe条件被设置为true,但是它转到了Else
语句。
Sub check() Dim temp As Integer Sheets("Sheet1").Select For j = 18 To 29 For i = 2 To 39 If Cells(i, j).Value = "BLOCKED" Then temp = temp + 1 Else temp = 0 End If Next MsgBox temp Next End Sub
尽pipeBLOCKED出现在我正在运行这个macros的Excel工作表中,但它不会返回true。
请帮我看看我在做什么的错误。
我怀疑你还没有真正地跨过你的代码,但是你只是说“它去了Else”,因为MsgBox
正在显示0
,它现在会这样做(除非任何列中的最后一个单元格不是“BLOCKED” “),因为每当你find一个没有被”阻塞“的单元时,你正在重置temp
的值。
尝试对你的代码进行这些修改,看看他们是否给出你期望的答案。
每个列中存在“BLOCKED”次数的版本:
Sub check() Dim temp As Integer With Sheets("Sheet1") For j = 18 To 29 temp = 0 For i = 2 To 39 If .Cells(i, j).Value = "BLOCKED" Then temp = temp + 1 End If Next MsgBox temp & " cell" & IIf(temp = 1, "", "s") & " in column " & j & IIf(temp = 1, " is", " are") & " 'BLOCKED'" Next End With End Sub
在整个区域里,有多less次“堵塞”的版本:
Sub check() Dim temp As Integer With Sheets("Sheet1") temp = 0 For j = 18 To 29 For i = 2 To 39 If .Cells(i, j).Value = "BLOCKED" Then temp = temp + 1 End If Next Next MsgBox temp & " cell" & IIf(temp = 1, "", "s") & " in entire area " & IIf(temp = 1, " is", " are") & " 'BLOCKED'" End With End Sub
请检查“BLOCKED”单元中是否有额外的空格。 也尝试获取单元格的文本
If Cells(i, j).Text= "BLOCKED" Then
它发生的“堵塞”不完全是一个单元格的内容。 你可能想试试这个。
If instr(1, Cells(i, j), "BLOCKED")>0 Then
希望能帮助到你。