Vba Excel宏查找“;“封锁”;在所有列的所有行中逐个单元格
在代码的这一小部分中,尽管条件设置为true,但它会转到Vba Excel宏查找“;“封锁”;在所有列的所有行中逐个单元格,vba,excel,Vba,Excel,在代码的这一小部分中,尽管条件设置为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
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
虽然我运行此宏的Excel工作表中存在BLOCKED,但它不会返回true
请帮我找出我所做的错误 请检查“阻止”单元格中是否有额外的空白。还可以尝试使用获取单元格的文本
If Cells(i, j).Text= "BLOCKED" Then
我怀疑您实际上没有仔细阅读您的代码,但您只是说“它转到其他位置”,因为
MsgBox
正在显示0
,它当前将显示0
(除非任何列中的最后一个单元格未被“阻止”)因为每次找到未被“阻止”的单元格时,都会重置temp
的值
尝试对代码进行这些更改,看看它们是否给出了您期望的答案
计算每列中存在“阻止”次数的版本:
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
计算整个区域中存在“阻塞”次数的版本:
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
碰巧“阻塞”并不完全是单元格的内容。
你可能想试试这个
If instr(1, Cells(i, j), "BLOCKED")>0 Then
希望有帮助。尝试在
如果,debug.Print单元格(i,j).Value
之前添加调试行,然后也添加debug.Print单元格(i,j).Value2
,查看在这两种情况下都得到了什么,尝试避免使用工作表(“Sheet1”)。选择,可以使用工作表(“Sheet1”)添加
在第一个循环之前,如果您在问题中包含工作表的单元格图像R2:AC39
,可能会有所帮助。这将帮助我们确定它是否由于包含“阻塞”
、或“阻塞”
、或“阻塞”
、或其他类似问题的工作表而失败。