[英]I am getting compile error expected identified function error
我将此代码插入 vba Excel 并在我运行调试编译后给我编译错误预期标识符 Function
End `Option Explicit
Function Validate() As Boolean
Dim frm As Worksheet
Set frm = ThisWorkbook.Sheets("Form")
Validate = True
With frm
.Range("I6").Interior.Color = xlNone
.Range("I8").Interior.Color = xlNone
.Range("I10").Interior.Color = xlNone
.Range("I12").Interior.Color = xlNone
.Range("I14").Interior.Color = xlNone
.Range("I16").Interior.Color = xlNone
.Range("I18").Interior.Color = xlNone
.Range("I20").Interior.Color = xlNone
End With
'Validating Trainee ID
If Trim(frm.Range("I6").Value) = "" Then
MsgBox "Trainee Id is blank.", vbOKOnly + vbInformation, "Trainee ID"
frm.Range("I6").Select
frm.Range("I6").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Trainee Name
If Trim(frm.Range("I8").Value) = "" Then
MsgBox "Trainee Name is blank.", vbOKOnly + vbInformation, "Trainee Name"
frm.Range("I8").Select
frm.Range("I8").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Gender
If Trim(frm.Range("I10").Value) <> "Female" And Trim(frm.Range("I10").Value) <> "Male" = "" Then
MsgBox "Please select valid Gender from Drop-dpwn.", vbOKOnly + vbInformation, "Gender"
frm.Range("I10").Select
frm.Range("I10").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Department
If Trim(frm.Range("I12").Value) = "" Then
MsgBox "Department is blank.", vbOKOnly + vbInformation, "Department"
frm.Range("I12").Select
frm.Range("I12").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Country
If Trim(frm.Range("I14").Value) = "" Then
MsgBox "Country is blank.", vbOKOnly + vbInformation, "Country"
frm.Range("I14").Select
frm.Range("I14").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Training
If Trim(frm.Range("I16").Value) = "" Then
MsgBox "Training is blank.", vbOKOnly + vbInformation, "Training"
frm.Range("I16").Select
frm.Range("I16").Interior.Color = vbRed
Validate = False
Exit Function
End If
我在 vba Excel 中插入此代码,在我运行调试编译后它给了我编译错误预期标识符
Function
'Validating Format
If Trim(frm.Range("I18").Value) <> "Face to Face" And Trim(frm.Range("I18").Value) <> "Online" = ""
Then
MsgBox "Please select valid Format from Drop-dpwn.", vbOKOnly + vbInformation, "Format"
frm.Range("I18").Select
frm.Range("I18").Interior.Color = vbRed
Validate = False
Exit Function
End If
'Validating Year Taken
If Trim(frm.Range("I20").Value) = "" Or Not IsNumeric(Trim(frm.Range("I20").Value)) Then
MsgBox "Please enter year of taking the course.", vbOKOnly + vbInformation, "Year Taken"
frm.Range("I20").Select
frm.Range("I20").Interior.Color = vbRed
Validate = False
Exit Function
End If`
Function
我将此代码插入 vba Excel 并在我运行调试编译后给我编译错误预期标识符 Function
这看起来很可疑:
End `Option Explicit
Option Explicit
应该是第一行的第一件事; 那个 `End` 看起来像一个复制粘贴的神器。
但是,这很可能是您描述的编译错误的原因:
Function
'Validating Format
Function
关键字是语言遵循/实现的语法语法规则的一部分,用于理解您正在编写的代码。
围绕Function
(和Sub
和Property
)关键字的规则使 VBA 解析器/编译器期望标识符名称紧跟在FUNCTION
令牌之后的空格之后:
Function NameOfTheFunction
如果你在 VBE 中输入这个,你会得到一个合法的无参数 function 定义,如下所示:
Function NameOfTheFunction()
|
End Function
()
括号分隔函数的参数列表,您可以在其中为 function 定义任意 [合理] 数量的参数:
ByVal NameOfTheParameter
按值传递参数;ByRef NameOfTheParameter
,或只是NameOfTheParameter
,通过引用传递参数;As
子句,以指定参数类型。 看起来您从前一个块的Exit Function
的Function
开始复制了一段代码,然后在代码中间粘贴了一个杂散的Function
关键字。
看起来这个问题可以泛化,这将通过使代码更易于扩展来防止将来发生此类错误。
据推测,某些单元格具有数据验证下拉菜单。 如果此类单元格包含无效值,工作表已经知道,因此我们可以将下拉验证推迟到 Excel。 这还包括验证一个值是否是 X 和 Y 之间的数字(或日期),并且在清除“忽略空白”选项的情况下,它还涵盖了当制作为要求内容长度大于的“文本长度”规则时所需的值0。
所以我们真正需要的是一个 function 告诉我们给定单元格是否存在验证错误,并让 Excel 的本机数据验证执行 rest:
Private Function HasErrors(ByVal TargetCell As Range) As Boolean
HasErrors = Not Application.Intersect(TargetCell, TargetCell.SpecialCells(xlCellTypeAllValidation)) Is Nothing
End Function
然后我们可以有一个验证单个字段的ValidateField
过程:
Private Function ValidateField(ByVal TargetCell As Range, Optional ByVal Message As String, Optional ByVal Title As String = "Validation Failed")
'data validation can hold the error message, no need to hard-code this data:
If Message = vbNullString Then Message = TargetCell.Validation.ErrorMessage
If HasErrors(TargetCell) Then
ValidateField = False
TargetCell.Interior.Color = vbRed
TargetCell.Select
MsgBox Message, vbOkOnly + vbInformation, Title
Else
ValidateField = True
TargetCell.Interior.Color = xlNone
End If
End Function
现在Validate
宏可以融入其中(假设验证消息被移动到数据验证中):
Public Function Validate() As Boolean ' consider making this a Sub procedure
With ThisWorkbook.Worksheets("Form") 'consider using the sheet's codename instead
'consider naming these ranges:
If Not ValidateField(.Range("I6")) Then Exit Function
If Not ValidateField(.Range("I8")) Then Exit Function
If Not ValidateField(.Range("I10")) Then Exit Function
'...
'named ranges would enhance readability:
If Not ValidateField(.Range("Country")) Then Exit Function
If Not ValidateField(.Range("YearTaken")) Then Exit Function
End With
Validate = True
End Function
请注意,在所有字段上定义了广泛的数据验证后, ThisWorkbook.Worksheets("Form").CircleInvalid
将使 Excel 在表单中的所有无效字段周围绘制一个红色验证圆圈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.