object required

mfung828

New Member
Joined
Dec 24, 2014
Messages
4
Hi,

Im new to vba. Literally started learning the language 3 days ago. I have created a textbox within a userform so that my users can input a date. But i keep getting "object required." please help. here is my code.

Dim Final_Date_of_Service As Date




Private Sub Date_of_Service_Txtbox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Date_of_Service As Date
Set Date_of_Service = Date_of_Service_Txtbox


If Date_of_Service <> "" Then
If IsDate(Date_of_Service) Then
Date_of_Service = Format(Date_of_Service, "MMDDYYYY")
Final_Date_of_Service = Date_of_Service
Else
MsgBox "Please enter a valid date in the format MMDDYYYY!", vbCritical
Date_of_Service = ""
Cancel = True
End If
End If
End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi,
You have named your textbox Date_of_Service_Txtbox

But in your code you refer to: Date_of_Service which I assume is meant to be the same textbox??

Update your code to match the textbox name and hopefully, this should remove the error.

Dave
 
Upvote 0
Welcome to the MrExcel board!

You have quite a few things mixed up in that code.

1. You have declared Date_of_Service As Date. You wouldn't use 'Set' to apply a value to that variable, just Date_of_Service = Date_of_Service_Txtbox. However, this will error (Type mismatch) if the text box is exited blank or with some text value that cannot be interpreted as a date.

2. If Date_of_Service <> "" Then That is trying to compare a Date to a (non-date) String (Type mismatch)

3. Date_of_Service = Format(Date_of_Service, "MMDDYYYY") The Format function returns a String so this line is trying to put a String into a Date variable (Type mismatch)

4. Date_of_Service = "" Again trying to put a String into a Date variable (Type mismatch)
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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