日期自动转换 [英] Date auto conversion

查看:88
本文介绍了日期自动转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张名为02-05-2015的表格。
我使用

  Dim sheetname As String 
sheetname = ActiveSheet.Name
MsgBox(sheetname )

它在msg框中显示02-05-2015。
但是如果我使用

  Range(K2)。Value = sheetname 

它显示05-02-2015。如果单元格格式以dd / mmm / yyyy更改,则显示2015年2月5日。为什么会出现此问题。我不知道。
即使我使用

  Range(K2)Value = Format(sheetname,dd / mm / yyyy)

没有变化。
我需要05/02/2015或05-02-2015 in cell as date format。

解决方案

这是逐行设计当您将任何值放入单元格Excel将尽力找出该怎么做。这包括一个数字的实际数字(在一些DUNS号码或具有前导零的零件号码的情况下,并不总是希望的),并尝试将似乎是一个约会的日期调整到系统的区域设置中, dd / yyyy 或 dd / mm / yyyy 02-05-2015 的麻烦在于,它可能是2015年2月5日至2015年的或 2015年5月2日 DMY MDY 的决定是在计算机的系统区域设置(在控制面板中找到)进行的。



根据您的描述,您希望Range(K2)可以持有代表 2015年5月2日的日期,而不是2015年2月5日。似乎您的系统设置为默认值为 mm / dd / yyyy



这三步程序应该会让您得到结果正在寻找。

  With Range(K2)
.Value = Chr(39)& .Parent.Name
.TextToColumns目的地:= Range(K2),DataType:= xlFixedWidth,FieldInfo:= Array(0,4)
.NumberFormat =mm-dd-yyyy
结束与

这需要工作表名称,并将其作为文本值强制为K2使用单引号(例如' tick )。然后使用文本到列命令将其解释为 DMY 日期,并将其作为当前系统区域设置中的日期存储。最后,它应用一些单元格格式来获取所需的单元格显示值。


I have a sheet with name "02-05-2015". I use

Dim sheetname As String
sheetname = ActiveSheet.Name
MsgBox (sheetname)

It displays 02-05-2015 in msg box. But if I use

Range("K2").Value = sheetname

Its shows 05-02-2015. If cell format is changed as dd/mmm/yyyy, It shows 05-Feb-2015. Why this problem occurs. I don't know. Even if I use

Range("K2").Value = Format(sheetname,"dd/mm/yyyy")

No change occurs. I need 05/02/2015 or 05-02-2015 in cell as date format.

解决方案

This is behaviour-by-design. When you put any value into a cell Excel tries its best to figure out what to do with it. This includes making a number an actual number (not always desired in the case of something like a DUNS number or part number with leading zeroes) and trying to reconcile what appears to be a date into the system's regional settings of either mm/dd/yyyy or dd/mm/yyyy. The trouble with 02-05-2015 is that it could be either 05-Feb-2015 or 02-May-2015. The decision for either DMY or MDY is made on the computer's system regional setting (found in the Control Panel).

From your description, it would appear that you wish Range("K2") to hold a date representing 02-May-2015, not 05-Feb-2015. It also seems that your system is set to a default of mm/dd/yyyy.

This three step routine should get you the results you are looking for.

With Range("K2")
    .Value = Chr(39) & .Parent.Name
    .TextToColumns Destination:=Range("K2"), DataType:=xlFixedWidth, FieldInfo:=Array(0, 4)
    .NumberFormat = "mm-dd-yyyy"
End With

That takes the worksheet name and stuffs it into K2 by forcing it as a text value with a prefacing single quote (e.g. ' or tick). It then uses the Text-to-Columns command to interpret that as a DMY date and store it as a date in the current system regional settings. Finally, it applies some cell formatting to get the desired cell display value.

这篇关于日期自动转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆