fixing date 31/12/1899 into textbox on userform

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
hi
I have this code , when I write the day of date in textbox then should complete the current month and current year but it shows this date 31/12/1899
so if I write 1 then should complete the date like this 01/06/2021
VBA Code:
Private Sub TextBox1_Change()
  Dim dDate As Date
 dDate = DateSerial(Year(Date), Month(Date), Day(Date))
 TextBox1.Value = Format(TextBox1.Value, "dd/mm/yyyy")
 dDate = TextBox1.Value
End Sub
any suggestion to fix it ,please?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I suspect using the AfterUpdate event more likely to allow you to do what you are trying to do

See if this update to your code helps

VBA Code:
Private Sub TextBox1_AfterUpdate()
    Dim dDate As Date
    dDate = DateValue(Val(Me.TextBox1.Value) & "/" & Month(Date))
    TextBox1.Value = Format(dDate, "dd/mm/yyyy")
End Sub

Type the day of month & then exit the TextBox - code should then add current month & year

Dave
 
Upvote 0
Solution
I suspect using the AfterUpdate event more likely to allow you to do what you are trying to do
actually I did it but nothing changes
also your updating doesn't work the problem still continues
 
Upvote 0
actually I did it but nothing changes
also your updating doesn't work the problem still continues

Did you use my code as published (unchanged) and also delete your old code?
You need to EXIT the control for this event to trigger

Dave
 
Upvote 0
Did you use my code as published (unchanged) and also delete your old code?
sorry my bad ! now it works thanks for your supporting
 
Upvote 0

Forum statistics

Threads
1,214,988
Messages
6,122,620
Members
449,092
Latest member
amyap

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