PDA

View Full Version : [SOLVED:] VBA Userform macro help for range dates



troy101
10-06-2022, 12:45 PM
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

arnelgp
10-06-2022, 06:57 PM
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

troy101
10-06-2022, 07:18 PM
Thank you, that worked.