Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.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代码中存在错误_Vba_Excel - Fatal编程技术网

VBA代码中存在错误

VBA代码中存在错误,vba,excel,Vba,Excel,我的代码的目的是,如果L1到W1000之间有任何长度为7的数据,则可以打印 虽然它找到了长度7的值,但我的代码不服从exit for 原因是什么 Private Sub CommandButton1_Click() Dim Prod As Variant Dim Dev As Variant Dim counter As Integer Dim j As Variant Prod = Array("PBA_100", "PCA_500", "PRD_500", "PGA_500", "PVD

我的代码的目的是,如果L1到W1000之间有任何长度为7的数据,则可以打印

虽然它找到了长度7的值,但我的代码不服从exit for

原因是什么

Private Sub CommandButton1_Click()

Dim Prod As Variant
Dim Dev As Variant
Dim counter As Integer
Dim j As Variant

Prod = Array("PBA_100", "PCA_500", "PRD_500", "PGA_500", "PVD_500")

For j = LBound(Prod) To UBound(Prod)
     MsgBox Prod(j)

 With ThisWorkbook.Sheets(Prod(j))
 LastRow = ThisWorkbook.Sheets(Prod(j)).Columns("A").Cells.Find("*", 
 SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    For Each cell In .Range("N2:N" & LastRow)
        arr = Split(Replace(cell.Value, "  ", " "), " ")
        For Each arrElem In arr
            If Len(arrElem) = 7 Then
            MsgBox arrElem
        Exit For
      Else
      MsgBox arrElem
            End If
        Next arrElem
    Next cell
 End With

Next j

End Sub

如果找到可用值,则需要退出嵌套for循环。像这样:

With ThisWorkbook.Sheets(Prod(j))
LastRow = ThisWorkbook.Sheets(Prod(j)).Columns("A").Cells.Find("*", 
SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row
    For Each cell In .Range("L1:W1000", .Cells(.Rows.Count, "A").End(xlUp))
        arr = Split(Replace(cell.Value, "  ", " "), " ")
        For Each arrElem In arr
            If Len(arrElem) = 7 Then
            ThisWorkbook.Sheets("Sheet1").Range("D" & k).Value = "available"
            Exit For 'You exit only the nested loop here!
            Else
            ThisWorkbook.Sheets("Sheet1").Range("D" & k).Value = "NOT available"
            End If
        Next arrElem
    Next cell
End With

其中k是定义的?k是在顶部定义的。我只包括了我遇到问题的部分。@AnirudhChauhan-用F8一步一步地开始调试,并尝试注意arrElem是什么及其值。例如,在If LenarrElem=7之前添加debug.print arrElem。希望在按F8大约30分钟后,您会看到解决方案。