cdate conversion from forms text box submission date errors if blank

dilfred

New Member
Joined
Jul 22, 2015
Messages
8
Hi, I am using Trevor Eyre's CalendarForm to enable date entries into my Excel Form, this works perfectly if the day is >12 as it is very clear which is the month/day and so correctly formats the output into UK dd/mm/yy, (which I subsequently convert to yy/mm/dd to avoid all future confusion), however if the day is less than 12 then it incorrectly shows the date, effectively swapping month/day over, so an entry of 6th Feb appears as the 2nd june instead.
I have added CDATE to the copy of the form data into a cell and this works perfectly and has resolved the above problem, HOWEVER if I press the populate data to spreadsheet before I have filled in this form entry it errors (IE cdate is trying to compute a blank entry). If I remove the cdate from the code below then it does not error when I run.

This is the code line which fails if I press go on the macro and I have not filled in the form "T_Need_Date" text box
VBA Code:
Sheet5.Cells(35, 11).value = CDate(FRM_Concession_Application.T_Need_Date)

This code with cdate removed works all the time, but sometimes creates a "back to front" date, US instead of UK
VBA Code:
Sheet5.Cells(35, 11).value = (FRM_Concession_Application.T_Need_Date)

if i remove the cdate from the above code it works without error.

How can I adjust the code above to stop it erroring if I accidently start the macro without populating the "T_need_Date" text box (ie it is still blank)

Thank you, I hope this is clear to understand.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
How can I adjust the code above to stop it erroring

Hi,
test first that your textbox contains a date

example

VBA Code:
With FRM_Concession_ApplicationT_Need_Date
    If IsDate(.Value) Then Sheet5.Cells(35, 11).Value = CDate(.Value)
End With
Dave
 
Last edited:
Upvote 0
Solution
Thank you, yes that works perfectly, just missing a full stop in the middle of the first line after Application. ;)

VBA Code:
With FRM_Concession_Application.T_Need_Date
    If IsDate(.Value) Then Sheet5.Cells(35, 11).Value = CDate(.Value)
End With
 
Upvote 0
Thank you, yes that works perfectly, just missing a full stop in the middle of the first line after Application. ;)

Hi
glad resolved, not sure what happened there thought changed your userform name to the Me keyword.

VBA Code:
With Me.T_Need_Date
    If IsDate(.Value) Then Sheet5.Cells(35, 11).Value = CDate(.Value)
End With

Appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,225,495
Messages
6,185,319
Members
453,287
Latest member
Emeister

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