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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

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