Input a date into a VBA form in Excel

PaulRob

New Member
Joined
Dec 17, 2023
Messages
13
Office Version
  1. 2010
Platform
  1. Windows
I am trying to have the user put in a DATE into a user form I have written into a VBA on the back of an Excel Spreadsheet.

Currently I have a txt box with the following coding:

Private Sub txtInServiceDate_Change()


End Sub

What/how do I change this so that the User inputs a date in the format mm/dd/yyyy please?

Thank you
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
VBA Code:
Private Sub txtInServiceDate_Change()

if not ISDATE(txtInServiceDate.Object.Text) then
    MsgBox"The data entered is not a date",vbcritical
else
    ' If you wish a specific format
    txtInServiceDate.Object.Text = Format(DateStr, "DD/MM/YYYY")
endif
End Sub
 
Upvote 0
Hi
welcome to forum

Don't worry too much about the format user enters a date - just check what they have entered is a date & then change it to your required format

VBA Code:
Private Sub txtInServiceDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Const DateFormat As String = "mm/dd/yyyy"
   
    With Me.txtInServiceDate
       Cancel = Len(.Value) > 0 And Not IsDate(.Value)
       If Not Cancel Then .Value = Format(.Value, DateFormat) Else .Value = ""
     End With
       
    If Cancel Then MsgBox "Please Enter A Valid In Service Date", 48, "Invalid"
   
End Sub

If user enters Dec 18 2023 should change to your format when they exit the control

Dave
 
Upvote 0
Solution
VBA Code:
Private Sub txtInServiceDate_Change()

if not ISDATE(txtInServiceDate.Object.Text) then
    MsgBox"The data entered is not a date",vbcritical
else
    ' If you wish a specific format
    txtInServiceDate.Object.Text = Format(DateStr, "DD/MM/YYYY")
endif
End Sub
Many thanks :)

Will check it out and use either this solution or the other one suggested!

Paul
 
Upvote 0
Hi
welcome to forum

Don't worry too much about the format user enters a date - just check what they have entered is a date & then change it to your required format

VBA Code:
Private Sub txtInServiceDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Const DateFormat As String = "mm/dd/yyyy"
  
    With Me.txtInServiceDate
       Cancel = Len(.Value) > 0 And Not IsDate(.Value)
       If Not Cancel Then .Value = Format(.Value, DateFormat) Else .Value = ""
     End With
      
    If Cancel Then MsgBox "Please Enter A Valid In Service Date", 48, "Invalid"
  
End Sub

If user enters Dec 18 2023 should change to your format when they exit the control

Dave
Many thanks :)

Will check it out and use either this solution or the other one suggested!

Paul
 
Upvote 0
Hi
welcome to forum

Don't worry too much about the format user enters a date - just check what they have entered is a date & then change it to your required format

VBA Code:
Private Sub txtInServiceDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Const DateFormat As String = "mm/dd/yyyy"
  
    With Me.txtInServiceDate
       Cancel = Len(.Value) > 0 And Not IsDate(.Value)
       If Not Cancel Then .Value = Format(.Value, DateFormat) Else .Value = ""
     End With
      
    If Cancel Then MsgBox "Please Enter A Valid In Service Date", 48, "Invalid"
  
End Sub

If user enters Dec 18 2023 should change to your format when they exit the control

Dave
Many thanks :)

Will check it out and use either this solution or the other one suggested!

Paul
 
Upvote 0
Hi
welcome to forum

Don't worry too much about the format user enters a date - just check what they have entered is a date & then change it to your required format

VBA Code:
Private Sub txtInServiceDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Const DateFormat As String = "mm/dd/yyyy"
  
    With Me.txtInServiceDate
       Cancel = Len(.Value) > 0 And Not IsDate(.Value)
       If Not Cancel Then .Value = Format(.Value, DateFormat) Else .Value = ""
     End With
      
    If Cancel Then MsgBox "Please Enter A Valid In Service Date", 48, "Invalid"
  
End Sub

If user enters Dec 18 2023 should change to your format when they exit the control

Dave
Similar question please Dave - how do I format to $x.xx (2 decimal places)? Or should I start a new thread please?
 
Upvote 0
Similar question please Dave - how do I format to $x.xx (2 decimal places)?

Similar way but probably in this case you can use the AfterUpdate event of the control

VBA Code:
Private Sub TextBox1_AfterUpdate()
    With Me.TextBox1
        .Value = Format(Val(.Value), "$#,##0.00")
    End With
End Sub

Do be mindful that what comes out of the control is Text & if want the same format to apply when posting to a range you should consider using an appropriate type-conversion-functions
to coerce to required data type & apply number format to the range.

Dave
 
Upvote 0
Similar way but probably in this case you can use the AfterUpdate event of the control

VBA Code:
Private Sub TextBox1_AfterUpdate()
    With Me.TextBox1
        .Value = Format(Val(.Value), "$#,##0.00")
    End With
End Sub

Do be mindful that what comes out of the control is Text & if want the same format to apply when posting to a range you should consider using an appropriate type-conversion-functions
to coerce to required data type & apply number format to the range.

Dave
Thank you Dave.

I may have misunderstood something as in my spreadsheet I put in:

Private Sub ActualCost_AfterUpdate()
With Me.txtActualCost.value = Format(Val(.value), "$#,##0.00")
End With
End Sub

On Compile I get a Method or Data Member not found.

Is this because In MODULE I have this field showing as .Numformat.value = "" or .Cells(iRow, 12) = frmForm.Numformat in different parts of my Module code?
 
Upvote 0
code goes in your userform code page & you have not copied the code I posted correctly.

VBA Code:
Private Sub txtActualCost_AfterUpdate()
    With Me.txtActualCost
        .Value = Format(Val(.Value), "$#,##0.00")
    End With
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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