VBA Combination of If and For Loop

Maryna

New Member
Joined
Dec 3, 2019
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I have a training register workbook with three worksheets. Each worksheet contains a table

Sheet Name - Table Name
TrainingReg - tblTrainingReg
TrainingActivities - tblTrainingAct
EmployeeInfo - tblEmployeeInfo

What I Want: If a new activity is added to tblTrainingAct then the new activity is also added to tblTraingReg for each employee.

For Example: There are 12 employees in tblEmployeeInfo. Then If a new activity e.g. Cleaning is added to tblTrainingAct then the activity will be added 12 times to tblTrainingReg as well.

I have underlined the parts of the code I am struggling with. I may also have completely missed the boat with the approach I am taking.
I know the rest of my code is clunky but I am new to vba and will work on cleaning up the code once I have achieved my goal.

Private Sub cmdAddActivity_Click() 'Commands executed when "Click to Transfer Information" is chosen

Dim Alastrow As Long

'looks for last filled row in tblEmployeeInfo
Alastrow = ThisWorkbook.Worksheets("TrainingActivities").Cells(Rows.Count, 1).End(xlUp).Row

'Transfers information typed in user form to tblEmployeeInfo
With ThisWorkbook.Worksheets("TrainingActivities")
.Cells(.Rows.Count, "A").End(xlUp).Offset(1) = txtActivityID.Value
txtActivityID.Text = Application.Max(.Range("A:A")) + 1
.Cells(Alastrow + 1, 2).Value = txtTrainingAct.Text
.Cells(Alastrow + 1, 3).Value = ComboBoxType.Text
End With

'Empties form and closes window
Unload Me

'Goes to TrainingReg sheet in Workbook
Worksheets("TrainingActivities").Activate

'This is the lines of code I am struggling with
'If a new activity is added to tblTrainingAct then new activity is added to tblTrainingReg for each employee in tblEmployeeInfo

Dim NewActivity As Variant
Dim Elastrow As Long
Dim i As Long
Dim tblActivity As Object, LastRow As Object

Set tblActivity = Worksheets("TrainingActivities").ListObjects("tblTrainingAct")
Set LastRow = Worksheets("TrainingReg").ListObjects("tblTrainingReg").ListRows.Add

Elastrow = ThisWorkbook.Worksheets("EmployeeInfo").Cells(Rows.Count, 1).End(xlUp).Row

NewActivity = txtTrainingAct.Value

If NewActivity = "" Then

MsgBox ("No New Activity Added")

Else

For i = 2 To Elastrow '(from the first row in tblEmployeeInfo to the last row)

tblActivity.Range.Cells 'line of code not complete
LastRow.Range.Offset(0, 3).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

Next i

End If

End Sub

Private Sub UserForm_Initialize() 'when the user form starts up

With UserForm2.txtActivityID
txtActivityID.Text = Application.Max(Range("A:A")) + 1 'automatically generate next available Training Activity ID number (IDs are sequential)
End With

With UserForm2.ComboBoxType 'fill combo box with available options
.AddItem "Performance Review"
.AddItem "QIS / QIC"
.AddItem "SOP / Verbal"
.AddItem "Control Chart - CRM / QC / SPIKE"
.AddItem "Duplicate Sample / Spike Recovery"
End With

End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi, If I understand you, I think that Power Query would be choice
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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