Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Excel宏查找“;“封锁”;在所有列的所有行中逐个单元格_Vba_Excel - Fatal编程技术网

Vba Excel宏查找“;“封锁”;在所有列的所有行中逐个单元格

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

在代码的这一小部分中,尽管条件设置为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
虽然我运行此宏的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
,可能会有所帮助。这将帮助我们确定它是否由于包含
“阻塞”
、或
“阻塞”
、或
“阻塞”
、或其他类似问题的工作表而失败。