vba 日月颠倒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28697150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
day and month are reversed
提问by user2443476
I have a cell with the following content :
我有一个包含以下内容的单元格:
01/02/2015
The cell is date formatted.
单元格是日期格式。
Then I copy the value and put it in my module class:
然后我复制该值并将其放入我的模块类中:
Set Line = New CTravelLine
Line.Date= Cells(1, 8).value
Everything works fine until the moment I put this value in another cell:
一切正常,直到我将此值放入另一个单元格的那一刻:
The value 01/02/2015 becomes 02/01/2015. I am using this format (dd/mm/yyyy). I have the impression that when the days are numerically lower than the month, the 2 values are reversed. The values are reversed whatever the method I tried:
值 01/02/2015 变为 02/01/2015。我正在使用这种格式 (dd/mm/yyyy)。我的印象是,当天数低于月份时,这两个值是相反的。无论我尝试什么方法,这些值都会反转:
Method 1:
方法一:
Dim WrdArray() As String, datetest As String
WrdArray() = Split(travelLine.Date, "/")
datetest= WrdArray(0) & "/" & WrdArray(1) & "/" & WrdArray(2)
Cells(5, 5) = datetest
Method 2:
方法二:
Cells(5, 5) = travelLine.Date
I don't understand how I can solve this problem. Thanks in advance for your help.
我不明白我该如何解决这个问题。在此先感谢您的帮助。
回答by Avidan
This might have happened due to 'Regional formatting problem'.
这可能是由于“区域格式问题”而发生的。
Excel has a habit of forcing the American date format (mm/dd/yyyy) when the dates have been imported from another data source. So, if the day in your date happens to be 1 - 12, then Excel will switch the date to mm/dd/yyyy.
当日期从另一个数据源导入时,Excel 有强制使用美国日期格式 (mm/dd/yyyy) 的习惯。因此,如果您的日期中的日期恰好是 1 - 12,那么 Excel 会将日期切换为 mm/dd/yyyy。
When dates are imported from a text file, there is an option in the VBA code to apply regional format which corrects this problem.
从文本文件导入日期时,VBA 代码中有一个选项可以应用区域格式来纠正此问题。
OR
或者
Change number format of date column in excelsheet from 'date' format category to 'text'; save it. (After Saving run the VBA Code if you have any. Now check whether the date format is 'text' or changed back to 'date'.)
将excel表格中日期列的数字格式从“日期”格式类别更改为“文本”;保存。(如果有的话,保存后运行 VBA 代码。现在检查日期格式是“文本”还是改回“日期”。)
If it has changed back to 'date' try to fix it as 'text'
如果它已变回“日期”,请尝试将其修复为“文本”
If it's 'text'; Correct the erroneous date cells and save the excel sheet. This will make dates not to change automatically to American Format.
如果是“文本”;更正错误的日期单元格并保存 Excel 工作表。这将使日期不会自动更改为美国格式。
回答by Abdul Hammoude
Long story short, I had a similar problem where the dates are working just fine in some cells but keep flipping in others regardless if I copy paste or enter manually, I did the whole data text to column and cell formatting solutions and all of that didn't work.
长话短说,我有一个类似的问题,其中日期在某些单元格中工作得很好,但在其他单元格中继续翻转,无论我是复制粘贴还是手动输入,我都将整个数据文本转换为列和单元格格式解决方案,所有这些都没有不行。
The solution actually is not in excel, its in the region and language setting ... I know right !!!
解决方案实际上不是在excel中,而是在区域和语言设置中......我知道对了!!!
To have the dates display as MM/DD/YYYY in the formats tab change the format to US.
要在格式选项卡中将日期显示为 MM/DD/YYYY,请将格式更改为 US。
To have the dates display as DD/MM/YYYY in the formats tab change the format to UK.
要在格式选项卡中将日期显示为 DD/MM/YYYY,请将格式更改为 UK。
Voila !
瞧!
回答by Victor
I had the same issue as you .
我和你有同样的问题。
Let me explain what I want to do :
让我解释一下我想做什么:
1) I have a csv file with some date. 2) I copy a range of my sheet in variable table. The range contain some columns with dates. 3) I make some manipulations on my table (very basic ones) 4) I transpose my variabe table (since only the last dimension of a variable table can be increase) 5) I put my variable table on a new sheet.
1)我有一个带有日期的csv文件。2)我在变量表中复制了我的工作表范围。该范围包含一些带有日期的列。3)我对我的表进行了一些操作(非常基本的操作) 4)我转置了我的变量表(因为只有变量表的最后一个维度可以增加) 5)我将我的变量表放在一张新表上。
What I found : There is no date issue after executing step 1) 2) 3) and 4). The date issue shows up when writing on the sheet...
我发现:执行步骤 1) 2) 3) 和 4) 后没有日期问题。在工作表上书写时会出现日期问题...
Considering what Avidan said on the post of Feb 24 '15 at 13:36, I guess it is excel which forces the american format mm/dd/yyyy... So I just change the date format at the very beginning of my program :
考虑到 Avidan 在 2015 年 2 月 24 日 13:36 的帖子中所说的话,我想是 excel 强制美国格式 mm/dd/yyyy ......所以我只是在我的程序开始时更改日期格式:
Before starting any manipulation date : 1) do .Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy") 2) execute your program 3) write the table on a sheet 4) back up to the date format you like directly on the sheet
在开始任何操作日期之前:1) 做 .Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy") 2) 执行你的程序 3) 在上面写表工作表 4) 直接在工作表上备份到您喜欢的日期格式
回答by user8471302
Just use:
只需使用:
Line.Date = cDate(Cells(1, 8).value2)

