VBA Date issue

markgrnh

New Member
Joined
Apr 7, 2023
Messages
18
Office Version
  1. 365
Platform
  1. Windows
Morning all, Sorry doing this without showing the code as I dont have access to it at the moment, but in short I have a calendar form that when selected it transfers that date into a textbox on another userform. Now it all works fine if the day is the 13th or more but if its before the day and month get swapped over.

I am working on UK dates so dd/mm/yyyy and set it up so it formats as date = Format(userform1.textbox2.value , "dd/mm/yyyy") or something like that, sorry dont have access at the moment. Not sure if it matters, but I am from the UK so my reginal settings are set to UK as well.

Is there another way rather than using the format function to make a date stick to the dd/mm/yyyy format so the month and days dont get swapped over when its less than the 13th of the month?

From some research I have seen the use of CDate, but I will be honest, I have never used that, or know how that works.

Sorry I know this is really vague, and I am not giving you all much to look at or work with, but wanted to see if anyone else has had the same issue and worked out a workaround?

Many thanks
Mark
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi,

What comes out of your textbox is text & need to coerce your string date to date using one of the available type conversion functions like CDate when you post the textbox value to the range. Providing your regional settings are set to use UK format this should result in correct format but you can also apply number format if required

example

Code:
With Range("A10")

.Value = CDate(Me.TextBox1.Value)

.NumberFormat = "dd/mm/yyyy"

End With

Do be aware that CDate will error if the value in the textbox is not a recognised date.



Dave
 
Upvote 1
Solution
Amazing thanks Dave, just got to work and tested your above and it worked straight away, many thanks my friend :)
 
Upvote 0
Most welcome glad suggestion resolved your issue & appreciate your feedback

Dave
 
Upvote 0

Forum statistics

Threads
1,215,206
Messages
6,123,639
Members
449,111
Latest member
ghennedy

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