简体   繁体   English

使用 VBA 读写后 Excel 日期更改(月和日交换)

[英]Excel date changed (month and day swapped) after reading and writing with VBA

I wrote two lines of simple VBA codes to read from and write a date to a cell, but unexpectedly the month and day swapped after the run.我写了两行简单的 VBA 代码来读取和写入一个单元格的日期,但出乎意料的是,运行后月份和日期交换了。 The steps are:步骤是:

  1. Enter a date "1/11/2017" in the cell A1.在单元格 A1 中输入日期“1/11/2017”。 The date is now displayed as "01/11/2017".日期现在显示为“01/11/2017”。

  2. Run the following lines of code:运行以下代码行:

     Dim s As String s = Cells(1, 1).value Cells(2, 1).value = s
  3. The cell B1 now displays "11/01/2017" with the month and day swapped.单元格 B1 现在显示“11/01/2017”,月和日交换。

My short date format setting in Region in Windows is "dd/MM/yyyy" so the string s stores the value "01/11/2017".我在 Windows 中的 Region 中的短日期格式设置为“dd/MM/yyyy”,因此字符串 s 存储值“01/11/2017”。 However, when writing to the cell, Excel implicitly converts the string s to a date assuming "mm/dd/yyyy" without following the date format setting in Region.但是,在写入单元格时,Excel 将字符串 s 隐式转换为假定“mm/dd/yyyy”的日期,而不遵循 Region 中的日期格式设置。 I tried to use different short date format settings but that does not change the way Excel converts the string.我尝试使用不同的短日期格式设置,但这并没有改变 Excel 转换字符串的方式。

So my question is: what could explain the swapping of the day and month?所以我的问题是:什么可以解释日和月的交换? What controls how Excel reads the date string and write to the cell?什么控制 Excel 如何读取日期字符串并写入单元格?

Dim s As String
s = Cells(1, 1).Value

s = Cells(1, 1).value will store the value of the cell in a String . s = Cells(1, 1).value将单元格的value存储在String Not what it shows in the cell.不是它在单元格中显示的内容。

For example例如

If your cell has 11/1/2017 but is formatted to show 01/11/17 then s will store 11/1/2017 .如果您的单元格有11/1/2017但被格式化为显示01/11/17那么s将存储11/1/2017 This is not a date.这不是约会。 It is a String .它是一个String If you do not believe me then try this如果你不相信我,那么试试这个

Dim s As String
s = Cells(1, 1).Value
Debug.Print s + 1 '<~~ You will get a Type Mismatch error

Now try this现在试试这个

Dim s As Date
s = Cells(1, 1).Value
Debug.Print s + 1 '<~~ You will get a proper date

==> When you are trying to store a String which contains a date to a cell which has General format then Excel will convert the string to date in a format what it feels is best (based on regional settings). ==>当您尝试将包含日期的String存储到具有General格式的单元格时,Excel 会将字符串转换为最佳格式的日期(基于区域设置)。 And this is what is happening in your case.这就是你的情况。

Resolution :分辨率

1 Declare a Date variable and then store the value in that. 1声明一个Date变量,然后将值存储在其中。 Ex: Dim s As Date例如: Dim s As Date

OR要么

2 Convert the string to a date and then store it. 2将字符串转换为日期,然后将其存储。 Excel will not change it. Excel 不会改变它。 And that is what DateValue(s) does.这就是DateValue(s)所做的。

Some Testing一些测试

Let's take these 3 scenarios让我们来看看这 3 个场景

Sub SampleA()
    Dim s As Date
    s = Cells(1, 1).Value
    Cells(2, 1).Value = s
End Sub

Sub SampleB()
    Dim s As String
    s = Cells(1, 1).Value
    Cells(2, 1).Value = DateValue(s)
End Sub

Sub SampleC() '<~~ As Suggested in the other answer
    Dim s As String

    s = Cells(1, 1).Value

    Cells(2, 1).NumberFormat = "dd/MM/yyyy"
    Cells(2, 1).Value = s
End Sub

Screenshot截图

在此处输入图片说明

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

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