我正在为我的工作场所创建一个改进的时间表,尝试实现宏以对手动输入的数据进行错误检查。
我有一个可以按预期工作的代码,但是当我更改了逻辑测试后,它就停止了工作。
我自己不擅长VBA编码,因此我正在寻求帮助。
Sub ErrorMsg2()
Dim a As Long, b As Long
Dim Msg As String
For a = 28 To 44 Step 4
For b = 4 To 8
If Application.CountIf(Range("C" & a).Resize(, 6), "(=0.7 or =0.8)") Then
If Cells(a, b) = (0.7 Or 0.8) And Cells(a, (b - 1)) = (0.9 Or 1) Then
If Msg = "" Then Msg = Cells(24, b) Else Msg = Msg & ", " & Cells(24, b)
End If
End If
Next b
MsgBox Range("A" & a) & " Has been scheduled a night shift followed by a day shift on " & Msg & vbNewLine & "Please Rectify." & vbNewLine & _
"Press OK to acknowledge.", vbExclamation + vbOKOnly, "Error"
Msg = ""
Next a
End Sub
例如,如果单元格D28 = 0.8且单元格C28 = 1,则将出现消息框。
但是,当b = 9时会出现消息框。
答案 0 :(得分:1)
“或”子句的工作方式有所不同,以下是相关代码段:
If Application.CountIf(Range("C" & a).Resize(, 6), 0.7) + Application.CountIf(Range("C" & a).Resize(, 6), 0.8) > 0 Then
If (Cells(a, b) = 0.7 Or Cells(a, b) = 0.8) And (Cells(a, (b - 1)) = 0.9 Or Cells(a, (b - 1)) = 1) Then
If Msg = "" Then
Msg = Cells(24, b)
Else
Msg = Msg & ", " & Cells(24, b)
End If
End If
Next b