Search code examples
excelvbauserform

excel vba only comments may appear after end sub


I am in the process of building a Userform to transfer data to a worksheet called "Staff Data". I have searched online for the coding and managed to cobble together the following code -

I seem to have come across a stumbling block. Every time I try to click on the "Add Name" button I get the following error code

Compile Error: Only comments may appear after End Sub, End Function, or End Property. I also get the FrmData part of the code below highlighted in blue

If FrmData.ARLArea = True Then AreaValue = "ARL"

AreaValue are Option Buttons nested in Frame 1 GradeValue are Option Buttons nested in Frame 2

So I am not sure if the problem lies with the way I have coded for gathering information from the Option Buttons, or the problems lies elsewhere.

Can someone please point me in the right direction to resolving this problem?

Private Sub AddName_Click()

'Variable Declaration
Dim BlnVal As Boolean

Dim RowCount As Long
Dim r As Long
Const LastRow = 2000
RowCount = Worksheets("Staff Data").Cells(Rows.Count, 1).End(xlUp).Row
 With Worksheets("Staff Data")
  .Cells(RowCount + 1, 1).Value = AreaValue.Value
  .Cells(RowCount + 1, 2).Value = FrmData.txtEmployeeNo1.Value
  .Cells(RowCount + 1, 3).Value = FrmData.txtFirstName1.Value
  .Cells(RowCount + 1, 4).Value = FrmData.txtLastName1.Value
  .Cells(RowCount + 1, 5).Value = GradeValue.Value
 End With
End Sub

    'Find Area value
    If FrmData.ARLArea = True Then AreaValue = "ARL"
    If FrmData.LSQArea = True Then AreaValue = "LSQ"
    If FrmData.KNBArea = True Then AreaValue = "KNB"
    If FrmData.RSQArea = True Then AreaValue = "RSQ"
    If FrmData.RevenueControlInspectors = True Then AreaValue = "RCI"
    If FrmData.SpecialRequirementsTeam = True Then AreaValue = "SRT"


    'Find Grade value
    If FrmData.CSA2 = True Then GradeValue = "CSA2"
    If FrmData.CSA1 = True Then GradeValue = "CSA1"
    If FrmData.CSS2 = True Then GradeValue = "CSS2"
    If FrmData.CSS1 = True Then GradeValue = "CSS1"
    If FrmData.CSM2 = True Then GradeValue = "CSM2"
    If FrmData.CSM1 = True Then GradeValue = "CSM1"
    If FrmData.AM = True Then GradeValue = "AM"
    If FrmData.RCI = True Then GradeValue = "RCI"
    If FrmData.SRT = True Then GradeValue = "SRT"

  On Error GoTo ErrOccured
    'Boolean Value
    BlnVal = 0

    'Data Validation
    Call Data_Validation

    'Check validation of all fields are completed are not
    If BlnVal = 0 Then Exit Sub


    'TurnOff screen updating
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

ErrOccured:
    'TurnOn screen updating
    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub

Solution

  • You have an End Sub statement on line 17 that is causing your program to not see anything beyond that. Try deleting that line and seeing if it works as expected or if there are other issues that haven't been detected yet.

    It seems like this entire section will be skipped

        If FrmData.ARLArea = True Then AreaValue = "ARL"
        If FrmData.LSQArea = True Then AreaValue = "LSQ"
        If FrmData.KNBArea = True Then AreaValue = "KNB"
        If FrmData.RSQArea = True Then AreaValue = "RSQ"
        If FrmData.RevenueControlInspectors = True Then AreaValue = "RCI"
        If FrmData.SpecialRequirementsTeam = True Then AreaValue = "SRT"
    
    
        'Find Grade value
        If FrmData.CSA2 = True Then GradeValue = "CSA2"
        If FrmData.CSA1 = True Then GradeValue = "CSA1"
        If FrmData.CSS2 = True Then GradeValue = "CSS2"
        If FrmData.CSS1 = True Then GradeValue = "CSS1"
        If FrmData.CSM2 = True Then GradeValue = "CSM2"
        If FrmData.CSM1 = True Then GradeValue = "CSM1"
        If FrmData.AM = True Then GradeValue = "AM"
        If FrmData.RCI = True Then GradeValue = "RCI"
        If FrmData.SRT = True Then GradeValue = "SRT"  
    

    since you are ending sub at line 17 and last line which looks like you may have been trying to either close the entire program or just have it close if error is detected. I can't tell for sure.