简体   繁体   English

日期格式在Excel VBA中不起作用

[英]Date Format not working in excel vba

I'm having some difficulties with my datepicker date. 我的日期选择器日期遇到一些困难。 StartDate and EndDate are values from DatePicker and Debug.Print will give me "mm/dd/yyyy". StartDate和EndDate是DatePicker和Debug中的值。Print将给我“ mm / dd / yyyy”。 I am trying to change the date format to "dd/mmm/yyyy" so that I will be able to compare it with my data from MS access. 我试图将日期格式更改为“ dd / mm / yyyy”,以便可以将其与来自MS Access的数据进行比较。 But it's not working. 但这不起作用。

Sub cmdSubmit_Click()
    Dim StartDate As Date, EndDate As Date

    StartDate = DTPickStart.Value    ' 6/11/2018
    EndDate = DTPickEnd.Value        ' 6/24/2018

    StartDate = Format(StartDate, "dd/mmm/yyyy")
    EndDate = Format(EndDate, "dd/mmm/yyyy")

    Debug.Print StartDate           ' gave me 6/11/2018 instead of 11/Jun/2018
    Debug.Print EndDate
End Sub

Any advice on what I am doing wrong here? 关于我在这里做错的任何建议吗?

Thanks in advance! 提前致谢!

Format probably returns the date in the format you want, but it is then transformed into a date-value which is then printed in the default format for your system. 格式可能以您想要的格式返回日期,但是它将转换为日期值,然后以系统的默认格式打印该日期值。 To get them stored in the format you want, you could try something like: 要以所需的格式存储它们,可以尝试如下操作:

Option Explicit

Sub cmdSubmit_Click()
    Dim StartDate As Date, EndDate As Date
    Dim s1 As String, s2 As String

    StartDate = #6/11/2018#
    EndDate = #6/24/2018#

    'StartDate = Format(StartDate, "dd/mmm/yyyy")
    'EndDate = Format(EndDate, "dd/mmm/yyyy")

    s1 = Format(StartDate, "dd/mmm/yyyy")
    s2 = Format(EndDate, "dd/mmm/yyyy")

    Debug.Print StartDate & " - " & s1          ' gave me 6/11/2018 instead of 11/Jun/2018
    Debug.Print EndDate & " - " & s2
End Sub

Alternately if you want them in a cell as dates, you could set the NumberFormat of the range you write them to, to be in your wanted format: 或者,如果您希望它们在单元格中作为日期,则可以将写入它们的范围的NumberFormat设置为所需的格式:

Sheet1.Range("A1") = StartDate
Sheet1.Range("A2") = EndDate
Sheet1.Range("A1:A2").NumberFormat = "dd.mmm.yyyy"

If you need to format the date in VBA you have 2 options: 如果需要在VBA中格式化日期,则有两个选项:

  1. Change the regional settings; 更改区域设置;
  2. Write to Excel, format there and bring the .Text : 写入Excel,在那里格式化并带.Text

Option Explicit

Sub TestMe()

    Dim StartDate As Date

    StartDate = #6/11/2018#
    Cells(1, 1) = StartDate
    Cells(1, 1).NumberFormat = "DD/MMM/YYYY"
    Debug.Print Cells(1, 1).Text '11.Jun.2018

End Sub

As per the MSDN Application.International(Index) method returns information about the current country/region and international settings. 按照MSDN Application.International(Index)方法返回有关当前国家/地区和国际设置的信息。

When you apply index as xlDateOrder 当您将索引应用为xlDateOrder时

0 = month-day-year
1 = day-month-year
2 = year-month-day

Hence, we will be using this property to override the system behavior. 因此,我们将使用此属性来覆盖系统行为。 This is especially useful when the Excel-VBA project is used across geographies with various individual regional settings. 当Excel-VBA项目用于具有各种单独区域设置的地理区域时,这尤其有用。

Below, I am re-writing your code with a minor tweak. 在下面,我将稍作调整重新编写您的代码。 Format function returns the string but you are assigning it to a date which is causing this issue. 格式函数返回字符串,但是您将其分配给导致此问题的日期。

Sub cmdSubmit_Click()
    Dim StartDate As Date, EndDate As Date
    Dim strStartDate as String, strEndDate as String

    StartDate = DTPickStart.Value    ' 6/11/2018
    EndDate = DTPickEnd.Value        ' 6/24/2018

    strStartDate = Format(StartDate, "dd/mmm/yyyy")
    strEndDate = Format(EndDate, "dd/mmm/yyyy")

    Debug.Print StartDate           ' It will give 11/Jun/2018
    Debug.Print EndDate
End Sub

Thanks everyone. 感谢大家。 I managed to figure it out, but I'm giving my vote to jainashish as it's closer to how I could use the result to query my access db. 我设法弄清楚了,但是我投票赞成jainashish,因为它更接近于如何使用结果查询访问数据库。

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

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