Error Updating a Null Date to a Worksheet from a User Form

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I've built a User Form that will update a few different worksheets. I have a few date fields that won't always be populated, if the Client doesn't sign up for particular services. When I test the form, I'm getting an error because the date is null. I don't want to populate the field with text, as there are some calculations that I'll be building in, based off dates. I'm thinking some sort of error handling might work, but I've never worked with error handling before and honestly, I'm still quite new to VBA. I've pasted the code in question below, with the impacted line in red font. Thoughts on the best approach?

Private Sub cmd_Submit_Click()


Dim LastRow As Long
Dim LastRow2 As Long
Dim LastRow3 As Long


Set ws1 = ThisWorkbook.Sheets("Client Info")
Set ws2 = ThisWorkbook.Sheets("Client Measurements")
Set ws3 = ThisWorkbook.Sheets("Payment Tracker")


LastRow = ws1.Range("C" & Rows.Count).End(xlUp).Row + 1
LastRow2 = ws2.Range("C" & Rows.Count).End(xlUp).Row + 1
LastRow3 = ws3.Range("C" & Rows.Count).End(xlUp).Row + 1


ws1.Range("B" & LastRow).Value = CDate(Me.txt_Updated)
ws1.Range("C" & LastRow).Value = (Me.txt_First)
ws1.Range("D" & LastRow).Value = (Me.txt_Last)
ws1.Range("E" & LastRow).Value = (Me.txt_Suffix)
ws2.Range("B" & LastRow2).Value = CDate(Me.txt_Updated)
ws2.Range("C" & LastRow2).Value = (Me.txt_First)
ws2.Range("D" & LastRow2).Value = (Me.txt_Last)
ws2.Range("E" & LastRow2).Value = (Me.txt_Suffix)
ws1.Range("G" & LastRow).Value = CDate(Me.txt_DoB)
ws1.Range("H" & LastRow).Value = (Me.cobo_Gender)
ws1.Range("I" & LastRow).Value = (Me.txt_SignupAge)
ws1.Range("K" & LastRow).Value = (Me.txt_Phone)
ws1.Range("L" & LastRow).Value = (Me.txt_Email)
ws1.Range("M" & LastRow).Value = CDate(Me.txt_DPStart)
ws1.Range("N" & LastRow).Value = CDate(Me.txt_DCStart)
ws1.Range("O" & LastRow).Value = CDate(Me.txt_OCStart)
ws1.Range("P" & LastRow).Value = CDate(Me.txt_CTIStart)
ws1.Range("Q" & LastRow).Value = CDate(Me.txt_CTOStart)
ws2.Range("G" & LastRow2).Value = (Me.cobo_EntryType)
ws2.Range("H" & LastRow2).Value = (Me.txt_Height)
ws2.Range("I" & LastRow2).Value = (Me.txt_Weight)
ws2.Range("J" & LastRow2).Value = (Me.txt_Chest)
ws2.Range("K" & LastRow2).Value = (Me.txt_Hips)
ws2.Range("L" & LastRow2).Value = (Me.txt_Waist)
ws2.Range("M" & LastRow2).Value = (Me.txt_BicepL)
ws2.Range("N" & LastRow2).Value = (Me.txt_BicepR)
ws2.Range("O" & LastRow2).Value = (Me.txt_ThighL)
ws2.Range("P" & LastRow2).Value = (Me.txt_ThighR)
ws2.Range("Q" & LastRow2).Value = (Me.txt_CalfL)
ws2.Range("R" & LastRow2).Value = (Me.txt_CalfR)




Me.txt_Updated = ""
Me.txt_First = ""
Me.txt_Last = ""
Me.txt_Suffix = ""
Me.txt_Updated = ""
Me.txt_First = ""
Me.txt_Last = ""
Me.txt_Suffix = ""
Me.txt_DoB = ""
Me.cobo_Gender = ""
Me.txt_SignupAge = ""
Me.txt_Phone = ""
Me.txt_Email = ""
Me.txt_DPStart = ""
Me.txt_DCStart = ""
Me.txt_OCStart = ""
Me.txt_CTIStart = ""
Me.txt_CTOStart = ""
Me.cobo_EntryType = ""
Me.txt_Height = ""
Me.txt_Weight = ""
Me.txt_Chest = ""
Me.txt_Hips = ""
Me.txt_Waist = ""
Me.txt_BicepL = ""
Me.txt_BicepR = ""
Me.txt_ThighL = ""
Me.txt_ThighR = ""
Me.txt_CalfL = ""
Me.txt_CalfR = ""


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Something like
Code:
If Not Len(Me.txt_OCStart) = 0 Then ws1.Range("O" & LastRow).Value = CDate(Me.txt_OCStart)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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