Question : Excel / VBA / Userform

I have a userform which data is inputted and the information is written to cells on sheet 1. The following fields are captured information, date, course, duration, trainer, id, first name, last name, status. This part all works fine.

The problem I have is with one small section called development type. I need development type to be entered if selected and write to sheet 1 "New Material", "Existing Material", "Other",  depending on the option button chosen without the requirement of the employee details section being filled out.

At the moment this part only works if the employee details section is also filled out. I cannot get it to to work otherwise, but have posted my code. Someone helped me with this and I added that part in so I'm sure i didn't do it correctly.

Here is my code and highlighted in bold is where the trouble is I think.

Private Sub cmdOk_Click()
    Dim RowCount As Long
    Dim ctl As Control
    Dim ctlName As String
    Dim ctlNumber As String
    Dim ctlField As String
' Check user input
   If Me.txtDate.Value = "" Then
        MsgBox "Please enter a Date.", vbExclamation, "Training Log"
        Exit Sub
    End If
        If Me.cboCourseName.Value = "" Then
        MsgBox "Please enter Course Name.", vbExclamation, "Training Log"
        Exit Sub
    End If
    If Me.txtDuration.Value = "" Then
        MsgBox "Please enter duration in hours.", vbExclamation, "Training Log"
        Exit Sub
    End If
    If Me.cboTrainer.Value = "" Then
        MsgBox "Please choose cost centre.", vbExclamation, "Training Log"
        Exit Sub
    End If
    If Not IsNumeric(Me.txtDuration.Value) Then
        MsgBox "The Duration must be a number.", vbExclamation, "Training Log"
        Exit Sub
    End If
    If Not IsDate(Me.txtDate.Value) Then
        MsgBox "The Date box must contain a date.", vbExclamation, "Training Log"
        Exit Sub
    End If

' Write data to worksheet
    ' get next available row
    RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
    ' get the details for each line with data (not blank) into an array
    ' then write all fields to the next line using "rowCount"
    For Each ctl In Me.Controls
    ctlName = Left(ctl.Name, 12)        ' get the field name
    If ctlName = "txtFirstName" Then
    If ctl.Value <> vbNullString Then
    ctlNumber = Replace(ctl.Name, "txtFirstName", "") ' get the field number
    ' write in common/general fields
        With Worksheets("Sheet1").Range("A1")
                ' trainer details
                .Offset(RowCount, 0).Value = DateValue(Me.txtDate.Value)
                .Offset(RowCount, 1).Value = Me.cboCourseName.Value
                .Offset(RowCount, 2).Value = Me.txtDuration.Value
                .Offset(RowCount, 3).Value = Me.cboTrainer.Value
                ' development details
                If FirstOptBtn = True Then
                    .Offset(RowCount, 9).Value = "New Material"
                ElseIf SecondOptBtn = True Then
                    .Offset(RowCount, 9).Value = "Existing Material"
                ElseIf ThirdOptBtn = True Then
                    .Offset(RowCount, 9).Value = "Other"
                End If
                ' Date entered
                .Offset(RowCount, 10).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
            End With

            ' work out the current field number and write rest of data line
            ctlField = "txtEmployeeID" & Trim(ctlNumber)
            Worksheets("Sheet1").Range("A1").Offset(RowCount, 4).Value = Me.Controls(ctlField).Value
            ctlField = "txtFirstName" & Trim(ctlNumber)
            Worksheets("Sheet1").Range("A1").Offset(RowCount, 5).Value = Me.Controls(ctlField).Value
            ctlField = "txtLastName" & Trim(ctlNumber)
            Worksheets("Sheet1").Range("A1").Offset(RowCount, 6).Value = Me.Controls(ctlField).Value
            ctlField = "cboStatus" & Trim(ctlNumber)
            Worksheets("Sheet1").Range("A1").Offset(RowCount, 7).Value = Me.Controls(ctlField).Value
            ctlField = "cboDepartment" & Trim(ctlNumber)
            Worksheets("Sheet1").Range("A1").Offset(RowCount, 8).Value = Me.Controls(ctlField).Value
        ' increment row count for each new employee data line
        RowCount = RowCount + 1
        End If
        End If
    Next ctl
Call clear_form
MsgBox "Update completed", vbInformation + vbOKOnly, "Complete"
End Sub

also it loops date, course,duration,trainer, according to the number of employees entered in the employee detail section. this part works fine as stated.
Hope this makes sense and any help to get the development type details working would be great!

Answer : Excel / VBA / Userform

I'm sorry, at the moment we have insufficient information to fully debug your code, and it isn't clear exactly what you are doing.

I didn't understand your penultimate comment fully, but procedures (Subs or Functions) will usually still work when pasted between code modules. Blocks of code from within procedures would need more care.

Re your last comment: If you feel that your original question has been answered, then it would be best to close the question and raise a new one. Otherwise, can you clarify the position at the moment?

Random Solutions  
programming4us programming4us