我已经汇总了一些代码,这些代码将在名为' test'的工作表中查看一些数据。检查以下内容:
(1)确认第2栏是否有错误。 (2)确认第3栏是否有错误。 (3)确认在定义的范围内是否有空白(从C的A1到最后一行)。
然后它会给你一个消息框,告诉你问题是什么。
代码是:
Sub check()
Dim y As Long, i As Long, LastRow As Long
Dim chkAA As Boolean, chkTEP As Boolean, chkNULL As Boolean
Dim row As Range, rng As Range, cell As Range
LastRow = Sheets("test").Cells(Rows.Count, "A").End(xlUp).row
Set rng = Sheets("Test").Range("A1:C" & LastRow)
Dim output As Integer
chkAA = True
chkTEP = True
chkNULL = True
For y = 1 To Rows.Count
If IsError(Sheets("Test").Cells(y, 2).Value) Then
chkAA = False
Exit For
End If
Next y
For i = 1 To Rows.Count
If Sheets(“Test”).Cells(i, 3).Value = "False" Then
chkTEP = False
Exit For
End If
Next i
For Each row In rng.Rows
For Each cell In row.Cells
If IsEmpty(cell.Value) Then
chkNULL = False
End If
Next cell
Next row
If chkAA = True And chkTEP = True And chkNULL = True Then
output = MsgBox("Validation complete", vbInformation, "Validation")
ElseIf chkAA = False And chkTEP = True And chkNULL = True Then
output = MsgBox("Double check your AA numbers", vbCritical, "Validation")
ElseIf chkAA = True And chkTEP = False And chkNULL = True Then
output = MsgBox("Double check your TEP combination", vbCritical, "Validation")
ElseIf chkAA = True And chkTEP = True And chkNULL = False Then
output = MsgBox("There is a blank cell", vbCritical, "Validation")
ElseIf chkAA = False And chkTEP = False And chkNULL = True Then
output = MsgBox("Double check your AA numbers and TEP combination", vbCritical, "Validation")
ElseIf chkAA = False And chkTEP = True And chkNULL = False Then
output = MsgBox("Double check your AA numbers and do not leave blanks", vbCritical, "Validation")
ElseIf chkAA = True And chkTEP = False And chkNULL = False Then
output = MsgBox("Double check your TEP combination and do not leave blanks", vbCritical, "Validation")
Else: chkAA = False And chkTEP = False And chkNULL = False:
output = MsgBox("Please make sure spreadhseet is filled correctly", vbCritical, "Validation")
End If
End Sub
它给了我一个超出范围的错误。
你能告诉我哪里出错了吗?
由于
答案 0 :(得分:2)
Sheets("Test")
尝试重新键入此行并重新运行。
修改强>
Jeeped指出,问题在于使用的引用类型。请注意下面的细微差别:
“& "
答案 1 :(得分:2)
现在您已经从包装工作表名称中删除了“智能引号”,如果必须检查列中的每个单元格,最好避免循环遍历1,048,576行。
with workSheets("Test")
'check for errors
if not .Columns("B").specialcells(xlCellTypeFormulas, xlErrors) is nothing then _
chkAA = False
'check for "False"
if not .Columns("C").find(what:="false", lookat:=xlwhole, matchcase:=false) is nothing then _
chkTEP = False
'check for blank cells
if not .rng.specialcells(xlCellTypeBlanks) is nothing then _
chkNULL = False
end if
此外,如果您要应用某种XOR按位逻辑来实现宣布的结果,则可能会更容易阅读。
dim chk as long
with workSheets("Test")
'check for errors
if not .Columns("B").specialcells(xlCellTypeFormulas, xlErrors) is nothing then _
chk = chk+1
'check for "False"
if not .Columns("C").find(what:="false", lookat:=xlwhole, matchcase:=false) is nothing then _
chk = chk+2
'check for blank cells
if not .rng.specialcells(xlCellTypeBlanks) is nothing then _
chk = chk+4
end if
select case chk
case 0 'none
output = MsgBox("Validation complete", vbInformation, "Validation")
case 1 'errors
output = MsgBox("Double check your AA numbers", vbCritical, "Validation")
case 2 'falses
output = MsgBox("Double check your TEP combination", vbCritical, "Validation")
case 3 'errors and falses
output = MsgBox("Double check your AA numbers and TEP combination", vbCritical, "Validation")
case 4 'blanks
output = MsgBox("There is a blank cell", vbCritical, "Validation")
case 5 'errors and blanks
output = MsgBox("Double check your AA numbers and do not leave blanks", vbCritical, "Validation")
case 6 'falses and blanks
output = MsgBox("Double check your TEP combination and do not leave blanks", vbCritical, "Validation")
case 7 'errors, falses and blanks
output = MsgBox("Please make sure spreadhseet is filled correctly", vbCritical, "Validation")
end select