简体   繁体   English

从Excel工作表到用户格式的日期为dd / mm / yyy格式?

[英]Dates from excel worksheet to userform in dd/mm/yyy format?

So, this is really driving me crazy now, I have had a lot of problems working with dates on VBA due to formatting issues, since I need my dates to be in dd/mm/yyyy format. 所以,这真的让我发疯了,由于格式问题,在VBA上处理日期时遇到了很多问题,因为我需要将日期设置为dd / mm / yyyy格式。 I did a lot of searching on the internet, and I found a lot of discussion about these date formats in VBA, however I could not find a solution to my problem. 我在Internet上进行了大量搜索,并且在VBA中找到了很多关于这些日期格式的讨论,但是找不到解决方案。

The problem : I have some dates stored on a worksheet, formatted as dd/mm/yyyy - I stored them there through userforms, and I had some problems with the formatting but it worked out fine in the end -, and I want to display one of those dates on a userform label. 问题 :我在工作表上存储了一些日期,格式为dd / mm / yyyy-我通过用户窗体将日期存储在其中,格式存在一些问题,但最终效果很好-我想显示用户表单标签上的日期之一。 Here is the code I wrote for that: 这是我为此编写的代码:

 Me.Label2_Prazo.Caption = Format(Plan1.Cells(pos, 5), "dd/mm/yyyy")

However, it still displays the date incorrectly. 但是,它仍然显示不正确的日期。 On my worksheet, the date is stored as "05/03/2016" (As in march 5th, 2016), but still whenever I try to display it on the userform it appears as "03/05/2016". 在我的工作表上,日期存储为“ 05/03/2016”(如2016年3月5日),但是无论何时我尝试在用户窗体上显示该日期,它都显示为“ 03/05/2016”。

I have already tried many different ways to make this work, but I can't seem to solve this. 我已经尝试了许多不同的方法来完成这项工作,但是我似乎无法解决这个问题。

Thanks in advance for any help you can provide! 预先感谢您提供的任何帮助!

I (personally) favor the solution of constructing the format myself. 我(个人)赞成自己构造格式的解决方案。 This is probably due to the fact that I have worked for several year in a corporate environment where I had to deal with Russian, Hungarian, German, USA, Chinese, and many other date formats. 这可能是由于我在公司环境中工作了几年而不得不处理俄语,匈牙利语,德语,美国,中文和许多其他日期格式的事实。 Some of them follow strange formats such as YYYY. MMMM D. 其中一些遵循奇怪的格式,例如YYYY. MMMM D. YYYY. MMMM D. . YYYY. MMMM D. Anyway, in your case I would therefore propose the following: 无论如何,因此,在您的情况下,我将提出以下建议:

Dim dttDate As Date

dttDate = CDate(Plan1.Cells(pos, 5).Value)
Me.Label2_Prazo.Caption = Right("0" & Day(dttDate), 2) & "/" & Right("0" & Month(dttDate), 2) & "/" & Year(dttDate)

On another note: while in VBA I try to construct most of my dates using DateSerial ONLY to ensure that I am really generating the date I intend to (and don't get mixed up in the various different date formats). 另一个要注意的是:在VBA中,我尝试仅使用DateSerial构造大多数日期,以确保确实生成了我想要的日期(并且不会混淆各种不同的日期格式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM