我需要学习一种跳过方法?或退出if例程,如果第一行不符合条件,则继续执行下一个例程。 阅读:ActiveCell.Offset(0,-1)=“ F”。
Ps。该子项位于Change事件中。
首先,我真的很陌生,为了获得更好的成绩,我一直在看书和看书。到目前为止,我的所有问题中有99%在前面的主题中已得到回答。这是我的第一个例外。欢迎阅读材料提示和技巧
我尝试过:如果不是ActiveCell.Offset(0,-1)=“ F”,则退出Sub 在这种情况下,这只是杀死了整个程序。 Ive还尝试将其拆分为5个不同的子对象,并尝试从change event子对象中调用它们,但是我在那也得到了错误。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("D:D")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now
'Written by Bjoern Monroe (04.27.2019) - [Rev 1]
'If the above cell is = L then the selected cell can't contain "VS" or "VN"
If ActiveCell.Offset(0, -1) = "L" Then
ElseIf ActiveCell.Text = "VS" Or ActiveCell.Text = "VN" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
'If the above cell is = F then the selected cell can't contain "VS" or "VN"
If ActiveCell.Offset(0, -1) = "F" Then
ElseIf ActiveCell.Text = "VS" Or ActiveCell.Text = "VN" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
'If the above cell is = S then the selected cell can't contain "L", "VS", "F", or "S"
If ActiveCell.Offset(0, -1) = "S" Then
ElseIf ActiveCell.Text = "L" Or ActiveCell.Text = "VS" Or ActiveCell.Text = "F" Or ActiveCell.Text = "S" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
'If the above cell is = VN then the selected cell can't contain "L", "VN", "F", or "S"
If ActiveCell.Offset(0, -1) = "VN" Then
ElseIf ActiveCell.Text = "L" Or ActiveCell.Text = "VN" Or ActiveCell.Text = "F" Or ActiveCell.Text = "S" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
'If the above cell is = VS then the selected cell can't contain "VS", "L", Or "S"
If ActiveCell.Offset(0, -1) = "VS" Then
ElseIf ActiveCell.Text = "VS" Or ActiveCell.Text = "L" Or ActiveCell.Text = "S" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
End Sub
我有一列,总是由用户填充F,L,S,VN和VS。 但是填充的值顺序几乎总是相同的。 如果上方的列单元格为F,则活动单元格只能为F,L或S。例如,决不能为VN或VS。我希望用户收到一条弹出消息,告诉他们他们将要编写错误的序列。
到目前为止的结果。我得到5个弹出框,而不是我期望的。这是有道理的,因为潜艇认为我希望它检查所有5个,即使它是否符合条件。
答案 0 :(得分:2)
第一步是修复If
语法,因为ElseIf
不应缩进,如果满足条件,则不执行任何操作。
我还添加了对ActiveCell.Column
的检查,因为如果您选择第一列中的任何行,您的代码将引发错误:
'If the above cell is = L then the selected cell can't contain "VS" or "VN"
If ActiveCell.Column > 1 Then
If ActiveCell.Offset(0, -1) = "L" Then
'Nothing to execute?
ElseIf ActiveCell.Text = "VS" Or ActiveCell.Text = "VN" Then
MsgBox ("Trigger Warning")
Else
MsgBox ("Wont Trigger Warning")
End If
End If