I'm new to vba so please forgive my ignorance. I am using Excel 2010 on Windows 7.
I have created a custom userform in a workbook "Assistant Hire Log - Individual". The userform works correctly in populating sheet 1 "Hire Log".
Upon pushing the "OK" button, I would like the userform to also populate a different workbook "Assistant Hire Log - Universal" which also has a sheet one "Hire Log". I have found some posts regarding updating two different worksheets form one userform, but is it possible to update two different workbooks from one userform. In advance, thank you so much for any help...
Current code listed below:
Private Sub butOK_Click()
Dim iRow1 As Long
Dim iRow2 As Long
Dim WS1 As Worksheet, WS2 As Worksheet
Set WS1 = Worksheets("Assistant Hire Log - Individual")
Set WS2 = Worksheets("Assistant Hire Log - Universal")
'Find first empty row in database
iRow1 = WS1.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row
'Find first empty row in database
iRow2 = WS2.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row
'Check user input
If Me.txtFirstName.Value = "" Then
MsgBox "Please enter a First Name.", vbexlamation, "Hire Log"
Me.txtFirstName.SetFocus
Exit Sub
End If
If Me.txtLastName.Value = "" Then
MsgBox "Please enter a Last Name.", vbexlamation, "Hire Log"
Me.txtLastName.SetFocus
Exit Sub
End If
If Me.txtEmail.Value = "" Then
MsgBox "Please enter an Email Address.", vbexlamation, "Hire Log"
Me.txtEmail.SetFocus
Exit Sub
End If
If Me.txtIDNo.Value = "" Then
MsgBox "Please enter an ID Number.", vbexlamation, "Hire Log"
Me.txtIDNo.SetFocus
Exit Sub
End If
If Me.txtEIDNo.Value = "" Then
MsgBox "Please enter an EID Number.", vbexlamation, "Hire Log"
Me.txtEIDNo.SetFocus
Exit Sub
End If
'Write data to database "Assistant Hire Log - Individual"
WS1.Cells(iRow1, 0).Value = Me.txtFirstName.Value
WS1.Cells(iRow1, 1).Value = Me.txtLastName.Value
WS1.Cells(iRow1, 2).Value = Me.txtIDNo.Value
WS1.Cells(iRow1, 3).Value = Me.txtEIDNo.Value
WS1.Cells(iRow1, 4).Value = Me.txtEmail.Value
WS1.Cells(iRow1, 5).Value = Me.CombHireType.Value
WS1.Cells(iRow1, 6).Value = Me.CombHireTypeII.Value
WS1.Cells(iRow1, 7).Value = Me.ComboStatus.Value
WS1.Cells(iRow1, 8).Value = Me.ComboStatusII.Value
WS1.Cells(iRow1, 9).Value = Me.txtLR.Value
WS1.Cells(iRow1, 10).Value = Me.txtJobTitle.Value
WS1.Cells(iRow1, 11).Value = Me.txtCC.Value
WS1.Cells(iRow1, 12).Value = Me.txtDepartment.Value
WS1.Cells(iRow1, 13).Value = Me.txtManager.Value
WS1.Cells(iRow1, 14).Value = Me.CombHourly.Value
WS1.Cells(iRow1, 15).Value = Me.txtNotes.Value
WS1.Cells(iRow1, 16).Value = Format(Me.txtDateReceived.Value, "MM/DD/YY")
WS1.Cells(iRow1, 17).Value = Format(Me.txtEffectiveDate.Value, "MM/DD/YY")
WS1.Cells(iRow1, 18).Value = Format(Me.txtBGStart.Value, "MM/DD/YY")
WS1.Cells(iRow1, 19).Value = Format(Me.txtBGClear.Value, "MM/DD/YY")
WS1.Cells(iRow1, 20).Value = Format(Me.txtPhysical.Value, "MM/DD/YY")
WS1.Cells(iRow1, 21).Value = Format(Me.txtFinal.Value, "MM/DD/YY")
WS1.Cells(iRow1, 22).Value = Format(Me.txtHRISdate.Value, "MM/DD/YY")
WS1.Cells(iRow1, 24).Value = Me.ComboEntity.Value
WS1.Cells(iRow1, 25).Value = Me.TxtInitials.Value
WS1.Cells(iRow1, 29).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
'Write data to database "Assistant Hire Log - Universal"
WS2.Cells(iRow2, 0).Value = Me.txtFirstName.Value
WS2.Cells(iRow2, 1).Value = Me.txtLastName.Value
WS2.Cells(iRow2, 2).Value = Me.txtIDNo.Value
WS2.Cells(iRow2, 3).Value = Me.txtEIDNo.Value
WS2.Cells(iRow2, 4).Value = Me.txtEmail.Value
WS2.Cells(iRow2, 5).Value = Me.CombHireType.Value
WS2.Cells(iRow2, 6).Value = Me.CombHireTypeII.Value
WS2.Cells(iRow2, 7).Value = Me.ComboStatus.Value
WS2.Cells(iRow2, 8).Value = Me.ComboStatusII.Value
WS2.Cells(iRow2, 9).Value = Me.txtLR.Value
WS2.Cells(iRow2, 10).Value = Me.txtJobTitle.Value
WS2.Cells(iRow2, 11).Value = Me.txtCC.Value
WS2.Cells(iRow2, 12).Value = Me.txtDepartment.Value
WS2.Cells(iRow2, 13).Value = Me.txtManager.Value
WS2.Cells(iRow2, 14).Value = Me.CombHourly.Value
WS2.Cells(iRow2, 15).Value = Me.txtNotes.Value
WS2.Cells(iRow2, 16).Value = Format(Me.txtDateReceived.Value, "MM/DD/YY")
WS2.Cells(iRow2, 17).Value = Format(Me.txtEffectiveDate.Value, "MM/DD/YY")
WS2.Cells(iRow2, 18).Value = Format(Me.txtBGStart.Value, "MM/DD/YY")
WS2.Cells(iRow2, 19).Value = Format(Me.txtBGClear.Value, "MM/DD/YY")
WS2.Cells(iRow2, 20).Value = Format(Me.txtPhysical.Value, "MM/DD/YY")
WS2.Cells(iRow2, 21).Value = Format(Me.txtFinal.Value, "MM/DD/YY")
WS2.Cells(iRow2, 22).Value = Format(Me.txtHRISdate.Value, "MM/DD/YY")
WS2.Cells(iRow2, 24).Value = Me.ComboEntity.Value
WS2.Cells(iRow2, 25).Value = Me.TxtInitials.Value
WS2.Cells(iRow2, 29).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
'Clear the form
For Each Ctl In Me.Controls
If TypeName(Ctl) = "TextBox" Or TypeName(Ctl) = "ComboBox" Then
Ctl.Value = ""
ElseIf TypeName(Ctl) = "OptionButton" Then
Ctl.Value = False
End If
Next Ctl
End Sub
I have created a custom userform in a workbook "Assistant Hire Log - Individual". The userform works correctly in populating sheet 1 "Hire Log".
Upon pushing the "OK" button, I would like the userform to also populate a different workbook "Assistant Hire Log - Universal" which also has a sheet one "Hire Log". I have found some posts regarding updating two different worksheets form one userform, but is it possible to update two different workbooks from one userform. In advance, thank you so much for any help...
Current code listed below:
Private Sub butOK_Click()
Dim iRow1 As Long
Dim iRow2 As Long
Dim WS1 As Worksheet, WS2 As Worksheet
Set WS1 = Worksheets("Assistant Hire Log - Individual")
Set WS2 = Worksheets("Assistant Hire Log - Universal")
'Find first empty row in database
iRow1 = WS1.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row
'Find first empty row in database
iRow2 = WS2.Cells(Rows.Count, 2) _
.End(xlUp).Offset(1, 0).Row
'Check user input
If Me.txtFirstName.Value = "" Then
MsgBox "Please enter a First Name.", vbexlamation, "Hire Log"
Me.txtFirstName.SetFocus
Exit Sub
End If
If Me.txtLastName.Value = "" Then
MsgBox "Please enter a Last Name.", vbexlamation, "Hire Log"
Me.txtLastName.SetFocus
Exit Sub
End If
If Me.txtEmail.Value = "" Then
MsgBox "Please enter an Email Address.", vbexlamation, "Hire Log"
Me.txtEmail.SetFocus
Exit Sub
End If
If Me.txtIDNo.Value = "" Then
MsgBox "Please enter an ID Number.", vbexlamation, "Hire Log"
Me.txtIDNo.SetFocus
Exit Sub
End If
If Me.txtEIDNo.Value = "" Then
MsgBox "Please enter an EID Number.", vbexlamation, "Hire Log"
Me.txtEIDNo.SetFocus
Exit Sub
End If
'Write data to database "Assistant Hire Log - Individual"
WS1.Cells(iRow1, 0).Value = Me.txtFirstName.Value
WS1.Cells(iRow1, 1).Value = Me.txtLastName.Value
WS1.Cells(iRow1, 2).Value = Me.txtIDNo.Value
WS1.Cells(iRow1, 3).Value = Me.txtEIDNo.Value
WS1.Cells(iRow1, 4).Value = Me.txtEmail.Value
WS1.Cells(iRow1, 5).Value = Me.CombHireType.Value
WS1.Cells(iRow1, 6).Value = Me.CombHireTypeII.Value
WS1.Cells(iRow1, 7).Value = Me.ComboStatus.Value
WS1.Cells(iRow1, 8).Value = Me.ComboStatusII.Value
WS1.Cells(iRow1, 9).Value = Me.txtLR.Value
WS1.Cells(iRow1, 10).Value = Me.txtJobTitle.Value
WS1.Cells(iRow1, 11).Value = Me.txtCC.Value
WS1.Cells(iRow1, 12).Value = Me.txtDepartment.Value
WS1.Cells(iRow1, 13).Value = Me.txtManager.Value
WS1.Cells(iRow1, 14).Value = Me.CombHourly.Value
WS1.Cells(iRow1, 15).Value = Me.txtNotes.Value
WS1.Cells(iRow1, 16).Value = Format(Me.txtDateReceived.Value, "MM/DD/YY")
WS1.Cells(iRow1, 17).Value = Format(Me.txtEffectiveDate.Value, "MM/DD/YY")
WS1.Cells(iRow1, 18).Value = Format(Me.txtBGStart.Value, "MM/DD/YY")
WS1.Cells(iRow1, 19).Value = Format(Me.txtBGClear.Value, "MM/DD/YY")
WS1.Cells(iRow1, 20).Value = Format(Me.txtPhysical.Value, "MM/DD/YY")
WS1.Cells(iRow1, 21).Value = Format(Me.txtFinal.Value, "MM/DD/YY")
WS1.Cells(iRow1, 22).Value = Format(Me.txtHRISdate.Value, "MM/DD/YY")
WS1.Cells(iRow1, 24).Value = Me.ComboEntity.Value
WS1.Cells(iRow1, 25).Value = Me.TxtInitials.Value
WS1.Cells(iRow1, 29).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
'Write data to database "Assistant Hire Log - Universal"
WS2.Cells(iRow2, 0).Value = Me.txtFirstName.Value
WS2.Cells(iRow2, 1).Value = Me.txtLastName.Value
WS2.Cells(iRow2, 2).Value = Me.txtIDNo.Value
WS2.Cells(iRow2, 3).Value = Me.txtEIDNo.Value
WS2.Cells(iRow2, 4).Value = Me.txtEmail.Value
WS2.Cells(iRow2, 5).Value = Me.CombHireType.Value
WS2.Cells(iRow2, 6).Value = Me.CombHireTypeII.Value
WS2.Cells(iRow2, 7).Value = Me.ComboStatus.Value
WS2.Cells(iRow2, 8).Value = Me.ComboStatusII.Value
WS2.Cells(iRow2, 9).Value = Me.txtLR.Value
WS2.Cells(iRow2, 10).Value = Me.txtJobTitle.Value
WS2.Cells(iRow2, 11).Value = Me.txtCC.Value
WS2.Cells(iRow2, 12).Value = Me.txtDepartment.Value
WS2.Cells(iRow2, 13).Value = Me.txtManager.Value
WS2.Cells(iRow2, 14).Value = Me.CombHourly.Value
WS2.Cells(iRow2, 15).Value = Me.txtNotes.Value
WS2.Cells(iRow2, 16).Value = Format(Me.txtDateReceived.Value, "MM/DD/YY")
WS2.Cells(iRow2, 17).Value = Format(Me.txtEffectiveDate.Value, "MM/DD/YY")
WS2.Cells(iRow2, 18).Value = Format(Me.txtBGStart.Value, "MM/DD/YY")
WS2.Cells(iRow2, 19).Value = Format(Me.txtBGClear.Value, "MM/DD/YY")
WS2.Cells(iRow2, 20).Value = Format(Me.txtPhysical.Value, "MM/DD/YY")
WS2.Cells(iRow2, 21).Value = Format(Me.txtFinal.Value, "MM/DD/YY")
WS2.Cells(iRow2, 22).Value = Format(Me.txtHRISdate.Value, "MM/DD/YY")
WS2.Cells(iRow2, 24).Value = Me.ComboEntity.Value
WS2.Cells(iRow2, 25).Value = Me.TxtInitials.Value
WS2.Cells(iRow2, 29).Value = Format(Now, "dd/mm/yyyy hh:nn:ss")
'Clear the form
For Each Ctl In Me.Controls
If TypeName(Ctl) = "TextBox" Or TypeName(Ctl) = "ComboBox" Then
Ctl.Value = ""
ElseIf TypeName(Ctl) = "OptionButton" Then
Ctl.Value = False
End If
Next Ctl
End Sub