首页 > 解决方案 > VBA - 不确定我的 if 语句中的错误在哪里(有多个条件)

问题描述

我不确定为什么我总是得到其他条件,而不是另一个。我不确定我的双重 if 条件语法是否错误。如果有人可以验证/知道出了什么问题,那就太好了。

基本上,如果该单元格中的值是 4 位数字并且第一个字母是 F,则不需要错误消息。

 RowCount = Cells(Rows.Count, "C").End(xlUp).Row
    For i = 2 To RowCount
        If Len(Cells(i, 4).Value) = 4 And Left(Cells(i, 4), 1) = "F" Then
        'Do nothing, no IA Code errors
            MsgBox "no errors"
        Else
            MsgBox "Error, please resubmit"
            
            Exit Sub
        End If
    Next i
    

标签: excelvba

解决方案


我只在第 2 行将“C”更改为 4。看起来您指的是错误的列来分配 RowCount。

Sub Error_NoError()
 RowCount = Cells(Rows.Count, 4).End(xlUp).Row
    For i = 2 To RowCount
        If Len(Cells(i, 4).Value) = 4 And Left(Cells(i, 4), 1) = "F" Then
        'Do nothing, no IA Code errors
            MsgBox "no errors"
        Else
            MsgBox "Error, please resubmit"
            
            Exit Sub
        End If
    Next i
End Sub

推荐阅读