簡體   English   中英

我收到編譯錯誤,預計會識別 function 錯誤

[英]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 (和SubProperty )關鍵字的規則使 VBA 解析器/編譯器期望標識符名稱緊跟在FUNCTION令牌之后的空格之后:

Function NameOfTheFunction

如果你在 VBE 中輸入這個,你會得到一個合法的無參數 function 定義,如下所示:

Function NameOfTheFunction()
|
End Function

()括號分隔函數的參數列表,您可以在其中為 function 定義任意 [合理] 數量的參數:

  • ByVal NameOfTheParameter按值傳遞參數;
  • ByRef NameOfTheParameter ,或只是NameOfTheParameter通過引用傳遞參數;
  • 為每個參數添加一個As子句,以指定參數類型。

看起來您從前一個塊的Exit FunctionFunction開始復制了一段代碼,然后在代碼中間粘貼了一個雜散的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM