I want to see if this button click can be less messy. Basically:
if checkbox is checked {
if some values appear on Past Contacts {
msgbox
}else add form values to sheets Contact and past contact
}else add values to contacts
Thanks
if checkbox is checked {
if some values appear on Past Contacts {
msgbox
}else add form values to sheets Contact and past contact
}else add values to contacts
Code:
Private Sub btnAdd_Click()
Dim who, conInitiate, conLastName, conFirstName As String
Dim conCIO, conEmail, conPhone, conSummary, conType As String
Dim conDate, conStartTime, conEndTime As Date
Dim lastRow, lastRowPC As Long
Dim cWs, PCWs As Worksheet
who = Me.cmbWho.Value
conDate = Me.txtDate.Value
conInitiate = Me.cmbInitiate.Value
conLastName = Me.txtLastName.Value
conFirstName = Me.txtFirstName.Value
conCIO = Me.cmbCIO.Value
conEmail = Me.txtEmail.Value
conPhone = Me.txtPhone.Value
conSummary = Me.txtSummary.Value
conType = Me.cmbType.Value
conStartTime = Me.txtStart.Value
conEndTime = Me.txtEndTime.Value
Set PCWs = Worksheets("PastContacts")
Set cWs = Worksheets("Contact")
' last row of Contact WS
lastRow = cWs.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Row
lastRowPC = PCWs.Range("a" & Rows.Count).End(xlUp).Offset(1, 0).Row
If Me.cboxFreq.Value = True Then
If WorksheetFunction.CountIf(PCWs.Range("A2:A" & lastRow), conLastName) > 0 Then
If WorksheetFunction.CountIf(PCWs.Range("B2:b" & lastRow), conFirstName) > 0 Then
MsgBox "The name " & conFirstName & " " & conLastName & "is already on the list.", vbOKOnly
End If
End If
cWs.Range("A" & lastRow).Value = who
cWs.Range("B" & lastRow).Value = Format(conDate, "mm/dd/yyyy")
cWs.Range("C" & lastRow).Value = conInitiate
cWs.Range("D" & lastRow).Value = conLastName
cWs.Range("E" & lastRow).Value = conFirstName
cWs.Range("F" & lastRow).Value = conCIO
cWs.Range("G" & lastRow).Value = conEmail
cWs.Range("H" & lastRow).Value = conPhone
cWs.Range("I" & lastRow).Value = conSummary
cWs.Range("J" & lastRow).Value = conType
cWs.Range("K" & lastRow).Value = Format(conStartTime, "hh:mm")
cWs.Range("L" & lastRow).Value = Format(conEndTime, "hh:mm")
cWs.Range("M" & lastRow).FormulaR1C1 = Format("=CalcTime(RC11,RC12)", "hh:mm")
PCWs.Range("A" & lastRowPC).Value = conLastName
PCWs.Range("B" & lastRowPC).Value = conFirstName
PCWs.Range("C" & lastRowPC).Value = conCIO
PCWs.Range("D" & lastRowPC).Value = conEmail
PCWs.Range("E" & lastRowPC).Value = conPhone
Call SortAndRemoveDupes
clearCtrl Me
Me.Hide
Else 'checkbox not checked
cWs.Range("A" & lastRow).Value = who
cWs.Range("B" & lastRow).Value = Format(conDate, "mm/dd/yyyy")
cWs.Range("C" & lastRow).Value = conInitiate
cWs.Range("D" & lastRow).Value = conLastName
cWs.Range("E" & lastRow).Value = conFirstName
cWs.Range("F" & lastRow).Value = conCIO
cWs.Range("G" & lastRow).Value = conEmail
cWs.Range("H" & lastRow).Value = conPhone
cWs.Range("I" & lastRow).Value = conSummary
cWs.Range("J" & lastRow).Value = conType
cWs.Range("K" & lastRow).Value = Format(conStartTime, "hh:mm")
cWs.Range("L" & lastRow).Value = Format(conEndTime, "hh:mm")
cWs.Range("M" & lastRow).FormulaR1C1 = Format("=CalcTime(RC11,RC12)", "hh:mm")
clearCtrl Me
Me.Hide
End If
End Sub
Thanks