Userform: Find date in worksheet and put data collected from Userform list box in the next empty rows

chips

Board Regular
Joined
Oct 21, 2008
Messages
52
I have a
Worksheet named ANAESData "ANAES Data Entry"
and a
userform named:
ufANData that has three list box
lbxDate (single option select) data from a named range rngDate which has 2 columns
lbxName (single option select) rngANAESNames
lbxShifts (Multi option select) rngShifts which has 2 columns

listboxes populate as planned

I have trouble inserting the data into the worksheet.

I have multiple tables for each day of the month. The date is at the top of the table. starting at L8 with the next date at q8 and the next at u8
I need to use the date selected in lbxDate to find the right row then add in the next blank row the shift data collected from lbxShifts
If there is more than one name selected then copy the shift data

LMNOPQRS
8wk1 14/6/2021wk1 15/6/2021
9codeShiftMon1 NamecodeshiftTue2 Name
10insert lbxShift col1lbxShift col2selected lbxName
copied from above if multiple names are selectedcopied from above if multiple names are selectednext lbxName

trying to code this is it possible?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
In cell C8 you literally have the text:
"wk1 14/6/2021"

In the lbxDate listbox, what is the date column, is it the first?
And how are the dates, for example: 14/06/2021

If all of the above is correct, then
Maybe some adjustments need to be made because you missed some information.

Change in the code "Sheet1" by the name of your sheet.

VBA Code:
Private Sub CommandButton1_Click()
  Dim sDate As String, sName As String
  Dim i As Long, col As Long, lr As Long
  Dim f As Range
  Dim sh As Worksheet
 
  If lbxDate.ListIndex = -1 Then
    MsgBox "Select date"
    Exit Sub
  End If
  If lbxName.ListIndex = -1 Then
    MsgBox "Select Name"
    Exit Sub
  End If
  If lbxShifts.ListIndex = -1 Then
    MsgBox "Select Shifts"
    Exit Sub
  End If
  Set sh = Sheets("Sheet1")
 
  sDate = Format(CDate(lbxDate.Value), "d/m/yyyy")
  sName = lbxName.Value
  Set f = sh.Rows(8).Find(sDate, , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    col = f.Column
    lr = sh.Columns(col).Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
    For i = 0 To lbxShifts.ListCount - 1
      If lbxShifts.Selected(i) Then
        sh.Cells(lr, col).Value = lbxShifts.List(i, 0)
        sh.Cells(lr, col + 1).Value = lbxShifts.List(i, 1)
        sh.Cells(lr, col + 2).Value = sName
        lr = lr + 1
      End If
    Next
  Else
    MsgBox "Date " & sDate & " does not exist"
  End If
End Sub
 
Upvote 0
Solution
I really must thank you so much! My description of my problem was not well defined however you gave me the information and knowledge to make it work! I am so grateful!!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top