I'm trying to make one UserForm do multiple tasks.
The first is to have a user enter and submit new data.
The second is to be able open the same UserForm and to look up previously entered data.
The third is to be able to make changes to previously entered data and change/update the WorkSheet (haven't started on that part yet).
This UserForm has 25 Labels with 25 TextBoxes next to each Label.
The Labels populate automatically based on a selection made in the cboDept combobox, and this works fine (see Code 1 and Code 2 below), the second part (see Code 3 below) does not work. The look up data function works only in the first TextBox, and I do not know how to have the UserForm 'find' the values that match the Labels for the next Textboxes, for a selected date.
The data is sorted in four columns in another Worksheet ("WorkDB"); Name, Department, Work-Type and Hours.
My challenge is to find the TextBox Values for each Label, for the same Name, on the same Date, for up to 25 different labels.
I think I am staring the answer in the face, but can not figure it out.
Any suggestions are most welcome.
Code 1. This is the code that changes the labels in the UserForm based on the Department that was selected. This works fine:
Private Sub cboDept_Change()
Dim idx As Long
Dim i As Long
idx = cboDept.ListIndex
If idx <> -1 Then
With Sheet8
For i = 3 To 27
Me.Controls("Label" & i + 12).Caption = .Range("A" & i).Offset(0, idx).Text
Next i
End With
End If
End Sub
Code 2. This is the code that places the data from the UserForm into another worksheet ("WorkDB"), this also works fine:
Private Sub cmdOK3_Click()
Application.ScreenUpdating = False
Application.Calculation = xlCalculateManual
Dim rng As Range, wd As Worksheet, r As Long
Set wd = ActiveWorkbook.Sheets("WorkDB")
r = wd.Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To 25
If Me.Controls("TextBox" & i).Value <> "" Then
r = r + 1: Set rng = wd.Range("A" & r)
rng.Offset(-1, 0).EntireRow.Copy: rng.PasteSpecial xlPasteFormulas
rng.Value = cboName1.Value
rng.Offset(0, 1) = cboDept.Value
rng.Offset(0, 2).Value = Me.Controls("Label" & i + 14).Caption
rng.Offset(0, 3).Value = txtdate.Value
rng.Offset(0, 4).Value = Me.Controls("TextBox" & i).Value
Set rng = rng.Offset(1)
End If
Next i
'Sheets("Main Menu").Select
Unload Me
UserForm1.Show
Application.ScreenUpdating = True
End Sub
3. This is the code in the same UserForm that is supposed to let the user look up previously entered data. I still need to add the code that will allow the user to edit this data and update WorkDB.
Private Sub cmdFind_Click()
Dim lastrow
Dim myfname As String
With ThisWorkbook.Sheets("WorkDB")
lastrow = Sheets("WorkDB").Range("A" & Rows.Count).End(xlUp).Row
myfname = cboName1.Text
txtdate = txtdate.Text
MyDept = cboDept.Text
For CurrentRow = lastrow To 3 Step -1
If ThisWorkbook.Sheets("WorkDB").Cells(CurrentRow, 1).Text = myfname And _
ThisWorkbook.Sheets("WorkDB").Cells(CurrentRow, 2).Text = MyDept And _
ThisWorkbook.Sheets("WorkDB").Cells(CurrentRow, 4).Text = txtdate Then
cboName1.Text = Sheets("WorkDB").Cells(CurrentRow, 1).Text
cboDept.Text = Sheets("WorkDB").Cells(CurrentRow, 2).Text
Label15 = Sheets("WorkDB").Cells(CurrentRow, 3).Text
TextBox1.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
'TextBox2.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
'TextBox3.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
'TextBox4.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
'TextBox5.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
'TextBox6.Text = Sheets("WorkDB").Cells(CurrentRow, 5).Text
End If
Next CurrentRow
End With
cboName1.SetFocus
End Sub
Bookmarks