Consulting

Results 1 to 3 of 3

Thread: VBA Userform macro help for range dates

  1. #1
    VBAX Newbie
    Joined
    Oct 2022
    Posts
    2
    Location

    VBA Userform macro help for range dates

    hi.

    I currently have a excel macro that allows the end user to update the roster based on a single date.

    It will have a userform pop out they fill in leave date, first name, leave type. Then click ADD.

    This will go to leave the tab and add that information to the next blank row under the 'tblEmployees'

    So what I having been trying to work out is how do I do the above but with a range of dates.
    IE. 07/10/2022 - 15/10/2022

    I have attached 2 images:
    • Userform (what end user fills in)
    • tblemployees (where it updates)


    Below is the code for this:

    Private Sub cbbAdd_Click()
    Dim LeaveDate As Date
    LeaveDate = txtLeaveDate.Text
    Dim FirstName As String
    FirstName = FirstNameCombo.Text
    Dim LeaveType As String
    LeaveType = LeaveTypeCombo.Text
    Dim wsh As Worksheet
    Set wsh = ThisWorkbook.Worksheets("Leave")
    Set tbl = wsh.ListObjects("tblEmployees")
    Dim lRow As ListRow
    Set lRow = tbl.ListRows.Add
    With lRow
    .Range(1) = LeaveDate
    .Range(2) = FirstName
    .Range(3) = LeaveType
    End With
    End Sub
    Thank you
    Troy
    Attached Images Attached Images
    Last edited by Aussiebear; 10-06-2022 at 04:06 PM. Reason: Added code tags to supplied code

  2. #2
    you name the New Textbox as txtLeaveEnd, then you can modify your code:
    Private Sub cbbAdd_Click()
        Dim LeaveDate As Date
        'LeaveDate = txtLeaveDate.Text
        Dim FirstName As String
        FirstName = FirstNameCombo.Text
        Dim LeaveType As String
        LeaveType = LeaveTypeCombo.Text
        Dim wsh As Worksheet
        Set wsh = ThisWorkbook.Worksheets("Leave")
        Set tbl = wsh.ListObjects("tblEmployees")
        Dim lRow As ListRow
        For LeaveDate = CDate(txtLeaveStart.Text) To CDate(txtLeaveEnd.Text)
            Set lRow = tbl.ListRows.Add
            With lRow
                .Range(1) = LeaveDate
                .Range(2) = FirstName
                .Range(3) = LeaveType
            End With
        Next
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Oct 2022
    Posts
    2
    Location
    Thank you, that worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •